Skip to content

Commit

Permalink
revert original approach
Browse files Browse the repository at this point in the history
  • Loading branch information
reversearrow committed Sep 23, 2024
1 parent 787fc8f commit 2e539ed
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 42 deletions.
2 changes: 1 addition & 1 deletion .github/docs/openapi3.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1524,7 +1524,7 @@ type Schema struct {
MinProps uint64 `json:"minProperties,omitempty" yaml:"minProperties,omitempty"`
MaxProps *uint64 `json:"maxProperties,omitempty" yaml:"maxProperties,omitempty"`
AdditionalProperties AdditionalProperties `json:"additionalProperties,omitempty" yaml:"additionalProperties,omitempty"`
Discriminator any `json:"discriminator,omitempty" yaml:"discriminator,omitempty"`
Discriminator *Discriminator `json:"discriminator,omitempty" yaml:"discriminator,omitempty"`
}
Schema is specified by OpenAPI/Swagger 3.0 standard. See
https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#schema-object
Expand Down
13 changes: 1 addition & 12 deletions openapi3/discriminator_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package openapi3

import (
"encoding/json"
"testing"

"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -53,15 +52,5 @@ func TestParsingDiscriminator(t *testing.T) {
err = doc.Validate(loader.Context)
require.NoError(t, err)

discriminatorMap, ok := doc.Components.Schemas["MyResponseType"].Value.Discriminator.(map[string]interface{})
require.True(t, ok)

marshaledDiscriminator, err := json.Marshal(discriminatorMap)
require.NoError(t, err)

var discriminator *Discriminator
err = json.Unmarshal(marshaledDiscriminator, &discriminator)
require.NoError(t, err)

require.Len(t, discriminator.Mapping, 2)
require.Len(t, doc.Components.Schemas["MyResponseType"].Value.Discriminator.Mapping, 2)
}
32 changes: 3 additions & 29 deletions openapi3/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ type Schema struct {
MinProps uint64 `json:"minProperties,omitempty" yaml:"minProperties,omitempty"`
MaxProps *uint64 `json:"maxProperties,omitempty" yaml:"maxProperties,omitempty"`
AdditionalProperties AdditionalProperties `json:"additionalProperties,omitempty" yaml:"additionalProperties,omitempty"`
Discriminator any `json:"discriminator,omitempty" yaml:"discriminator,omitempty"`
Discriminator *Discriminator `json:"discriminator,omitempty" yaml:"discriminator,omitempty"`
}

type Types []string
Expand Down Expand Up @@ -1298,33 +1298,7 @@ func (schema *Schema) visitXOFOperations(settings *schemaValidationSettings, val
if v := schema.OneOf; len(v) > 0 {
var discriminatorRef string
if schema.Discriminator != nil {
var discriminator *Discriminator
if descriminatorValuemap, okcheck := schema.Discriminator.(map[string]any); okcheck {
marshaledDiscriminator, err := json.Marshal(descriminatorValuemap)
if err != nil {
return &SchemaError{
Schema: schema,
SchemaField: "discriminator",
Reason: fmt.Sprintf("unable to marshal the discriminator field in schema: %v", err),
}, false
}

if err := json.Unmarshal(marshaledDiscriminator, &discriminator); err != nil {
return &SchemaError{
Schema: schema,
SchemaField: "discriminator",
Reason: fmt.Sprintf("unable to unmarshall the discriminator field in schema: %v", err),
}, false
}
} else {
return &SchemaError{
Schema: schema,
SchemaField: "discriminator",
Reason: fmt.Sprintf("discriminator is expected to be an object, but received an unknown type"),
}, false
}

pn := discriminator.PropertyName
pn := schema.Discriminator.PropertyName
if valuemap, okcheck := value.(map[string]any); okcheck {
discriminatorVal, okcheck := valuemap[pn]
if !okcheck {
Expand All @@ -1345,7 +1319,7 @@ func (schema *Schema) visitXOFOperations(settings *schemaValidationSettings, val
}, false
}

if discriminatorRef, okcheck = discriminator.Mapping[discriminatorValString]; len(discriminator.Mapping) > 0 && !okcheck {
if discriminatorRef, okcheck = schema.Discriminator.Mapping[discriminatorValString]; len(schema.Discriminator.Mapping) > 0 && !okcheck {
return &SchemaError{
Value: discriminatorVal,
Schema: schema,
Expand Down

0 comments on commit 2e539ed

Please sign in to comment.