RicoSuter / NSwag

The Swagger/OpenAPI toolchain for .NET, ASP.NET Core and TypeScript.
http://NSwag.org
MIT License
6.73k stars 1.29k forks source link

Feature: System.Text.Json #4083

Open tamusjroyce opened 2 years ago

tamusjroyce commented 2 years ago

Is there work in progress to move to System.Text.Json.Serialization? Not asking create this serialization option feature, as it would be a major change. But curious if it has been considered or is in the works. Was thinking about taking a try at it.

https://docs.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-migrate-from-newtonsoft-how-to?pivots=dotnet-6-0#table-of-differences-between-newtonsoftjson-and-systemtextjson

As a featurish question, feel free to close at any time (no response necessary. especially if you don't have the time).

Thanks for the great library! Visual Studio built-in support works great, even for Azure Functions calling other Azure Functions.

SebastianStehle commented 2 years ago

I think many parts are already independent from the serializer. For you as a developer it does not really matter how OpenAPI documents are serialized.

I have used nswag with JSON.NET and STS and the only thing missing for me is support for inheritance with STS.

pekspro commented 2 years ago

There already is some experimental support for System.Text.Json:

image

oduis commented 2 years ago

Careful though, the new SystemTextJson ist still pretty buggy (relly experiemental):

chrisheil commented 7 months ago

Careful though, the new SystemTextJson ist still pretty buggy (relly experiemental):

This got me in a bind recently. I'm guessing there is a way to update this behavior in NSwag but for now I just rolled back to Newtonsoft. I'll add this here for anyone else who runs into this issue.

I have two services talking to each other with NSwag generated models using System.Text.Json. One of the properties is a bool with an auto property set to true. NSwag adds the JsonIgnore attribute with WhenWritingDefault. Something like this:

public class FooBar
{
    [System.Text.Json.Serialization.JsonPropertyName("isCondition")]
    [System.Text.Json.Serialization.JsonIgnore(Condition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingDefault)]
    public bool IsCondition { get; set; } = true;
}

If IsCondition is set to false when making a request, that property will not be serialized into the request json. The server recieving the request will then deserialize the json into the FooBar object, and since IsConditon was not included it will use the auto property value, which is true.

I would imagine the solution is either use WhenWritingNull instead of WhenWritingDefault, and there seems to be ways to specify custom options in the config, but it was easier to use Newtonsoft at this point.

SkinnySackboy commented 2 months ago

Hi,

It's been a while now, is System.Text.Json still experimental in 2024?