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

Calling .AddSystemTextJsonMergePatch() adds the application/merge-patch+json as default option in Swagger #61

Closed
harvzor opened this issue Jun 2, 2023 · 3 comments · Fixed by #67

Comments

@harvzor
Copy link

harvzor commented Jun 2, 2023

After calling .AddSystemTextJsonMergePatch() in the startup, it adds the application/merge-patch+json content type to Swagger.

However, this means that other endpoints will default to this type, even if they're not PATCH:

[HttpPut("broken/{id:int}")]
public IActionResult BrokenUpdateCustomer([FromRoute] int id, [FromBody] BrokenCustomerPutDto brokenCustomerPutDto)
{
    // ...
}

Probably it defaults to this because the list is in alphabetical order:

image

I think this is more of an issue on Swashbuckle's side. I'm not expecting this to be fixed (probably it can't be fixed here) - I just wanted this to be documented 😄

Workaround

Put [Consumes(MediaTypeNames.Application.Json)] on either every other method that accepts a body, or just apply it to the class:

[Consumes(MediaTypeNames.Application.Json)]
public class CustomersController : ControllerBase
{
    // ...
}
@Morcatko
Copy link
Owner

Morcatko commented Jun 6, 2023

Interesting.
I never noticed it.
It seems to show all content types even if I remove AddNewtonsoftJsonMergePatch and comment out all non-default settings in swagger options in my application.

It looks like it is really a swagger issue but I have no time right now to investigate more.

@poizan42
Copy link
Contributor

That's because SystemTextJsonMergePatchInputFormatter does not implement GetSupportedContentTypes. It essentially declares that it supports every possible model type, which in reality it does not as its just going to fail on either getting the generic arguments of a non-generic type at

or the cast at

@Morcatko
Copy link
Owner

Thanks for the fix. It will be available as v6.0.2 soon

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants