RicoSuter/NSwag

C# Client generates property name instead of referenced Schema when used in anyOf

Opened this issue · 0 comments

When generating using a schema that makes use of anyOf and null. The referenced schema in the anyOf is ignored and instead a class by the name of the property is generated. Naturally this breaks with almost 100% of use-cases.
Below is the minimum spec I could design to generate this issue:

{
	"openapi": "3.1.0",
	"paths": {
		"/example": {
			"get": {
				"responses": {
					"200": {
						"content": {
							"application/json": {
								"schema": {
									"$ref": "#/components/schemas/SchemaBar"
								}
							}
						}
					}
				}
			}
		}
	},
	"components": {
		"schemas": {
		"SchemaFoo": {
			"properties": {
			  "name": {
				"type": "string",
				"title": "name",
				"description": "ExampleName"
			  }
			},
			"type": "object",
			"required": [
			  "name"
			],
			"title": "SchemaFoo"
		},
		"SchemaBar": {
			"properties": {
			  "content": {
				"anyOf": [
				  {
					"$ref": "#/components/schemas/SchemaFoo"
				  },
				  {
					"type": "null"
				  }
				],
				"description": "AnyOf Content between existing Schema and Null"
			  }
			},
			"type": "object",
			"required": [
			  "content"
			],
			"title": "SchemaBar"
		  }
		}
	}
}

The offending section of generated client.

    [System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "14.1.0.0 (NJsonSchema v11.0.2.0 (Newtonsoft.Json v13.0.0.0))")]
    public partial class SchemaFoo
    {
        /// <summary>
        /// ExampleName
        /// </summary>
        [Newtonsoft.Json.JsonProperty("name", Required = Newtonsoft.Json.Required.Always)]
        [System.ComponentModel.DataAnnotations.Required(AllowEmptyStrings = true)]
        public string Name { get; set; }

        private System.Collections.Generic.IDictionary<string, object> _additionalProperties;

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

    }

    [System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "14.1.0.0 (NJsonSchema v11.0.2.0 (Newtonsoft.Json v13.0.0.0))")]
    public partial class SchemaBar
    {
        /// <summary>
        /// AnyOf Content between existing Schema and Null
        /// </summary>
        [Newtonsoft.Json.JsonProperty("content", Required = Newtonsoft.Json.Required.Always)]
        public Content Content { get; set; }

        private System.Collections.Generic.IDictionary<string, object> _additionalProperties;

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

    }

Easiest solution to this when the spec can be changed is to replace anyOf with oneOf where it appears to interpret correctly.