NSwag Duplicate objects created

538 Views Asked by At

I use NSwagStudio to create a C# client from an OpenAPI document. So far everything works, but I noticed a problem to which I haven't found a solution yet.

The problem is that NSwagStudio creates duplicate classes for a recurring structure. The following JSON as an example:

{
  "components": {
    "schemas": {
      "Person": {
        "type": "object",
        "properties": {
          "Id": {
            "type": "string"
          },
          "Name": {
            "type": "string"
          },
          "First Name": {
            "type": "string"
          },
          "Lastname": {
            "type": "string"
          },
          "attributes": {
            "type": "object",
            "properties": {
              "type": {
                "type": "string"
              },
              "url": {
                "type": "string"
              }
            }
          }
        },
        "description": "Account Object"
      },
      "Address": {
        "type": "object",
        "properties": {
          "Id": {
            "type": "string"
          },
          "Street": {
            "type": "string"
          },
          "Post Code": {
            "type": "string"
          },
          "City": {
            "type": "string"
          },
          "Country": {
            "type": "string"
          },
          "attributes": {
            "type": "object",
            "properties": {
              "type": {
                "type": "string"
              },
              "url": {
                "type": "string"
              }
            }
          }
        },
        "description": "Address Object"
      }
    }
  }
}

Here are 2 simple objects described, Person and Address. Both objects have this structure in common:

"attributes" : {
            "type" : "object",
            "properties" : {
              "type" : {
                "type" : "string"
              },
              "url" : {
                "type" : "string"
              }
            }
          }

In C#, the result looks like this:

public partial class Attributes

    {
        [Newtonsoft.Json.JsonProperty("type", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
        public string Type { get; set; }

        [Newtonsoft.Json.JsonProperty("url", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
        public string Url { get; set; }

        private System.Collections.Generic.IDictionary<string, object> _additionalProperties = new System.Collections.Generic.Dictionary<string, object>();

        [Newtonsoft.Json.JsonExtensionData]
        public System.Collections.Generic.IDictionary<string, object> AdditionalProperties
        {
            get { return _additionalProperties; }
            set { _additionalProperties = value; }
        }

        public string ToJson()
        {

            return Newtonsoft.Json.JsonConvert.SerializeObject(this, new Newtonsoft.Json.JsonSerializerSettings());

        }
        public static Attributes FromJson(string data)
        {

            return Newtonsoft.Json.JsonConvert.DeserializeObject<Attributes2>(data, new Newtonsoft.Json.JsonSerializerSettings());

        }

    }

public partial class Attributes2
    {
        [Newtonsoft.Json.JsonProperty("type", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
        public string Type { get; set; }

        [Newtonsoft.Json.JsonProperty("url", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
        public string Url { get; set; }

        private System.Collections.Generic.IDictionary<string, object> _additionalProperties = new System.Collections.Generic.Dictionary<string, object>();

        [Newtonsoft.Json.JsonExtensionData]
        public System.Collections.Generic.IDictionary<string, object> AdditionalProperties
        {
            get { return _additionalProperties; }
            set { _additionalProperties = value; }
        }

        public string ToJson()
        {

            return Newtonsoft.Json.JsonConvert.SerializeObject(this, new Newtonsoft.Json.JsonSerializerSettings());

        }
        public static Attributes2 FromJson(string data)
        {

            return Newtonsoft.Json.JsonConvert.DeserializeObject<Attributes2>(data, new Newtonsoft.Json.JsonSerializerSettings());

        }

    }

These attribute classes themselves are just helper classes for other classes. In total over 700 of them were created.

Is it not possible to create only one class for this?

The path part of the OpenAPI document contains $ref references to the "main" object

{
  "/Person": {
    "description": "",
    "get": {
      "responses": {
        "200": {
          "description": "Status Code 200",
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/Person"
              }
            }
          }
        },
        
      }
    }
  }
}

The C# class Person, which is specified by $ref (see above) Here the getter/setter with Attributes372 class.

  public partial class Person
    {
        [Newtonsoft.Json.JsonProperty("Id", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
        public string Id { get; set; }

       
        [Newtonsoft.Json.JsonProperty("Name", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
        public string Name{ get; set; }

       
        [Newtonsoft.Json.JsonProperty("LastName", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
        public string LastName { get; set; }

       
        [Newtonsoft.Json.JsonProperty("FirstName", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
        public string FirstName { get; set; }


        [Newtonsoft.Json.JsonProperty("attributes", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
        public Attributes372 Attributes { get; set; }

        private System.Collections.Generic.IDictionary<string, object> _additionalProperties = new System.Collections.Generic.Dictionary<string, object>();

        [Newtonsoft.Json.JsonExtensionData]
        public System.Collections.Generic.IDictionary<string, object> AdditionalProperties
        {
            get { return _additionalProperties; }
            set { _additionalProperties = value; }
        }

    }

``






0

There are 0 best solutions below