Skip to content

Commit

Permalink
Set ref path when resolving refs too
Browse files Browse the repository at this point in the history
  • Loading branch information
percivalalb committed Jun 17, 2024
1 parent ad0d7d0 commit 2b0efa4
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
16 changes: 16 additions & 0 deletions openapi3/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,8 +385,22 @@ func (loader *Loader) resolveComponent(doc *T, ref string, path *url.URL, resolv
err = nil
}

setComponent := func(target any) {
if componentPath != nil {
if i, ok := target.(interface {
setRefPath(*url.URL)
}); ok {
copy := *componentPath
copy.Fragment = parsedURL.Fragment
i.setRefPath(&copy)
}
}
}

switch {
case reflect.TypeOf(cursor) == reflect.TypeOf(resolved):
setComponent(cursor)

reflect.ValueOf(resolved).Elem().Set(reflect.ValueOf(cursor).Elem())
return componentDoc, componentPath, nil

Expand All @@ -399,6 +413,8 @@ func (loader *Loader) resolveComponent(doc *T, ref string, path *url.URL, resolv
if err = json.Unmarshal(enc, expect); err != nil {
return err
}

setComponent(expect)
return nil
}
if err := codec(cursor, resolved); err != nil {
Expand Down
18 changes: 18 additions & 0 deletions openapi3/refs.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions openapi3/refs.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ func (x *{{ $type.Name }}Ref) CollectionName() string { return "{{ $type.Collect
// RefPath returns the path of the $ref relative to the root document.
func (x *{{ $type.Name }}Ref) RefPath() *url.URL { return &x.refPath }

func (x *{{ $type.Name }}Ref) setRefPath(u *url.URL) { x.refPath = *u }

// MarshalYAML returns the YAML encoding of {{ $type.Name }}Ref.
func (x {{ $type.Name }}Ref) MarshalYAML() (interface{}, error) {
if ref := x.Ref; ref != "" {
Expand Down

0 comments on commit 2b0efa4

Please sign in to comment.