Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Toby-Lawrance opened this issue Sep 15, 2024 · 0 comments

Comments

@Toby-Lawrance
Copy link

Toby-Lawrance commented Sep 15, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant