-
Notifications
You must be signed in to change notification settings - Fork 196
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Doesn't support multiple operations (for example, GET, POST) for one endpoint #68
Comments
@sergeysolod17 Thanks for the question! That's correct. Although technically one Function method can take more than one HTTP verb/method, semantically it should be two different operations. Therefore, I would recommend separating multiple verbs/methods from each other, and giving them respective operation IDs. |
Thank you! |
According to your answer, the Visual Studio Azure Function template function then is wrong to code the way it does. It provides misleading support for the function via the Swagger UI and json output. As it stands the function template (Http trigger with OpenAPI) code, and the behaviour of OpenAPI are out of sync and need addressing in some way. Etiher allow it to work the way the OP expressd (Which I very much prefer), or change, or pass to the responsible persons to change, the VS template to correctly code to your assertion and recomendation. |
I went over to look at the templates github repo, and they removed OpenAPI support ! |
Yes they did.. but it seems that they added a separate OpenAPI template |
I'm a bit confused here. My understanding is that in a well designed API, we can leverage the same method with different HTTP verbs/operations. This is clearly articulated in the Swagger/Open API documentation, and based on my experience, a fairly common and intuitive architecture.
This example is identical to how the Microsoft Graph API is documented and functions. For example, on the the user resource type, we can both list all users and create a new user on the same path:
This pattern is repeated endlessly throughout even the Microsoft API specifications. Are you suggesting we need to instead do something like?
Apart from duplicating code it seems like an anti-pattern. If it helps anyone, using routes seems to at least mitigate the unique function name requirement part of this problem and allow for Swagger to generate docco for multiple verbs on the same API path. |
For future reference: The following is a sample in VS2022 of a GET and POST working for the same REST resource (Hello) with Swagger. As mentioned above, the key to this working as you want it to is using This works but means, as of now, you cant really have a function handling more than one HTTP method for a resource AND leverage swagger. I leave the arguments about THAT to the community.
|
Came across this issue today after instantiating a default Azure Functions project in Visual studio and realising that the Swagger docs only respect the first method in the function's methods params array. For years we have implemented different behaviours for the same endpoint depending on the HTTP method used to communicate with them as HTTP methods themselves are semantically implicit - we would really appreciate seeing this implemented properly as at the moment it's neither clear why this behaves the way it does, nor is it compatible with this rather widely-used approach to API development. Functions developed this way are already in production and being used by 3rd parties, refactoring to accommodate this would not be semantically beneficial and would introduce a needless breaking change to our dependants. |
Just encountered this same issue today while attempting to implement OpenAPI with a function that accepts multiple methods. This seems crazy that it hasn’t been addressed. I’m not sure how to structure things now. |
Would love for this to be addressed as I have a function that has both Get and Post routes. Unfortunately, I had to separate the functionality out into it's own worker function to be called within two other function definitions with different function names. Luckily, it seems like I can have two function definitions with the same route so the path will remain the same and functionality from the outside will not change. Would've been nice to just have two OpenApiOperation tags above one function definition with the ability to specify Get/Post. |
When I scanned the generated file swagger.json I found desription for one endpoint:
swagger: '2.0'
info:
title: Azure Functions Open API Extension
version: 1.0.0
host: localhost:7071
basePath: /api
schemes:
paths:
/HttpTriggerCSharp1:
get:
tags:
- name
operationId: Run
produces:
- text/plain
parameters:
- in: query
name: name
description: The Name parameter
required: true
type: string
responses:
'200':
description: The OK response
schema:
type: string
security:
- function_key: [ ]
securityDefinitions:
function_key:
type: apiKey
name: code
in: query
But in C# code we have support two types HTTP request GET and POST:
namespace Company.Function
{
public static class HttpTriggerCSharp1
{
[FunctionName("HttpTriggerCSharp1")]
[OpenApiOperation(operationId: "Run", tags: new[] { "name" })]
[OpenApiSecurity("function_key", SecuritySchemeType.ApiKey, Name = "code", In = OpenApiSecurityLocationType.Query)]
[OpenApiParameter(name: "name", In = ParameterLocation.Query, Required = true, Type = typeof(string), Description =
"The Name parameter")]
[OpenApiResponseWithBody(statusCode: HttpStatusCode.OK, contentType: "text/plain", bodyType: typeof(string),
Description = "The OK response")]
public static async Task Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");
}
The text was updated successfully, but these errors were encountered: