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

OpenAPI 2.0: Unable to Unmarshal Spec with Discriminator Field Set #1010

Open
reversearrow opened this issue Sep 17, 2024 · 0 comments · May be fixed by #1011
Open

OpenAPI 2.0: Unable to Unmarshal Spec with Discriminator Field Set #1010

reversearrow opened this issue Sep 17, 2024 · 0 comments · May be fixed by #1011

Comments

@reversearrow
Copy link
Contributor

OpenAPI 2.0 supports the discriminator field, but when the discriminator is included in an OpenAPI 2.0 document, unmarshaling the document fails.

OpenAPI 2.0 the discriminator field is set using string, whereas in OpenAPI 3.0 and later, the discriminator is represented as an object.

The unmarshaling process fails because the schema struct used for parsing expects the discriminator field to be an object, which is valid in OpenAPI 3.0 but not in OpenAPI 2.0.

This schema struct is shared between the OpenAPI v2.0 and v3.0+.

The solution is to update the type of the discriminator to interface{} (or any) and cast it based on the OpenAPI version, ensuring compatibility between versions.

Example Payload with discriminator field set

{
  "basePath": "/v2",
  "host": "test.example.com",
  "info": {
    "title": "MyAPI",
    "version": "0.1",
    "x-info": "info extension"
  },
  "paths": {
    "/foo": {
      "get": {
        "operationId": "getFoo",
        "responses": {
          "200": {
            "description": "returns all information",
            "schema": {
              "$ref": "#/definitions/Pet"
            }
          },
          "default": {
            "description": "OK"
          }
        },
        "summary": "get foo"
      }
    }
  },
  "schemes": [
    "http"
  ],
  "swagger": "2.0",
  "definitions": {
    "Pet": {
      "type": "object",
      "required": [
        "petType"
      ],
      "properties": {
        "petType": {
          "type": "string"
        },
        "name": {
          "type": "string"
        },
        "age": {
          "type": "integer"
        }
      },
      "discriminator": "petType"
    },
    "Dog": {
      "allOf": [
        {
          "$ref": "#/definitions/Pet"
        },
        {
          "type": "object",
          "properties": {
            "breed": {
              "type": "string"
            }
          }
        }
      ]
    },
    "Cat": {
      "allOf": [
        {
          "$ref": "#/definitions/Pet"
        },
        {
          "type": "object",
          "properties": {
            "color": {
              "type": "string"
            }
          }
        }
      ]
    }
  }
}

Response

json: cannot unmarshal string into field Schema.discriminator of type openapi3.Discriminator
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant