Skip to content

Polymorphic types are not created when no properties are defined #1836

Description

@dnperfors

We generated a model from an openapi specification that contains the following model:

    Derde:
      type: object
      required:
        - type
      properties:
        type:
          type: string
      discriminator:
        propertyName: type
        mapping:
          BekendeDerde: '#/components/schemas/BekendeDerde'
          OnbekendeDerde: '#/components/schemas/OnbekendeDerde'
    BekendeDerde:
      allOf:
        - $ref: '#/components/schemas/Derde'
        - type: object
          properties:
            burgerservicenummer:
              $ref: '#/components/schemas/Burgerservicenummer'
            naam:
              $ref: '#/components/schemas/NaamVolledigeNaam'
    OnbekendeDerde:
      allOf:
        - $ref: '#/components/schemas/Derde'

(Sorry, it is dutch, but the names themselves are not really important)

When generating this using NSwagStudio, we get 2 type: Derde (the base class) and BekendeDerde. OnbekendeDerde (which is part of the mapping) is not generated which leads to issues when we get a OnbekendeDerde in the data.

I reproduced this with a test as well (in NSwag generation test project) and stepping through the code it looks like the CSharpTypeResolver in NJsonSchema doesn't generate the type.

        [Fact]
        public async Task When_generating_csharp_with_single_allOf_inheritance_then_inheritance_is_generated()
        {
            // Arrange
            var json = @"{
    ""openapi"": ""3.0.0"",
    ""components"": {
        ""schemas"": {
            ""SessionStateResent"": {
                ""allOf"": [
                    {
                        ""$ref"": ""#/components/schemas/SportsbookEventBody""
                    }
                ]
            },
            ""SportsbookEventBody"": {
                ""type"": ""object"",
                ""additionalProperties"": false
            }
        }
    }
}";
            var document = await OpenApiDocument.FromJsonAsync(json);

            // Act
            var settings = new CSharpClientGeneratorSettings();
            var generator = new CSharpClientGenerator(document, settings);
            var code = generator.GenerateFile();

            // Assert
            Assert.Contains("class SessionStateResent : SportsbookEventBody", code);
        }

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions