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

Revert "openapi3: process discriminator mapping values as refs" #1029

Merged
merged 1 commit into from
Nov 8, 2024
Merged
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
25 changes: 8 additions & 17 deletions .github/docs/openapi3.txt
Original file line number Diff line number Diff line change
Expand Up @@ -411,8 +411,8 @@ type Discriminator struct {
Extensions map[string]any `json:"-" yaml:"-"`
Origin *Origin `json:"origin,omitempty" yaml:"origin,omitempty"`

PropertyName string `json:"propertyName" yaml:"propertyName"` // required
Mapping StringMap[MappingRef] `json:"mapping,omitempty" yaml:"mapping,omitempty"`
PropertyName string `json:"propertyName" yaml:"propertyName"` // required
Mapping StringMap `json:"mapping,omitempty" yaml:"mapping,omitempty"`
}
Discriminator is specified by OpenAPI/Swagger standard version 3. See
https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#discriminator-object
Expand Down Expand Up @@ -831,15 +831,6 @@ type Location struct {
}
Location is a struct that contains the location of a field.

type MappingRef SchemaRef
MappingRef is a ref to a Schema objects. Unlike SchemaRefs it is serialised
as a plain string instead of an object with a $ref key, as such it also does
not support extensions.

func (mr MappingRef) MarshalText() ([]byte, error)

func (mr *MappingRef) UnmarshalText(data []byte) error

type MediaType struct {
Extensions map[string]any `json:"-" yaml:"-"`
Origin *Origin `json:"origin,omitempty" yaml:"origin,omitempty"`
Expand Down Expand Up @@ -922,10 +913,10 @@ type OAuthFlow struct {
Extensions map[string]any `json:"-" yaml:"-"`
Origin *Origin `json:"origin,omitempty" yaml:"origin,omitempty"`

AuthorizationURL string `json:"authorizationUrl,omitempty" yaml:"authorizationUrl,omitempty"`
TokenURL string `json:"tokenUrl,omitempty" yaml:"tokenUrl,omitempty"`
RefreshURL string `json:"refreshUrl,omitempty" yaml:"refreshUrl,omitempty"`
Scopes StringMap[string] `json:"scopes" yaml:"scopes"` // required
AuthorizationURL string `json:"authorizationUrl,omitempty" yaml:"authorizationUrl,omitempty"`
TokenURL string `json:"tokenUrl,omitempty" yaml:"tokenUrl,omitempty"`
RefreshURL string `json:"refreshUrl,omitempty" yaml:"refreshUrl,omitempty"`
Scopes StringMap `json:"scopes" yaml:"scopes"` // required
}
OAuthFlow is specified by OpenAPI/Swagger standard version 3. See
https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#oauth-flow-object
Expand Down Expand Up @@ -2076,11 +2067,11 @@ func NewRegexpFormatValidator(pattern string) StringFormatValidator
NewRegexpFormatValidator creates a new FormatValidator that uses a regular
expression to validate the value.

type StringMap[V any] map[string]V
type StringMap map[string]string
StringMap is a map[string]string that ignores the origin in the underlying
json representation.

func (stringMap *StringMap[V]) UnmarshalJSON(data []byte) (err error)
func (stringMap *StringMap) UnmarshalJSON(data []byte) (err error)
UnmarshalJSON sets StringMap to a copy of data.

type T struct {
Expand Down
18 changes: 2 additions & 16 deletions openapi3/discriminator.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,8 @@ type Discriminator struct {
Extensions map[string]any `json:"-" yaml:"-"`
Origin *Origin `json:"origin,omitempty" yaml:"origin,omitempty"`

PropertyName string `json:"propertyName" yaml:"propertyName"` // required
Mapping StringMap[MappingRef] `json:"mapping,omitempty" yaml:"mapping,omitempty"`
}

// MappingRef is a ref to a Schema objects. Unlike SchemaRefs it is serialised
// as a plain string instead of an object with a $ref key, as such it also does
// not support extensions.
type MappingRef SchemaRef

func (mr *MappingRef) UnmarshalText(data []byte) error {
mr.Ref = string(data)
return nil
}

func (mr MappingRef) MarshalText() ([]byte, error) {
return []byte(mr.Ref), nil
PropertyName string `json:"propertyName" yaml:"propertyName"` // required
Mapping StringMap `json:"mapping,omitempty" yaml:"mapping,omitempty"`
}

// MarshalJSON returns the JSON encoding of Discriminator.
Expand Down
10 changes: 0 additions & 10 deletions openapi3/internalize_refs.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,16 +351,6 @@ func (doc *T) derefSchema(s *Schema, refNameResolver RefNameResolver, parentIsEx
}
}
}
// Discriminator mapping values are special cases since they are not full
// ref objects but are string references to schema objects.
if s.Discriminator != nil && s.Discriminator.Mapping != nil {
for k, mapRef := range s.Discriminator.Mapping {
s2 := (*SchemaRef)(&mapRef)
isExternal := doc.addSchemaToSpec(s2, refNameResolver, parentIsExternal)
doc.derefSchema(s2.Value, refNameResolver, isExternal || parentIsExternal)
s.Discriminator.Mapping[k] = mapRef
}
}

for _, name := range componentNames(s.Properties) {
s2 := s.Properties[name]
Expand Down
1 change: 0 additions & 1 deletion openapi3/internalize_refs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ func TestInternalizeRefs(t *testing.T) {
{"testdata/issue831/testref.internalizepath.openapi.yml"},
{"testdata/issue959/openapi.yml"},
{"testdata/interalizationNameCollision/api.yml"},
{"testdata/discriminator.yml"},
}

for _, test := range tests {
Expand Down
10 changes: 0 additions & 10 deletions openapi3/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -953,16 +953,6 @@ func (loader *Loader) resolveSchemaRef(doc *T, component *SchemaRef, documentPat
return err
}
}
// Discriminator mapping refs are a special case since they are not full
// ref objects but are plain strings that reference schema objects.
if value.Discriminator != nil && value.Discriminator.Mapping != nil {
for k, v := range value.Discriminator.Mapping {
if err := loader.resolveSchemaRef(doc, (*SchemaRef)(&v), documentPath, visited); err != nil {
return err
}
value.Discriminator.Mapping[k] = v
}
}
return nil
}

Expand Down
4 changes: 2 additions & 2 deletions openapi3/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -1298,7 +1298,7 @@ func (schema *Schema) visitNotOperation(settings *schemaValidationSettings, valu
func (schema *Schema) visitXOFOperations(settings *schemaValidationSettings, value any) (err error, run bool) {
var visitedOneOf, visitedAnyOf, visitedAllOf bool
if v := schema.OneOf; len(v) > 0 {
var discriminatorRef MappingRef
var discriminatorRef string
if schema.Discriminator != nil {
pn := schema.Discriminator.PropertyName
if valuemap, okcheck := value.(map[string]any); okcheck {
Expand Down Expand Up @@ -1344,7 +1344,7 @@ func (schema *Schema) visitXOFOperations(settings *schemaValidationSettings, val
return foundUnresolvedRef(item.Ref), false
}

if discriminatorRef.Ref != "" && discriminatorRef.Ref != item.Ref {
if discriminatorRef != "" && discriminatorRef != item.Ref {
continue
}

Expand Down
8 changes: 4 additions & 4 deletions openapi3/security_scheme.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,10 +322,10 @@ type OAuthFlow struct {
Extensions map[string]any `json:"-" yaml:"-"`
Origin *Origin `json:"origin,omitempty" yaml:"origin,omitempty"`

AuthorizationURL string `json:"authorizationUrl,omitempty" yaml:"authorizationUrl,omitempty"`
TokenURL string `json:"tokenUrl,omitempty" yaml:"tokenUrl,omitempty"`
RefreshURL string `json:"refreshUrl,omitempty" yaml:"refreshUrl,omitempty"`
Scopes StringMap[string] `json:"scopes" yaml:"scopes"` // required
AuthorizationURL string `json:"authorizationUrl,omitempty" yaml:"authorizationUrl,omitempty"`
TokenURL string `json:"tokenUrl,omitempty" yaml:"tokenUrl,omitempty"`
RefreshURL string `json:"refreshUrl,omitempty" yaml:"refreshUrl,omitempty"`
Scopes StringMap `json:"scopes" yaml:"scopes"` // required
}

// MarshalJSON returns the JSON encoding of OAuthFlow.
Expand Down
6 changes: 3 additions & 3 deletions openapi3/stringmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package openapi3
import "encoding/json"

// StringMap is a map[string]string that ignores the origin in the underlying json representation.
type StringMap[V any] map[string]V
type StringMap map[string]string

// UnmarshalJSON sets StringMap to a copy of data.
func (stringMap *StringMap[V]) UnmarshalJSON(data []byte) (err error) {
*stringMap, _, err = unmarshalStringMap[V](data)
func (stringMap *StringMap) UnmarshalJSON(data []byte) (err error) {
*stringMap, _, err = unmarshalStringMap[string](data)
return
}

Expand Down
24 changes: 0 additions & 24 deletions openapi3/testdata/discriminator.yml

This file was deleted.

75 changes: 0 additions & 75 deletions openapi3/testdata/discriminator.yml.internalized.yml

This file was deleted.

17 changes: 0 additions & 17 deletions openapi3/testdata/ext.yml

This file was deleted.

Loading