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

openapi3: refacto drill bit #865

Merged
merged 1 commit into from
Nov 25, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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