RicoSuter / NSwag

The Swagger/OpenAPI toolchain for .NET, ASP.NET Core and TypeScript.
http://NSwag.org
MIT License
6.79k stars 1.29k forks source link

XML Request sample is not generated in the UI #1392

Open munafk opened 6 years ago

munafk commented 6 years ago

I am using Swagger 11.17.15 version. I get only application/json as request and response types in Swagger UI. However I need application/xml instead. I tried adding [Consumes] on top of the controller, still no result. Please help!

RicoSuter commented 6 years ago

Have you tried the new AspNetCoreToSwaggerGenerator (i.e. UseSwaggerUiWithApiExplorer()), I think this generator processes the [Consumes] attributes and adds more produces mime types?

munafk commented 6 years ago

I tried, but I am not getting any operations listed. Is there a concrete example you can point me to?

RicoSuter commented 6 years ago

https://github.com/RSuter/NSwag/blob/master/src/NSwag.Sample.NETCore21/Startup.cs#L39

munafk commented 6 years ago

Hard luck. It does not handle Consumes attributes.

StasPerekrestov commented 5 years ago

Just a nasty workaround in a case if you have to accept XML as a request body

 .UseSwagger(config =>
               {
                    config.PostProcess = (document, request) =>
                                    {
                                        foreach (SwaggerOperationDescription operationDescription in document.Operations)
                                        {
                                            string[] actualConsumes = operationDescription.Operation.ActualConsumes.ToArray();
                                            const string xmlMediaType = "application/xml";
                                            const string jsonMediaType = "application/json";
                                            if (actualConsumes.Any(a => a.Equals(xmlMediaType, StringComparison.OrdinalIgnoreCase)))
                                            {
                                                SwaggerOperation operation = operationDescription.Operation;
                                                IDictionary<string, OpenApiMediaType> content = operation.RequestBody.Content;
                                                OpenApiMediaType openApiMediaType = content[jsonMediaType];
                                                // content.Remove(jsonMediaType);
                                                content.Add(xmlMediaType, openApiMediaType);
                                            }
                                        }
                                    };
               });
MagicJohnJang commented 4 years ago

I still can't get the actualConsumes with xml, did I miss anything?

image

dkampli commented 2 years ago

I'm currently facing the same issue. I use NSwag 13.15.5 version. any updates on the solution to this problem ?