Skip to content

openapi with newtonsoft.json suppport generate incorrect request/response model schema #60458

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

Closed
1 task done
John0King opened this issue Feb 18, 2025 · 11 comments
Closed
1 task done
Labels
area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates feature-openapi ✔️ Resolution: By Design Resolved because the behavior in this issue is the intended design. Status: Resolved

Comments

@John0King
Copy link
Contributor

John0King commented Feb 18, 2025

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

when using OpenAPI from Microsoft and Newtonsoft.json , it still use System.Text.Json to generated OpenApi document , that cause the request/response model's json schema be incorrect.

Expected Behavior

expect to generate the model Foo has a NewtonName property.

Steps To Reproduce

  1. create a web api project
  2. use follow code
// program.main
builder.Services.AddControllers()
    .AddNewtonsoftJson();


// controller

[HttpGet(Name = "Get")]
public Foo Get(Foo foo)
{
    return foo;
}

public class Foo
{
    [System.Text.Json.Serialization.JsonPropertyName("StjName")]
    [Newtonsoft.Json.JsonProperty("NewtonName")]
    public required string Name { get; set; }
}
  1. run the project
  2. open /openapi/v1.json and see the result

Exceptions (if any)

No response

.NET Version

9.0.200

Anything else?

No response

@martincostello martincostello added feature-openapi area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates and removed area-web-frameworks area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc labels Feb 18, 2025
@martincostello
Copy link
Member

Microsoft.AspNetCore.OpenApi doesn't support Newtonsoft.Json because it is built on top of the JSON schema support in System.Text.Json. It also supports native AoT, which Newtonsoft.Json is not compatible with.

If you wish to use the new OpenAPI support with MVC you'll need to either migrate to System.Text.Json, or double-annotate/configure all your models so you get equivalent behaviour from both JSON implementations.

@John0King
Copy link
Contributor Author

@martincostello Newtonsfot.Json and S.T.J have many difference , and can I switch the "JsonSchemaService" with a Newtonsoft.Json version in DI ?

@martincostello
Copy link
Member

I don't think you can because it's fundamentally built on top of features added to System.Text.Json for .NET 9.

@martincostello
Copy link
Member

If you need Newtonsoft.Json support, use NSwag or Swashbuckle to generate OpenAPI documents.

@John0King
Copy link
Contributor Author

the Swashbuckle also use System.Text.Json by default , but it allow to replace the schemaService with a Newtonsoft.Json version

Image

I think the Micorosoft.AspNetCore.OpenApi package need more abstraction

@martincostello
Copy link
Member

It does yes, because it has better performance than Newtonsoft.Json and async support. And it's a lot of work to maintain two implementations to allow for it, and there isn't 100% parity between them.

I highly doubt Microsoft.AspNetCore.OpenApi will support it for the reasons I've already outlined, and if it did you'd have to wait until at least .NET 10 to do so.

A better investment of your time would be to move to System.Text.Json to improve the performance of your system as a whole. Otherwise you'll need to use one of the other suggested libraries to produce OpenAPI documents.

@martincostello
Copy link
Member

Further, Microsoft.AspNetCore.OpenApi depends on Microsoft.OpenApi, which itself is built on top of System.Text.Json for v2 that .NET 10 will depend on, so I think that makes it effectively impossible for it to support Newtonsoft.Json without re-implementing everything a second time.

@captainsafia
Copy link
Member

@John0King Thanks for filing this issue!

As @martincostello mentioned, the M.A.OpenApi package builds on-top of System.Text.Json and I don't anticipate that we'll be supporting another serializer out of the box in the future.

With that said, the closest analogy to the code that you posted here is creating a copy of your Newtonsoft.Json serializer settings and passing them to the ConfigureHttpJsonOptions call in the API.

the Swashbuckle also use System.Text.Json by default , but it allow to replace the schemaService with a Newtonsoft.Json version

Image

I think the Micorosoft.AspNetCore.OpenApi package need more abstraction

@captainsafia captainsafia added the ✔️ Resolution: By Design Resolved because the behavior in this issue is the intended design. label Feb 18, 2025
@John0King
Copy link
Contributor Author

I'm still using Swashbuckle , I just investigate that can I switch to Microsoft.AspNetCore.OpenApi , because that sound like a framework buildin solution (and it should have replacable abstractions like other components in the framework, but it's not, it only have manipulate abstractions eg. IOpenApiDocumentTransformer,IOpenApiOperationTransformer,IOpenApiSchemaTransformer).

IMO currently Microsoft.AspNetCore.OpenApi lack of follow features

  • no support for reading comments from xmldoc
  • not support swtich other schema generator

and the reason for continue to use newtonsoft.json instead of S.T.J is

  • can not deserilize number to string property( it only support read string to number)
  • no buildin custom datetime formate solution
  • NamingStragy can not be decorate on class/struct level

@martincostello
Copy link
Member

  • no support for reading comments from xmldoc

It will be part of .NET 10 preview 2: #39927 (comment)

can not deserilize number to string property( it only support read string to number)

Can you elaborate? This should be supported via JsonSerializerOptions.NumberHandling and these values (e.g. WriteAsString).

no buildin custom datetime formate solution

If it's custom then by definition it's not built-in. What exactly are you trying to do?

NamingStragy can not be decorate on class/struct level

If that's the case, then I suggest opening an API suggestion in the dotnet/runtime repository to add such support. There's plenty of time for such a feature to land in .NET 10 if it was approved.

Copy link
Contributor

This issue has been resolved and has not had any activity for 1 day. It will be closed for housekeeping purposes.

See our Issue Management Policies for more information.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates feature-openapi ✔️ Resolution: By Design Resolved because the behavior in this issue is the intended design. Status: Resolved
Projects
None yet
Development

No branches or pull requests

3 participants