question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Default swagger content-type for Response

See original GitHub issue

Since latest 2-3 releases (I don’t know exactly which one) I notice that the default content-type selected on the swagger HTML dropdown menu for the method reponse, is not "application/json" but "text/plain".

If I don’t change it everytime before clicking on Try button, I get an error because no content-type negotiation for responses is allowed in my application. I think that "application/json" should be the right default value, but anyway is it possible to set the default content-type for response in Swagger configuration to avoid changing it everytime?

This is my actual configuration

public void ConfigureServices(IServiceCollection services)
{
	services.AddMvc(config =>
	{
		// HTTP 406 when not supported format is requested by client
		config.ReturnHttpNotAcceptable = true;
	})
	.SetCompatibilityVersion(CompatibilityVersion.Version_2_1)
	// Add FluentValidation
	.AddFluentValidation(fv => fv.RegisterValidatorsFromAssemblyContaining<MachineCreateModelValidator>());

	// Add API versioning
	services.AddApiVersioning(options =>
	{
		options.AssumeDefaultVersionWhenUnspecified = true;
		options.DefaultApiVersion = new ApiVersion(1, 0);
		options.ReportApiVersions = true;
	});
}

The HTML result

nswag content-type

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:4
  • Comments:14 (4 by maintainers)

github_iconTop GitHub Comments

6reactions
OculiViridicommented, Jul 23, 2019

I temporary solved the problem by simply putting the Produces attribute on the top of all of my controllers, like this:

[Produces("application/json")]
[ApiVersion("1.0")]
[SwaggerTag("Machines", Description = "Core operations on machines.")]
public class MachinesController : ControllerBase
{ }

UPDATE 2019/07/23

I forgot to mention that if you are using a common base class for all your Controllers, you can just put the Produces attribute only on the base class.

[ApiController]
[Produces("application/json")]
[Route("api/v{version:apiVersion}/[controller]/[action]")]
public abstract class BaseController : ControllerBase
{
    // TODO: Add needed common methods/properties for all Controllers
}

[Authorize]
[ApiVersion("1.0")]
[OpenApiTag("Machines", Description = "Operations on machines")]
public class MachinesController : BaseController
{
    // TODO: Add controller methods
}

[Authorize]
[ApiVersion("1.0")]
[OpenApiTag("Machines", Description = "Operations on machines")]
public class DriversController : BaseController
{
    // TODO: Add controller methods
}

@RSuter But I don’t know if it is really necessary, since you said that should come automatically. Any news?

4reactions
OculiViridicommented, Jul 23, 2019

Also There are one more temporary resolving way.

Just override Produces on AddSwaggerDocument when add swagger document on startup.cs

services.AddSwaggerDocument(settings =>
{
	settings.PostProcess = document => document.Produces = new List<string>
	{
		"application/json",
		"text/json"
	};
});

@js-lee0624

Just a note for users reading the suggestion, from NSwag v13, there was a refactoring, so the method is called AddOpenApiDocument instead of AddSwaggerDocument.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Describing Responses
Response Media Types · HTTP Status Codes · Response Body · Response That Returns a File · anyOf, oneOf · Empty Response Body...
Read more >
How do I set or remove the Default Response Content ...
The default response content type when using SwashBuckle is text/plain . How can I change the default to be application/json or even remove ......
Read more >
Swashbuckle Pro Tips for ASP.NET Web API – Content Types
And those four content types are the default response ones – application/json , text/json , application/xml and text/xml .
Read more >
Custom Response - HTML, Stream, File, others - FastAPI
If you want to override the response from inside of the function but at the same time document the "media type" in OpenAPI,...
Read more >
Solved: Swagger compliance assertion with content type dif...
The swagger compliance assertion has a too strict validation on the content type. Currently only the content type application/xml and ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found