Json2CSharp / Json2CSharpCodeGenerator

Microsoft Reciprocal License
292 stars 81 forks source link

Special Characters in json can cause duplicate properties. #84

Open benborra opened 1 year ago

benborra commented 1 year ago

Hi all

I've got a json that was generated using swagger. The classes that I got returned however contained a duplicate property in them. This because the json contained wild card parameters and a '+' sign which obviously cant build with C#

This is the snippet that contains the issue:

{
    "content": {
        "application/json-patch+json": {
            "schema": {
                "$ref": "#/components/schemas/Inspection"
            }
        },
        "application/json": {
            "schema": {
                "$ref": "#/components/schemas/Inspection"
            }
        },
        "text/json": {
            "schema": {
                "$ref": "#/components/schemas/Inspection"
            }
        },
        "application/*+json": {
            "schema": {
                "$ref": "#/components/schemas/Inspection"
            }
        }
    },
    "required": true
}

Perhaps a number can be appended, however wouldn't expect valid json to convert into invalid C#

Kind regards

Json2CSharp commented 1 year ago

This can resolved by using a dictionnary like so, however not sure why its not detecting the ref property and its always null, so we can implement or an option to detect dictionaries when property names don't match c# like property names public class Root { public Dictionary<string, Schema> content { get; set; } public bool required { get; set; } }

    public class Schema
    {
        [JsonProperty("#ref")]
        public string Ref { get; set; }
    }