-
-
Notifications
You must be signed in to change notification settings - Fork 539
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
Use System.Text.Json in JSON Schema generator #1014
Comments
/cc @steveharter @joshfree is there already a replacement for the contract resolver available? I once saw some related classes but they were internal (no access to metadata)... |
For 3.0 there are no public APIs to return the metadata. These were considered but considered out of scope for this release because we are focusing on a minimum viable product for mainstream scenarios. I'd like to expose the metadata, which would also be required for a new type of "converter" that I would like to add. The new converter would handle some scenarios the existing one can't. |
Ok. As long as metadata is not exposed by System.Text.Json we cannot support this new library in NJsonSchema (JSON Schema generation) and NSwag (Swagger/OpenAPI) - so this might be a huge blocker for people who want to use the new serializer in ASP.NET Core AND OpenAPI. The solution for now is to use System.Text.Json for serialization and add NSwag with a Newtonsoft.Json contract resolver which resembles the System.Text.Json serializer behavior... |
I thought System.Text.Json was .NET Standard 2.1, not 2.0... If so, it wouldn't run on .NET Framework at all. |
It seems to support .net standard 2.0: |
I was thinking it used Span and made use of new things in the .NET Core runtime which they weren't going to add to .NET Framework. Maybe I'm confusing that with the latest C# language features that they added which will not be supported on .NET Framework. |
I was wrong about that. It says here https://devblogs.microsoft.com/dotnet/try-the-new-system-text-json-apis/ that it works with .NET Framework. |
.NET Standard 1.x has been end of support therefore I think there's no need to support .NET Standard 1.x. |
That may be, but, don't expect people to move off .NET Standard 2.0 anytime soon. |
You can multi-target your library to resolve it: <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netstandard1.4;netstandard2.0;netstandard2.1</TargetFrameworks>
</PropertyGroup>
<ItemGroup>
<PackageReference Condition="'$(TargetFramework)' == 'netstandard2.1'" Include="System.Text.Json" Version="4.6.0" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
</ItemGroup>
</Project> in .cs file #if NETCOREAPP3_0 || NETSTANDARD2_1
....
#else
...
#endif |
Thx, but referencing the library is the small problem, the blocker is this: |
@RicoSuter - are you able to provide any more info on the solution for now? How do I add NSwag with a Newtonsoft.Json contract resolver? |
If you call AddOpenApiDocument() without using Newtonsoft in ASP.NET Core itself, it should still work but with a the default contract resolver. If the default contract resolver does not reflect what the actual serializer is doing, then you need to change the contract resolver so that it reflects the serializers (in the AddOpenApiDocument(options => options.SerializerSettings = ...) |
I think at least NJsonSchema should recognize JsonIgoreAttribute and other attributes in System.Text.Json while generating OpenApi schema. |
JsonIgoreAttribute from System.Text.Json? |
Sorry, that is a typo. It should be JsonIgnoreAttribute. |
and I probably missed other attributes... |
We'd need to implement a custom Newtonsoft contract resolver which reads the System.Text.Json options and attributes and correctly maps them... see /cc @Sinhk |
Ref: 5981ecb |
@steveharter is there already a roadmap for exposing serializer metadata (contract resolver)? Otherwise we need to use reflection and reimplement all your rules manually in the schema generator... (I really want to avoid that). |
@RicoSuter Are there any additional steps where you could use some help? I would like to update my F# wrapper with support for F# types to use the System.Text.Json support and would be happy to try to help get this over the finish line. |
@panesofglass thanks for your post. Currently we map the System.Text.Json “rules” manually to the newtonsoft metadata with a custom contract resolver. This is a quick solution for now until STJ offers metadata directly. There are probably lots of edge cases and attributes missing... would be great if someone could help complete this (also more unit tests needed). Apart from that you can just set the SerializerOptions property on the generator settings and it will use these rules. What is missing for you? |
I don’t know whether anything is missing. I was uncertain whether this had been released, but it seems it has? I’m happy to look into adding some tests. |
I have not yet added tests to this repo, but I've been able to switch to |
Open questions:
Current implementation (NJS 10.1.15+, NSwag v13.5.0+):
See https://github.com/RicoSuter/NJsonSchema/blob/master/src/NJsonSchema/Generation/SystemTextJsonUtilities.cs
The text was updated successfully, but these errors were encountered: