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

Switch to System.Text.Json for (de)serialization #767

Open
kipusoep opened this issue Jan 19, 2024 · 7 comments
Open

Switch to System.Text.Json for (de)serialization #767

kipusoep opened this issue Jan 19, 2024 · 7 comments
Assignees
Labels
enhancement New feature or request

Comments

@kipusoep
Copy link

We're using the new System.Text.Json namespace for WebApi JSON serialization (I think it's OOTB like this nowadays).
TimeSpan objects are serialized in the format 23:59:59 and Newtonsoft.Json is unable to deserialize this.
It would be nice if the generated Open API code would use System.Text.Json instead.

@christianhelle christianhelle self-assigned this Jan 19, 2024
@christianhelle christianhelle added the enhancement New feature or request label Jan 19, 2024
@christianhelle
Copy link
Owner

@kipusoep Thanks for taking the time to report this

Which code generator are you thinking of? I have 100% control over Refitter since I wrote the tool and NSwag features the option to configure which JSON serialization library to use. I have very little control over the other code generators since I just piggyback over them

@kipusoep
Copy link
Author

The Open API generator.

@christianhelle
Copy link
Owner

@kipusoep There's a similar issue on the OpenAPI Generator repo and one of the comments is about a fork that implements this. Hopefully, this becomes a pull request and ends up in the OpenAPI Generator itself. Otherwise, someone should implement this, that could be me, you, or someone else

@rasmusrim
Copy link

Here is the fork. It has worked well on several of my projects so far: https://github.com/rasmusrim/openapi-generator-csharp-system-text-json

@kipusoep
Copy link
Author

Is there a relatively easy way to use your fork with the VS tool?

@christianhelle
Copy link
Owner

christianhelle commented Mar 14, 2024

@kipusoep The mentioned fork looks like Mustache templates to me, and my extension supports using custom mustache templates. You can try to clone that repo to a local folder like C:\mustache-templates\system-text-json and then configure the OpenAPI Generator options in my extension to something like this:

image

@christianhelle
Copy link
Owner

@kipusoep @rasmusrim The mustache templates can be used to generate code, but the generated code doesn't build. At least not out-out-of-the-box. I guess some tweaking of the templates would help

Here's the output:
OpenAPI-Generator-Output.zip

The immediate problems that I noticed are the following:

  • Enums are decorated with [JsonConverer(typeof(JsonStringEnumMemberConverter)) but JsonStringEnumMemberConverter doesn't exist
  • Both Newtonsoft.Json and System.Text.Json are being used

Mustache templates are hard to work with when starting from scratch but they are not that hard to modify, so maybe a few tweaks would make this work.

Eventually this fork, once it works, should be merged into OpenAPI Generator itself

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

No branches or pull requests

3 participants