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

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

Closed
scavarda opened this issue Nov 8, 2021 · 3 comments · Fixed by #2815
Closed

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

scavarda opened this issue Nov 8, 2021 · 3 comments · Fixed by #2815
Labels
help-wanted A change up for grabs for contributions from the community

Comments

@scavarda
Copy link

scavarda commented Nov 8, 2021

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.

@bkoelman
Copy link
Contributor

@scavarda Thanks for reporting this. I ran into the same issue and patched up SchemaGenerator here. I didn't bother to create a PR because Swashbuckle seems to be abandoned, but feel free to do so.

@martincostello martincostello added the help-wanted A change up for grabs for contributions from the community label Apr 14, 2024
@martincostello
Copy link
Collaborator

We'd be grateful for a PR to resolve this issue.

@bkoelman
Copy link
Contributor

@martincostello Created #2815.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help-wanted A change up for grabs for contributions from the community
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants