We are attempting to integrate System.Text.Json into our REST API and rest clients, previously using Newtonsoft.
We are using nswag CSharpClientGenerator.GenerateFile to generate our REST API clients via code (not nSwag Studio).
Because of the casing differences between the property names and the json name, items are not correctly deserializing. We have been able to over come this by setting PropertyNameCaseInsensitive = true.
We were able to do this via a partial class as below:
partial class MakeClient
{
partial void UpdateJsonSerializerSettings(JsonSerializerOptions settings)
{
settings.PropertyNameCaseInsensitive = true;
}
}
I really don't want to create these partial classes for every client.
Is there an easier way to do this, such as injecting JsonSerializerOptions into the client, or some type of global setting?
Try setting the Newtonsoft's one of predefined resolvers: CamelCasePropertyNamesContractResolver in nSwag Setting like:
Alternatively, if you are maintaining a separate nswag setting for each of your API Specification, you can add there too.
This resolver will be set up in your respective FromJson and ToJson methods in your schemas, provided you have enabled these methods to be generated via NSwag. There is a setting for that as well.
This will code generate as: