Skip to content

Commit

Permalink
update with different approach
Browse files Browse the repository at this point in the history
  • Loading branch information
reversearrow committed Sep 23, 2024
1 parent 2e539ed commit b0f93cc
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
1 change: 1 addition & 0 deletions openapi2/issues1010_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,5 @@ func TestIssue1010(t *testing.T) {
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"`
}

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

0 comments on commit b0f93cc

Please sign in to comment.