Skip to content

Commit

Permalink
openapi3: refacto drill bit (#865)
Browse files Browse the repository at this point in the history
Signed-off-by: Pierre Fenoll <pierrefenoll@gmail.com>
  • Loading branch information
fenollp committed Nov 25, 2023
1 parent 14fc067 commit 6986148
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions openapi3/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,11 +315,25 @@ func (loader *Loader) resolveComponent(doc *T, ref string, path *url.URL, resolv
drill := func(cursor interface{}) (interface{}, error) {
for _, pathPart := range strings.Split(fragment[1:], "/") {
pathPart = unescapeRefString(pathPart)
attempted := false

// Special case due to multijson
if s, ok := cursor.(*SchemaRef); ok && pathPart == "additionalProperties" {
if ap := s.Value.AdditionalProperties.Has; ap != nil {
cursor = *ap
} else {
cursor = s.Value.AdditionalProperties.Schema
}
attempted = true
}

if cursor, err = drillIntoField(cursor, pathPart); err != nil {
e := failedToResolveRefFragmentPart(ref, pathPart)
return nil, fmt.Errorf("%s: %w", e, err)
if !attempted {
if cursor, err = drillIntoField(cursor, pathPart); err != nil {
e := failedToResolveRefFragmentPart(ref, pathPart)
return nil, fmt.Errorf("%s: %w", e, err)
}
}

if cursor == nil {
return nil, failedToResolveRefFragmentPart(ref, pathPart)
}
Expand Down Expand Up @@ -401,14 +415,6 @@ func readableType(x interface{}) string {
}

func drillIntoField(cursor interface{}, fieldName string) (interface{}, error) {
// Special case due to multijson
if s, ok := cursor.(*SchemaRef); ok && fieldName == "additionalProperties" {
if ap := s.Value.AdditionalProperties.Has; ap != nil {
return *ap, nil
}
return s.Value.AdditionalProperties.Schema, nil
}

switch val := reflect.Indirect(reflect.ValueOf(cursor)); val.Kind() {
case reflect.Map:
elementValue := val.MapIndex(reflect.ValueOf(fieldName))
Expand Down

0 comments on commit 6986148

Please sign in to comment.