Skip to content

JsonSchemaFlattenAttribute generates duplicate fields when multiple inheritance levels #1870

@killergege

Description

@killergege

I have a controller that returns a type with more than 1 level of inheritance, for instance :

[HttpGet("test")]
public Task<Test> Test()
{
    return Task.FromResult(new Test());
}

[JsonSchemaFlatten]
public class Test : Test2
{
    public string? Name { get; init; }
}

public class Test2 : Test1
{
    public string? FirstName { get; init; }
}

public class Test1
{
    public string? LastName { get; init; }
}

With only one level (I removed the Test1 inheritance), the output is what you'd expect, a properly flattened object:

      "Test": {
        "type": "object",
        "additionalProperties": false,
        "properties": {
          "firstName": {
            "type": "string",
            "nullable": true
          },
          "name": {
            "type": "string",
            "nullable": true
          }
        }
      },

With one level, without Flatten, it looks okay too :

      "Test": {
        "allOf": [
          {
            "$ref": "#/components/schemas/Test2"
          },
          {
            "type": "object",
            "additionalProperties": false,
            "properties": {
              "name": {
                "type": "string",
                "nullable": true
              }
            }
          }
        ]
      },
      "Test2": {
        "type": "object",
        "additionalProperties": false,
        "properties": {
          "firstName": {
            "type": "string",
            "nullable": true
          }
        }
      },

But with Flatten and multiple levels, the firstName field is duplicated in the object + in the allOf section.

   "Test": {
        "type": "object",
        "additionalProperties": false,
        "properties": {
          "firstName": {
            "type": "string",
            "nullable": true
          },
          "name": {
            "type": "string",
            "nullable": true
          }
        },
        "allOf": [
          {
            "$ref": "#/components/schemas/Test1"
          },
          {
            "additionalProperties": false,
            "properties": {
              "firstName": {
                "type": "string",
                "nullable": true
              }
            }
          }
        ]
      },
      "Test1": {
        "type": "object",
        "additionalProperties": false,
        "properties": {
          "lastName": {
            "type": "string",
            "nullable": true
          }
        }
      }

I don't know well the OpenApi schema but it looks strange.

This also causes an issue when using the ISchemaProcessor when accessing context.Schema.ActualProperties as it throws an InvalidOperationException when reading ActualProperties : System.InvalidOperationException: 'The properties 'firstName' are defined multiple times.'

Is it a bug ?
Is there a workaround ?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions