Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

STJ for serialization and NSJ for NSwag #1510

Closed
bdurrani opened this issue Mar 28, 2022 · 2 comments
Closed

STJ for serialization and NSJ for NSwag #1510

bdurrani opened this issue Mar 28, 2022 · 2 comments

Comments

@bdurrani
Copy link

Hi there

Im trying to implement the solution posts by @RicoSuter here

#1014 (comment)

"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..."

Sorry, Im pretty new to this

Im using .NET 6
I have a reference to Microsoft.AspNetCore.Mvc.NewtonsoftJson added to the project

I have the following in my startup.cs
I'm using STJ for serialization

  services.AddProblemDetails(ConfigureProblemDetailsMiddleware)
            .AddControllers()
            .AddJsonOptions(options =>
            {
                Helpers.ConfigureJsonSerializerOptions(options.JsonSerializerOptions);
            });
        services.AddOpenApiDocument(settings =>
        {
            settings.Title = SwaggerServiceName;
            settings.SerializerSettings = new Newtonsoft.Json.JsonSerializerSettings();
            settings.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
        });

In AddOpenApiDocument(), SerializerSettings is always null.
I know I'm supposed to use SystemTextJsonUtilities. ConvertJsonOptionsToNewtonsoftSettings() somewhere, but I dont have access to my STJ settings in AddOpenApiDocument().

I'm probably doing something silly here. But any code snippets would be appreciated.

Thanks for your help.

@RicoSuter
Copy link
Owner

You do not need to change the SerializerSettings in AddOpenApiDocument as it will automatically use the configuration from the AddJsonOptions... or what are you trying to solve here?

@bdurrani
Copy link
Author

🤦🏽‍♂️ You're right

First of all, thanks for responding.

At first, I was trying to figure out why the camel case Json naming policy for enums was not working. Then I found the issue I linked above and went into a rabbit hole. But the actual fix was I had to upgrade NSwag from 13.15.7 to 13.15.10. That seems to work great!

Now I'm trying to see if there is a way to specify custom names for enums that will be used for swagger and serialization.

For example, for the following enum

public enum TestEnum
{
    /// <summary>
    /// This is the thing of one
    /// </summary>
     [EnumMember(Value = "Another-One")]
    OneThing,
    /// <summary>
    /// Another thing of two
    /// </summary>
     [EnumMember(Value = "Trick-Or-Treat")]
    TwoThing
}

I want swagger and the STJ (serialize and deserialize) to use the string in the EnumMember attribute, not the actual name of the enum. Somehow NSwag is respecting EnumMemberAttribute, but STJ does not.

But then I found this issue flagging my problem (while writing this response).

So I guess I'm going to dig into that some more.

I'll go ahead and close this issue 😅

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants