Allow Swagger to return Content Type XML or JSON

At the moment only application/json is available. What is required to add application/xml as option to the selectbox?

Response

The most annoying thing is that two “Response Content Type” dropdowns appear in swagger-ui, one at the top of the operation (above the parameters) and one embedded within my Message Body parameter area (which is redundant and seems to be completely ignored).

For API calls with no Message Body parameter (i.e. a GET as opposed to PUT, POST) the UI works great and I have no complaints.

{
"swaggerVersion": "2.0",
"resourcePath": "/document_list",
"apis": [
{
"path": "/document_list",
"operations": [
{
"httpMethod": "GET",
"summary": "Search for documents based on a set of criteria",
"produces": [
"application/json",
"application/xml",
"application/atom+xml"
],

In App_Start>SwaggerConfig.cs

// Similar to Schema filters, Swashbuckle also supports Operation and Document filters:
//
// Post-modify Operation descriptions once they’ve been generated by wiring up one or more
// Operation filters.
//
c.OperationFilter();

Swagger XML and JSON options

 
public class CustomResponseType : IOperationFilter
        {
            public void Apply(Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription)
            {
                if (operation.produces.Contains("text/json"))
                {
                    operation.produces.Remove("text/json");
                }
                if (!operation.produces.Contains("application/json"))
                {
                    operation.produces.Add("application/json");
                }
                if (!operation.produces.Contains("application/xml"))
                {
                    operation.produces.Add("application/xml");
                }
              
            }
        }
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s