Skip to content

(STJ) Possibility to pass as parameter an instance of JsonSerializerOptions in constructor to follow Microsoft's best practices #4701

@bdovaz

Description

@bdovaz

According to the official documentation it says that it should not be instantiated continuously:

https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/configure-options?pivots=dotnet-8-0#reuse-jsonserializeroptions-instances

In the template you can see that it is instantiated without the possibility of passing an instance to it from outside.

Relevant sections of the template:

https://github.com/RicoSuter/NSwag/blob/v14.0.1/src/NSwag.CodeGeneration.CSharp/Templates/Client.Class.liquid#L13-L17

https://github.com/RicoSuter/NSwag/blob/v14.0.1/src/NSwag.CodeGeneration.CSharp/Templates/Client.Class.liquid#L68-L81

Code in charge of generating the instantiation in NJsonSchema:

https://github.com/RicoSuter/NJsonSchema/blob/v11.0.0/src/NJsonSchema.CodeGeneration.CSharp/CSharpJsonSerializerGenerator.cs#L78-L89

I would like it to be in the constructor and not in a static way by following dependency injection patterns which is how I instantiate the clients.

EDIT: right now I have manipulated the template to do what I want but it is difficult to update NSwag if you have modified a template like this because every time there is a change you have to spend time to recompose everything (nswag's changes + mine).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions