Skip to content

Commit

Permalink
Merge pull request #151 from tdakkota/fix/incorrect-discriminator-inf…
Browse files Browse the repository at this point in the history
…erence

fix(gen): discriminator inference by unique fields
  • Loading branch information
ernado committed Feb 2, 2022
2 parents 276b190 + 69925cd commit eb5784c
Showing 1 changed file with 21 additions and 24 deletions.
45 changes: 21 additions & 24 deletions gen/schema_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -402,13 +402,14 @@ func (g *schemaGen) oneOf(name string, schema *oas.Schema) (*ir.Type, error) {
var (
// Determine unique fields for each SumOf variant.
uniq = map[string]map[string]struct{}{}
// Whether sum has complex types.
isComplex bool
)
for _, s := range sum.SumOf {
uniq[s.Name] = map[string]struct{}{}
if !s.Is(ir.KindPrimitive) {
isComplex = true
if !s.Is(ir.KindMap, ir.KindStruct) {
return nil, errors.Wrapf(&ErrNotImplemented{Name: "discriminator inference"},
"oneOf %s: variant %s: no unique fields, "+
"unable to parse without discriminator", sum.Name, s.Name,
)
}
for _, f := range s.Fields {
uniq[s.Name][f.Name] = struct{}{}
Expand Down Expand Up @@ -441,26 +442,25 @@ func (g *schemaGen) oneOf(name string, schema *oas.Schema) (*ir.Type, error) {
}
}

if isComplex {
// Check that at most one type has no unique fields.
metNoUniqueFields := false
for _, variant := range sum.SumOf {
k := variant.Name
if len(uniq[k]) == 0 {
if metNoUniqueFields {
// Unable to deterministically select sub-schema only on fields.
return nil, errors.Wrapf(&ErrNotImplemented{Name: "discriminator inference"},
"oneOf %s: variant %s: no unique fields, "+
"unable to parse without discriminator", sum.Name, k,
)
}

// Set mapping without unique fields as default
sum.SumSpec.DefaultMapping = k
metNoUniqueFields = true
// Check that at most one type has no unique fields.
metNoUniqueFields := false
for _, variant := range sum.SumOf {
k := variant.Name
if len(uniq[k]) == 0 {
if metNoUniqueFields {
// Unable to deterministically select sub-schema only on fields.
return nil, errors.Wrapf(&ErrNotImplemented{Name: "discriminator inference"},
"oneOf %s: variant %s: no unique fields, "+
"unable to parse without discriminator", sum.Name, k,
)
}

// Set mapping without unique fields as default
sum.SumSpec.DefaultMapping = k
metNoUniqueFields = true
}
}

}
type sumVariant struct {
Name string
Expand All @@ -484,9 +484,6 @@ func (g *schemaGen) oneOf(name string, schema *oas.Schema) (*ir.Type, error) {
b := variants[j]
return strings.Compare(a.Name, b.Name) < 0
})
if !isComplex {
return sum, nil
}
for _, v := range variants {
for _, s := range sum.SumOf {
if s.Name != v.Name {
Expand Down

0 comments on commit eb5784c

Please sign in to comment.