Skip to content

Problem with 2-level Inheritance with allOf extensions method #2268

@scavarda

Description

@scavarda

Using Swashbuckle.AspNetCore 6.2.3

Classes definition

using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System.Runtime.Serialization;

using System.Runtime.Serialization;

namespace Pets
{
    /// <summary>
    /// Pet
    /// </summary>
    public class Pet
    {
        /// <summary>
        /// Gets or Sets Name
        /// </summary>
        public string Name { get; set; }
    }

    /// <summary>
    /// A representation of a cat. Note that &#x60;Cat&#x60; will be used as the discriminator value.
    /// </summary>
    public class Cat : Pet
    {
        /// <summary>
        /// The measured skill for hunting
        /// </summary>
        /// <value>The measured skill for hunting</value>
        [JsonConverter(typeof(StringEnumConverter))]
        public enum HuntingSkillEnum
        {
            [EnumMember(Value = "clueless")]
            Clueless = 1,

            [EnumMember(Value = "lazy")]
            Lazy = 2,

            [EnumMember(Value = "adventurous")]
            Adventurous = 3,

            [EnumMember(Value = "aggressive")]
            Aggressive = 4

        }

        /// <summary>
        /// The measured skill for hunting
        /// </summary>
        /// <value>The measured skill for hunting</value>
        public HuntingSkillEnum HuntingSkill { get; set; }
    }

    /// <summary>
    /// A representation of a dog. Note that &#x60;Dog&#x60; will be used as the discriminator value.
    /// </summary>
    [DataContract(Name = "Dog")]
    public class Dog : Pet
    {
        /// <summary>
        /// the size of the pack the dog is from
        /// </summary>
        /// <value>the size of the pack the dog is from</value>
        public int PackSize { get; set; }
    }

    /// <summary>
    /// A representation of a bulldog. Note that &#x60;BullDog&#x60; will be used as the discriminator value.
    /// </summary>
    [DataContract(Name = "BullDog")]
    public class BullDog : Dog
    {
        public bool IsFriendly { get; set; }
    }
}

Service configuration

services.AddSwaggerGen(options =>
{
    options.EnableAnnotations(enableAnnotationsForInheritance: false, enableAnnotationsForPolymorphism: false);
    options.UseAllOfForInheritance();
    options.SelectDiscriminatorNameUsing(_ => "discriminator");
    options.SelectDiscriminatorValueUsing(_ => _.Name);
});

With these classes the openapi document is different from the one proposed at

https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.0.md#models-with-polymorphism-support

My output doc:

{
  "openapi": "3.0.1",
  "paths": {
    ...
  },
  "components": {
    "schemas": {
      "BullDog": {
        "type": "object",
        "allOf": [
          {
            "$ref": "#/components/schemas/Dog"
          }
        ],
        "properties": {
          "isFriendly": {
            "type": "boolean"
          }
        },
        "additionalProperties": false
      },
      "Cat": {
        "type": "object",
        "allOf": [
          {
            "$ref": "#/components/schemas/Pet"
          }
        ],
        "properties": {
          "huntingSkill": {
            "$ref": "#/components/schemas/HuntingSkillEnum"
          }
        },
        "additionalProperties": false
      },
      "Dog": {
        "required": [
          "discriminator"
        ],
        "type": "object",
        "allOf": [
          {
            "$ref": "#/components/schemas/Pet"
          }
        ],
        "properties": {
          "discriminator": {
            "type": "string"
          },
          "packSize": {
            "type": "integer",
            "format": "int32"
          }
        },
        "additionalProperties": false,
        "discriminator": {
          "propertyName": "discriminator",
          "mapping": {
            "Dog": "#/components/schemas/Dog",
            "BullDog": "#/components/schemas/BullDog"
          }
        }
      },
      "HuntingSkillEnum": {
        "enum": [
          "Clueless",
          "Lazy",
          "Adventurous",
          "Aggressive"
        ],
        "type": "string"
      },
      "Pet": {
        "required": [
          "discriminator"
        ],
        "type": "object",
        "properties": {
          "discriminator": {
            "type": "string"
          },
          "name": {
            "type": "string",
            "nullable": true
          }
        },
        "additionalProperties": false,
        "discriminator": {
          "propertyName": "discriminator",
          "mapping": {
            "Pet": "#/components/schemas/Pet",
            "Cat": "#/components/schemas/Cat",
            "Dog": "#/components/schemas/Dog",
            "BullDog": "#/components/schemas/BullDog"
          }
        }
      },
      ...
    }
  }
}

The type , properties and required properties are not inside the allOff property of JSON Schema, but at the same level.

Trying to create autogenerated client with openapi-generator-cli fails.
If I try the version with all inside the allOff property it succeeds.

Metadata

Metadata

Assignees

No one assigned

    Labels

    help-wantedA change up for grabs for contributions from the community

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions