Skip to content

Commit f44ed5a

Browse files
authored
Revert "openapi3: process discriminator mapping values as refs (#1022)"
This reverts commit 4d76e26.
1 parent c41a068 commit f44ed5a

File tree

11 files changed

+19
-179
lines changed

11 files changed

+19
-179
lines changed

.github/docs/openapi3.txt

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -411,8 +411,8 @@ type Discriminator struct {
411411
Extensions map[string]any `json:"-" yaml:"-"`
412412
Origin *Origin `json:"origin,omitempty" yaml:"origin,omitempty"`
413413

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

834-
type MappingRef SchemaRef
835-
MappingRef is a ref to a Schema objects. Unlike SchemaRefs it is serialised
836-
as a plain string instead of an object with a $ref key, as such it also does
837-
not support extensions.
838-
839-
func (mr MappingRef) MarshalText() ([]byte, error)
840-
841-
func (mr *MappingRef) UnmarshalText(data []byte) error
842-
843834
type MediaType struct {
844835
Extensions map[string]any `json:"-" yaml:"-"`
845836
Origin *Origin `json:"origin,omitempty" yaml:"origin,omitempty"`
@@ -922,10 +913,10 @@ type OAuthFlow struct {
922913
Extensions map[string]any `json:"-" yaml:"-"`
923914
Origin *Origin `json:"origin,omitempty" yaml:"origin,omitempty"`
924915

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

2079-
type StringMap[V any] map[string]V
2070+
type StringMap map[string]string
20802071
StringMap is a map[string]string that ignores the origin in the underlying
20812072
json representation.
20822073

2083-
func (stringMap *StringMap[V]) UnmarshalJSON(data []byte) (err error)
2074+
func (stringMap *StringMap) UnmarshalJSON(data []byte) (err error)
20842075
UnmarshalJSON sets StringMap to a copy of data.
20852076

20862077
type T struct {

openapi3/discriminator.go

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,8 @@ type Discriminator struct {
1111
Extensions map[string]any `json:"-" yaml:"-"`
1212
Origin *Origin `json:"origin,omitempty" yaml:"origin,omitempty"`
1313

14-
PropertyName string `json:"propertyName" yaml:"propertyName"` // required
15-
Mapping StringMap[MappingRef] `json:"mapping,omitempty" yaml:"mapping,omitempty"`
16-
}
17-
18-
// MappingRef is a ref to a Schema objects. Unlike SchemaRefs it is serialised
19-
// as a plain string instead of an object with a $ref key, as such it also does
20-
// not support extensions.
21-
type MappingRef SchemaRef
22-
23-
func (mr *MappingRef) UnmarshalText(data []byte) error {
24-
mr.Ref = string(data)
25-
return nil
26-
}
27-
28-
func (mr MappingRef) MarshalText() ([]byte, error) {
29-
return []byte(mr.Ref), nil
14+
PropertyName string `json:"propertyName" yaml:"propertyName"` // required
15+
Mapping StringMap `json:"mapping,omitempty" yaml:"mapping,omitempty"`
3016
}
3117

3218
// MarshalJSON returns the JSON encoding of Discriminator.

openapi3/internalize_refs.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -351,16 +351,6 @@ func (doc *T) derefSchema(s *Schema, refNameResolver RefNameResolver, parentIsEx
351351
}
352352
}
353353
}
354-
// Discriminator mapping values are special cases since they are not full
355-
// ref objects but are string references to schema objects.
356-
if s.Discriminator != nil && s.Discriminator.Mapping != nil {
357-
for k, mapRef := range s.Discriminator.Mapping {
358-
s2 := (*SchemaRef)(&mapRef)
359-
isExternal := doc.addSchemaToSpec(s2, refNameResolver, parentIsExternal)
360-
doc.derefSchema(s2.Value, refNameResolver, isExternal || parentIsExternal)
361-
s.Discriminator.Mapping[k] = mapRef
362-
}
363-
}
364354

365355
for _, name := range componentNames(s.Properties) {
366356
s2 := s.Properties[name]

openapi3/internalize_refs_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ func TestInternalizeRefs(t *testing.T) {
2525
{"testdata/issue831/testref.internalizepath.openapi.yml"},
2626
{"testdata/issue959/openapi.yml"},
2727
{"testdata/interalizationNameCollision/api.yml"},
28-
{"testdata/discriminator.yml"},
2928
}
3029

3130
for _, test := range tests {

openapi3/loader.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -953,16 +953,6 @@ func (loader *Loader) resolveSchemaRef(doc *T, component *SchemaRef, documentPat
953953
return err
954954
}
955955
}
956-
// Discriminator mapping refs are a special case since they are not full
957-
// ref objects but are plain strings that reference schema objects.
958-
if value.Discriminator != nil && value.Discriminator.Mapping != nil {
959-
for k, v := range value.Discriminator.Mapping {
960-
if err := loader.resolveSchemaRef(doc, (*SchemaRef)(&v), documentPath, visited); err != nil {
961-
return err
962-
}
963-
value.Discriminator.Mapping[k] = v
964-
}
965-
}
966956
return nil
967957
}
968958

openapi3/schema.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1298,7 +1298,7 @@ func (schema *Schema) visitNotOperation(settings *schemaValidationSettings, valu
12981298
func (schema *Schema) visitXOFOperations(settings *schemaValidationSettings, value any) (err error, run bool) {
12991299
var visitedOneOf, visitedAnyOf, visitedAllOf bool
13001300
if v := schema.OneOf; len(v) > 0 {
1301-
var discriminatorRef MappingRef
1301+
var discriminatorRef string
13021302
if schema.Discriminator != nil {
13031303
pn := schema.Discriminator.PropertyName
13041304
if valuemap, okcheck := value.(map[string]any); okcheck {
@@ -1344,7 +1344,7 @@ func (schema *Schema) visitXOFOperations(settings *schemaValidationSettings, val
13441344
return foundUnresolvedRef(item.Ref), false
13451345
}
13461346

1347-
if discriminatorRef.Ref != "" && discriminatorRef.Ref != item.Ref {
1347+
if discriminatorRef != "" && discriminatorRef != item.Ref {
13481348
continue
13491349
}
13501350

openapi3/security_scheme.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -322,10 +322,10 @@ type OAuthFlow struct {
322322
Extensions map[string]any `json:"-" yaml:"-"`
323323
Origin *Origin `json:"origin,omitempty" yaml:"origin,omitempty"`
324324

325-
AuthorizationURL string `json:"authorizationUrl,omitempty" yaml:"authorizationUrl,omitempty"`
326-
TokenURL string `json:"tokenUrl,omitempty" yaml:"tokenUrl,omitempty"`
327-
RefreshURL string `json:"refreshUrl,omitempty" yaml:"refreshUrl,omitempty"`
328-
Scopes StringMap[string] `json:"scopes" yaml:"scopes"` // required
325+
AuthorizationURL string `json:"authorizationUrl,omitempty" yaml:"authorizationUrl,omitempty"`
326+
TokenURL string `json:"tokenUrl,omitempty" yaml:"tokenUrl,omitempty"`
327+
RefreshURL string `json:"refreshUrl,omitempty" yaml:"refreshUrl,omitempty"`
328+
Scopes StringMap `json:"scopes" yaml:"scopes"` // required
329329
}
330330

331331
// MarshalJSON returns the JSON encoding of OAuthFlow.

openapi3/stringmap.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ package openapi3
33
import "encoding/json"
44

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

88
// UnmarshalJSON sets StringMap to a copy of data.
9-
func (stringMap *StringMap[V]) UnmarshalJSON(data []byte) (err error) {
10-
*stringMap, _, err = unmarshalStringMap[V](data)
9+
func (stringMap *StringMap) UnmarshalJSON(data []byte) (err error) {
10+
*stringMap, _, err = unmarshalStringMap[string](data)
1111
return
1212
}
1313

openapi3/testdata/discriminator.yml

Lines changed: 0 additions & 24 deletions
This file was deleted.

openapi3/testdata/discriminator.yml.internalized.yml

Lines changed: 0 additions & 75 deletions
This file was deleted.

0 commit comments

Comments
 (0)