Forum Discussion

ahmedalnuaimi's avatar
ahmedalnuaimi
New Member
3 years ago

Restricting Values Assignable to Discriminating Property

  • Swagger/OpenAPI version: OpenAPI 3.0.1

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"
          }
        }
      }
    }
  }
}

How can we help?

Since there are specific values for the discriminating field "type", I generated examples and clients 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 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.

No RepliesBe the first to reply