Skip to content
This repository has been archived by the owner on Sep 2, 2023. It is now read-only.

Invalid swagger.json generated for JSON SubTypes (Inheritance) #200

Open
swurzinger opened this issue Aug 18, 2020 · 0 comments
Open

Invalid swagger.json generated for JSON SubTypes (Inheritance) #200

swurzinger opened this issue Aug 18, 2020 · 0 comments

Comments

@swurzinger
Copy link
Contributor

The swagger specification (both V2 and V3) mentions:

To support polymorphism, Swagger adds the support of the discriminator field. When used, the discriminator will be the name of the property used to decide which schema definition is used to validate the structure of the model. As such, the discriminator field MUST be a required field.

Although the swagger-gradle-plugin generates a discriminator field in the swagger.json the property field is missing (and not required).

The documentation expresses this requirement even clearer (OA3, but equally applies to Swagger V2):

In our example, the discriminator points to the objectType property that contains the data type name. The discriminator is used with anyOf or oneOf keywords only. It is important that all the models mentioned below anyOf or oneOf contain the property that the discriminator specifies. This means, for example, that in our code above, both simpleObject and complexObject must have the objectType property. This property is required in these schemas.

Example (taken from the tests):

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "__innerType")
@JsonSubTypes(
    @JsonSubTypes.Type(value = SubInnerJsonSubType.class, name = "SubInnerJsonSubType")
)
public class InnerJsonSubType {
    private String value;
}

public class SubInnerJsonSubType extends InnerJsonSubType {
    private String subValue;
}

Currently swagger-gradle-plugin generated the following swagger-file for the classes above:

{
  "definitions" : {
    "InnerJsonSubType" : {
      "type" : "object",
      "discriminator" : "__innerType",
      "properties" : {
        "value" : {
          "type" : "string"
        }
      }
    },
    "SubInnerJsonSubType" : {
      "allOf" : [ {
        "$ref" : "#/definitions/InnerJsonSubType"
      }, {
        "type" : "object",
        "properties" : {
          "subValue" : {
            "type" : "string"
          }
        }
      } ]
    }
  }
}

However, despite being referenced by the discriminator field the __innerType field does not exist. The expected generated output would be this:

{
  "definitions" : {
    "InnerJsonSubType" : {
      "type" : "object",
      "discriminator" : "__innerType",
      "required" : [ "__innerType" ],
      "properties" : {
        "value" : {
          "type" : "string"
        },
        "__innerType" : {
          "type" : "string"
        }
      }
    },
    "SubInnerJsonSubType" : {
      "allOf" : [ {
        "$ref" : "#/definitions/InnerJsonSubType"
      }, {
        "type" : "object",
        "properties" : {
          "subValue" : {
            "type" : "string"
          }
        }
      } ]
    }
  }
}

note the __innerType property and the required properties list.

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

No branches or pull requests

1 participant