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

fix: issue unmarshalling when discriminator field is set in openapi2.0 #1011

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 98 additions & 0 deletions openapi2/issues1010_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package openapi2

import (
"encoding/json"
"testing"

"github.com/stretchr/testify/require"
)

func TestIssue1010(t *testing.T) {
v2 := []byte(`
{
"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"
}
}
}
]
}
}
}
`)

var doc2 T
err := json.Unmarshal(v2, &doc2)
require.NoError(t, err)
require.Equal(t, "petType", doc2.Definitions["Pet"].Value.Discriminator.PropertyName)
}
36 changes: 33 additions & 3 deletions openapi3/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -404,12 +404,42 @@ func (schema Schema) MarshalYAML() (any, error) {

// UnmarshalJSON sets Schema to a copy of data.
func (schema *Schema) UnmarshalJSON(data []byte) error {
type SchemaBis Schema
type Alias Schema
type SchemaBis struct {
Alias
Discriminator json.RawMessage `json:"discriminator,omitempty" yaml:"discriminator,omitempty"`
}

var x SchemaBis
if err := json.Unmarshal(data, &x); err != nil {
return unmarshalError(err)
}
_ = json.Unmarshal(data, &x.Extensions)

type DiscriminatorObject struct {
Discriminator *Discriminator `json:"discriminator,omitempty" yaml:"discriminator,omitempty"`
}

if len(x.Discriminator) > 0 {
var ds *string
if err := json.Unmarshal(x.Discriminator, &ds); err == nil {
// In OpenAPI 2, the Discriminator is a string field, while in OpenAPI 3,
// it corresponds to the Discriminator.PropertyName field.
// If the OpenAPI 2 Discriminator (ds) is not nil, we assign it to
// the OpenAPI 3 equivalent, which is Discriminator.PropertyName.
if ds != nil {
x.Alias.Discriminator = &Discriminator{
PropertyName: *ds,
}
}
} else {
var do DiscriminatorObject
if err := json.Unmarshal(x.Discriminator, &do.Discriminator); err == nil && do.Discriminator != nil {
x.Alias.Discriminator = do.Discriminator
}
}
}

_ = json.Unmarshal(data, &x.Alias.Extensions)

delete(x.Extensions, "oneOf")
delete(x.Extensions, "anyOf")
Expand Down Expand Up @@ -464,7 +494,7 @@ func (schema *Schema) UnmarshalJSON(data []byte) error {
x.Extensions = nil
}

*schema = Schema(x)
*schema = Schema(x.Alias)

if schema.Format == "date" {
// This is a fix for: https://github.com/getkin/kin-openapi/issues/697
Expand Down
Loading