RicoSuter / NSwag

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

Generated CSharp Client contains weird characters #935

Open marcwittke opened 7 years ago

marcwittke commented 7 years ago

Reproduction:

Behavior: The resulting code is syntactically wrong. Line 9816 uses french quotation marks for a generic type definition:

public partial class PaginatedResult«Submission» : System.ComponentModel.INotifyPropertyChanged

Expected Behavior:

    public partial class PaginatedResult<Submission> : System.ComponentModel.INotifyPropertyChanged

Same behavior can be reproduced using the API through .net itself:

SwaggerDocument document = await SwaggerDocument.FromUrlAsync("https://ps-reg-demo.chemaxon.com/RegistryCxn/rest/api/doc");
SwaggerToCSharpClientGeneratorSettings settings = new SwaggerToCSharpClientGeneratorSettings();
SwaggerToCSharpClientGenerator generator = new SwaggerToCSharpClientGenerator(document, settings);
string code = generator.GenerateFile();
RicoSuter commented 7 years ago

They are coming from the names in the swagger spec - you can fix this with an own CSharpGeneratorSettings.TypeNameGenerator and we should probably fix the default one for this scenario:

https://github.com/RSuter/NJsonSchema/blob/master/src/NJsonSchema/DefaultTypeNameGenerator.cs

marcwittke commented 7 years ago

Wow, you're right, didn't came to my mind that this might be valid JSON. However, I've to provide an implementation of the enum name generator as well, I noticed:

    public enum SearchConditionOperator
    {
        [System.Runtime.Serialization.EnumMember(Value = "=")]
        = = 0,

        [System.Runtime.Serialization.EnumMember(Value = "<>")]
        <> = 1,

        [System.Runtime.Serialization.EnumMember(Value = "<")]
        < = 2,

        [System.Runtime.Serialization.EnumMember(Value = ">")]
        > = 3,

CSharp doesn't like these enum members, surprisingly...

RicoSuter commented 7 years ago

Maybe we should enhance the DefaultEnumGenerator too so that no wrong C# or TypeScript code can be generated - there is no reason to not change it as it would always output wrong for specs like yours...

klacol commented 4 years ago

Same problem with the JIRA API. How could this be fixed? Is there possibly a configuration parameter to replace invalid chars from enum values?

borgez commented 4 years ago

I have the same problem. In the class name «

njabulozmnisi commented 4 years ago

I have the same problem. In the class name «

@borgez did you find a way to resolve your issue with « (french quotation mark)?

RicoSuter commented 3 years ago

Needs to be fixed here: https://github.com/RicoSuter/NJsonSchema/blob/master/src/NJsonSchema.CodeGeneration/DefaultEnumNameGenerator.cs

Either by changing the default impl or by providing an own one... Please create a PR with tests.