Skip to content

Restricting Values Assignable to Discriminating Property #8209

Closed
@ahmedalnuaimi

Description

@ahmedalnuaimi

Q&A (please complete the following information)

  • OS: macOS
  • Browser: Chrome
  • Version: 105
  • Method of installation: springdoc-openapi
  • Swagger-UI version: 4.14.0
  • Swagger/OpenAPI version: OpenAPI 3.0.1

Content & configuration

Swagger/OpenAPI definition:

{
  "openapi" : "3.0.1",
  "info" : {
    "title" : "OpenAPI definition",
    "version" : "v0"
  },
  "servers" : [ {
    "url" : "http://sandbox.test.com:8063/api/recs",
    "description" : "Generated server url"
  } ],
  "paths" : {
    "/data" : {
      "get" : {
        "tags" : [ "Data" ],
        "operationId" : "getData",
        "parameters" : [ {
          "name" : "goal",
          "in" : "query",
          "required" : false,
          "schema" : {
            "$ref" : "#/components/schemas/GoalsEnum_User"
          }
        } ],
        "responses" : {
          "404" : {
            "description" : "Not Found",
            "content" : {
              "*/*" : {
                "schema" : {
                  "type" : "object"
                }
              }
            }
          },
          "200" : {
            "description" : "Result generated successfully",
            "content" : {
              "application/json" : {
                "schema" : {
                  "type" : "array",
                  "items" : {
                    "oneOf" : [ {
                      "$ref" : "#/components/schemas/EventDataDto"
                    }, {
                      "$ref" : "#/components/schemas/FreeRideDataDto"
                    }]
                  }
                }
              }
            }
          }
        }
      }
    }
  },
  "components" : {
    "schemas" : {
      "GoalsEnum_User" : {
        "type" : "string",
        "enum" : [ "User1", "User2" ]
      },
      "EventDataDto" : {
        "type" : "object",
        "allOf" : [ {
          "$ref" : "#/components/schemas/ParentDataSchema_UserData"
        }, {
          "type" : "object",
          "properties" : {
            "rules" : {
              "type" : "array",
              "items" : {
                "$ref" : "#/components/schemas/RuleDto"
              }
            }
          }
        }, {
          "$ref" : "#/components/schemas/ParentDataSchema"
        } ]
      },
      "FreeRideDataDto" : {
        "type" : "object",
        "allOf" : [ {
          "$ref" : "#/components/schemas/ParentDataSchema"
        }, {
          "type" : "object",
          "properties" : {
            "completedRoutes" : {
              "type" : "array",
              "items" : {
                "type" : "integer",
                "format" : "int64"
              }
            },
            "averageDistance" : {
              "type" : "number",
              "format" : "double"
            },
            "averageDuration" : {
              "type" : "number",
              "format" : "double"
            }
          }
        }, {
          "$ref" : "#/components/schemas/ParentDataSchema_UserData"
        } ]
      },
      "ParentDataSchema" : {
        "required" : [ "type" ],
        "type" : "object",
        "properties" : {
          "type" : {
            "type" : "string",
            "enum" : [ "FREE_RIDE", "EVENT" ]
          }
        },
        "discriminator" : {
          "propertyName" : "type",
          "mapping" : {
            "EVENT" : "#/components/schemas/EventRecommendationDto",
            "FREE_RIDE" : "#/components/schemas/FreeRideRecommendationDto"
          }
        }
      },
      "ParentDataSchema_UserData" : {
        "required" : [ "type" ],
        "type" : "object",
        "properties" : {
          "type" : {
            "type" : "string",
            "enum" : [ "FREE_RIDE", "EVENT" ]
          }
        },
        "discriminator" : {
          "propertyName" : "type",
          "mapping" : {
            "EVENT" : "#/components/schemas/EventRecommendationDto",
            "FREE_RIDE" : "#/components/schemas/FreeRideRecommendationDto"
          }
        }
      }
    }
  }
}

Screenshots

[
    {
      "type": "FREE_RIDE",
      "rules": [
        {
          "ruleId": 0,
          "categoryId": 0,
          "name": "string",
          "type": "string",
          "value": "string"
        }
      ]
    },
    {
      "type": "FREE_RIDE",
      "completedRoutes": [
        0
      ],
      "averageDistance": 0,
      "averageDuration": 0
    }
]

How can we help?

Since there are specific values for the discriminating field "type", I expect the examples to have the correct value for detected types. Although the types were listed correctly, the type field is not set to the discriminating value.
Is there anything I can do to the Swagger/OpenAPI definitions or Swagger UI to fix this? I'm even open to adding a bug-fix if you can point me to where the values of the field examples are set and how can I choose the discriminating value instead of the first one in the enum instead.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions