RicoSuter / NSwag

The Swagger/OpenAPI toolchain for .NET, ASP.NET Core and TypeScript.
http://NSwag.org
MIT License
6.69k stars 1.24k forks source link

Question: Generate C# dto classes with same casing of the definition #4179

Open alkampfergit opened 1 year ago

alkampfergit commented 1 year ago

I've searched but I was not able to find any option to generate C# client with DTO with the same casing of the API

As an example, I have an api with standard camelCase name, if I generate typescript client evertying is ok.


export interface AceReducedDto {
    id?: string | undefined;
    name?: string | undefined;
    enabled: boolean;
}

In typescript client, correctly all properties starts with lowercase letter. If you look at the snippet below, C# client uses only PascalName properties with the correct Newtonsoft attributes that contains properties with the correct casing.

I have a scenario where I need the real C# property to have the exact casing as I specified in swagger file.

    [System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "10.3.11.0 (Newtonsoft.Json v11.0.0.0)")]
    public partial class AceReducedDto 
    {
        [Newtonsoft.Json.JsonProperty("id", Required = Newtonsoft.Json.Required.Default, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
        public string Id { get; set; }

I'd like the above C# client is generated like this

    [System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "10.3.11.0 (Newtonsoft.Json v11.0.0.0)")]
    public partial class AceReducedDto 
    {
        [Newtonsoft.Json.JsonProperty("id", Required = Newtonsoft.Json.Required.Default, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
        public string id { get; set; }

I've searched a little bit around but I was not able to find any simple solution.

What am I missing?

Thanks for any help anyone can give me.

paulomorgado commented 10 months ago

@alkampfergit, what you are asking for does not conform with the Framework design guidelines, which every tool in the .NET space aims to conform with.

Why would you want that?

alkampfergit commented 10 months ago

I know that the request is strange. I use reflection from generated C# client in a software that uses jint to let the user write JavaScript scripts.

It would be useful to have C# classes that are identical in shape with JavaScript, they would not be idiomatic, but this is really an edge situation. The library we are using generates intelligense for the user based on C# class. With a C# client generated in this way we can work seamlessly between C# and Js.

lahma commented 10 months ago

Jint by default ignores the first character casing and then is case-sensitive for the rest of them. You could probably just generate the intellisense helpers with first character lower-cased.

You could also probably just replace the default property name generator: https://github.com/RicoSuter/NJsonSchema/blob/63ee6152e9819596e9c7158a9ac634a96f73b2e9/src/NJsonSchema.CodeGeneration.CSharp/CSharpGeneratorSettings.cs#L46

alkampfergit commented 10 months ago

Thank y for the suggestion!

paulomorgado commented 10 months ago

Or, since you're already using reflection, use the annotation attributes to extract the JSON names.