Skip to content
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

Swagger documentation always documents a 200 Success response. #5

Open
skritikos opened this issue Mar 13, 2019 · 1 comment
Open

Comments

@skritikos
Copy link

I'm having an issue where no matter what SwaggerResponse status codes I include, or XML documentation I provide, the produced Swagger JSON contains a 200 Response. Is there a way around this?

Here is a modification of the provided example to return a 201 Created response rather than a 200 OK.

namespace TestFunctionApp
{
    public static class Function1
    {
        [FunctionName("Function1")]
        [SwaggerOperation("Function1", Tags = new[] { "Sample" })]
        [SwaggerResponse(type: typeof(string), statusCode: 201)]
        [SupportedMediaType("application/json")]
        [OptionalQueryParameter("name", typeof(string))]
        public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)]HttpRequestMessage req, ILogger log)
        {
            return req.CreateResponse(HttpStatusCode.Created);
        }
    }
}

Swagger Generation Function;

public static class GenerateSwaggerFunction
    {
        [FunctionName("GenerateSwaggerFunction")]
        [SwaggerIgnore]
        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", Route = "swagger/v1")]
            HttpRequestMessage req, ILogger log)
        {
            var services = new ServiceCollection();

            var functionAssembly = Assembly.GetExecutingAssembly();
            services.AddAzureFunctionsApiProvider(functionAssembly);

            // Add Swagger Configuration
            services.AddSwaggerGen(options => {
                // SwaggerDoc - API
                options.SwaggerDoc("v1", new Info
                {
                    Title = "Example API",
                    Version = "v1",
                    Description = "Example API"
                });

                // Add Enums to Swagger as String
                options.DescribeAllEnumsAsStrings();
                options.EnableAnnotations();
            });

            var serviceProvider = services.BuildServiceProvider(true);
            var content = serviceProvider.GetSwagger("v1");

            return new ContentResult
            {
                Content = content
            };
        }
    }

And here is the generated Swagger content;

{"swagger":"2.0","info":{"version":"v1","title":"Example API","description":"Example API"},"paths":{"/api/Function1":{"get":{"tags":["Sample"],"summary":"Function1","operationId":"Run","consumes":["application/json"],"produces":[],"parameters":[{"name":"name","in":"query","required":false,"type":"string"}],"responses":{"200":{"description":"Success"},"201":{"schema":{"type":"string"}}}},"post":{"tags":["Sample"],"summary":"Function1","operationId":"Run","consumes":["application/json"],"produces":[],"parameters":[{"name":"name","in":"query","required":false,"type":"string"}],"responses":{"200":{"description":"Success"},"201":{"schema":{"type":"string"}}}}}},"definitions":{},"tags":[]}
@jeromekol
Copy link

We are facing the same issue and there would be an attribute available on the Swashbuckle package: [SwaggerResponseRemoveDefaults]
Would it be possible to make it in this nuget available as well?

bherila pushed a commit that referenced this issue Jun 25, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants