0

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

Generated Example:

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

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.

0 Answers0