Skip to content

Commit

Permalink
openapi3: handle file refs missing root spec #/
Browse files Browse the repository at this point in the history
Signed-off-by: Pierre Fenoll <[email protected]>
  • Loading branch information
fenollp committed Nov 26, 2023
1 parent 663b0dd commit 0aa2d6c
Showing 1 changed file with 68 additions and 0 deletions.
68 changes: 68 additions & 0 deletions openapi3/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,14 @@ func resolvePathWithRef(ref string, rootPath *url.URL) (*url.URL, error) {
return resolvedPath, nil
}

func shouldAppendFragment(ref string, rootPath *url.URL) (bool, error) {
refDocPath, err := resolvePathWithRef(ref, rootPath)
if err != nil {
return false, err
}
return is_file(refDocPath) && refDocPath.Fragment == "", nil
}

func isSingleRefElement(ref string) bool {
return !strings.Contains(ref, "#")
}
Expand Down Expand Up @@ -519,6 +527,12 @@ func (loader *Loader) resolveHeaderRef(doc *T, component *HeaderRef, documentPat
loader.visitedHeader[component.Value] = struct{}{}
}

if should, err := shouldAppendFragment(component.Ref, documentPath); err != nil {
return err
} else if should {
component.Ref += "#/"
}

if ref := component.Ref; ref != "" {
if isSingleRefElement(ref) {
var header Header
Expand Down Expand Up @@ -569,6 +583,12 @@ func (loader *Loader) resolveParameterRef(doc *T, component *ParameterRef, docum
loader.visitedParameter[component.Value] = struct{}{}
}

if should, err := shouldAppendFragment(component.Ref, documentPath); err != nil {
return err
} else if should {
component.Ref += "#/"
}

if ref := component.Ref; ref != "" {
if isSingleRefElement(ref) {
var param Parameter
Expand Down Expand Up @@ -629,6 +649,12 @@ func (loader *Loader) resolveRequestBodyRef(doc *T, component *RequestBodyRef, d
loader.visitedRequestBody[component.Value] = struct{}{}
}

if should, err := shouldAppendFragment(component.Ref, documentPath); err != nil {
return err
} else if should {
component.Ref += "#/"
}

if ref := component.Ref; ref != "" {
if isSingleRefElement(ref) {
var requestBody RequestBody
Expand Down Expand Up @@ -696,6 +722,12 @@ func (loader *Loader) resolveResponseRef(doc *T, component *ResponseRef, documen
loader.visitedResponse[component.Value] = struct{}{}
}

if should, err := shouldAppendFragment(component.Ref, documentPath); err != nil {
return err
} else if should {
component.Ref += "#/"
}

if ref := component.Ref; ref != "" {
if isSingleRefElement(ref) {
var resp Response
Expand Down Expand Up @@ -774,6 +806,12 @@ func (loader *Loader) resolveSchemaRef(doc *T, component *SchemaRef, documentPat
loader.visitedSchema[component.Value] = struct{}{}
}

if should, err := shouldAppendFragment(component.Ref, documentPath); err != nil {
return err
} else if should {
component.Ref += "#/"
}

if ref := component.Ref; ref != "" {
if isSingleRefElement(ref) {
var schema Schema
Expand Down Expand Up @@ -865,6 +903,12 @@ func (loader *Loader) resolveSecuritySchemeRef(doc *T, component *SecurityScheme
loader.visitedSecurityScheme[component.Value] = struct{}{}
}

if should, err := shouldAppendFragment(component.Ref, documentPath); err != nil {
return err
} else if should {
component.Ref += "#/"
}

if ref := component.Ref; ref != "" {
if isSingleRefElement(ref) {
var scheme SecurityScheme
Expand Down Expand Up @@ -905,6 +949,12 @@ func (loader *Loader) resolveExampleRef(doc *T, component *ExampleRef, documentP
loader.visitedExample[component.Value] = struct{}{}
}

if should, err := shouldAppendFragment(component.Ref, documentPath); err != nil {
return err
} else if should {
component.Ref += "#/"
}

if ref := component.Ref; ref != "" {
if isSingleRefElement(ref) {
var example Example
Expand Down Expand Up @@ -945,6 +995,12 @@ func (loader *Loader) resolveCallbackRef(doc *T, component *CallbackRef, documen
loader.visitedCallback[component.Value] = struct{}{}
}

if should, err := shouldAppendFragment(component.Ref, documentPath); err != nil {
return err
} else if should {
component.Ref += "#/"
}

if ref := component.Ref; ref != "" {
if isSingleRefElement(ref) {
var resolved Callback
Expand Down Expand Up @@ -995,6 +1051,12 @@ func (loader *Loader) resolveLinkRef(doc *T, component *LinkRef, documentPath *u
loader.visitedLink[component.Value] = struct{}{}
}

if should, err := shouldAppendFragment(component.Ref, documentPath); err != nil {
return err
} else if should {
component.Ref += "#/"
}

if ref := component.Ref; ref != "" {
if isSingleRefElement(ref) {
var link Link
Expand Down Expand Up @@ -1026,6 +1088,12 @@ func (loader *Loader) resolvePathItemRef(doc *T, pathItem *PathItem, documentPat
return
}

if should, err := shouldAppendFragment(pathItem.Ref, documentPath); err != nil {
return err
} else if should {
pathItem.Ref += "#/"
}

if ref := pathItem.Ref; ref != "" {
if !pathItem.isEmpty() {
return
Expand Down

0 comments on commit 0aa2d6c

Please sign in to comment.