Closed
Description
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
Metadata
Metadata
Assignees
Labels
No labels