NSwag icon indicating copy to clipboard operation
NSwag copied to clipboard

XML Request sample is not generated in the UI

Open munafk opened this issue 6 years ago • 7 comments

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!

munafk avatar Jun 18 '18 06:06 munafk

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

RicoSuter avatar Jun 18 '18 07:06 RicoSuter

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

munafk avatar Jun 18 '18 09:06 munafk

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

RicoSuter avatar Jun 18 '18 11:06 RicoSuter

Hard luck. It does not handle Consumes attributes.

munafk avatar Jun 18 '18 12:06 munafk

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);
                                            }
                                        }
                                    };
               });

StasPerekrestov avatar May 23 '19 17:05 StasPerekrestov

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

image

MagicJohnJang avatar Jul 15 '20 06:07 MagicJohnJang

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

dkampli avatar Jun 21 '22 19:06 dkampli