Skip to content
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

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

Closed
bdovaz opened this issue Jan 11, 2024 · 1 comment · Fixed by #4888

Comments

@bdovaz
Copy link

bdovaz commented Jan 11, 2024

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

@bkoelman
Copy link
Contributor

Couldn't agree more. This looks similar to #4662.

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

Successfully merging a pull request may close this issue.

2 participants