Skip to content

Commit

Permalink
Correctly resolve path of yaml resource if double referenced.
Browse files Browse the repository at this point in the history
Where previously following path was resolved `testdata/components/models/error.yaml`, missing `recursiveRef` folder.

 Author:    davor.sauer <davor.sauer@gmail.com>
  • Loading branch information
d-sauer committed Sep 22, 2022
1 parent d12860c commit 69e5596
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 16 deletions.
16 changes: 8 additions & 8 deletions openapi3/internalize_refs_test.go
Expand Up @@ -2,7 +2,7 @@ package openapi3

import (
"context"
"io/ioutil"
"os"
"regexp"
"testing"

Expand Down Expand Up @@ -41,25 +41,25 @@ func TestInternalizeRefs(t *testing.T) {
err = doc.Validate(ctx)
require.NoError(t, err, "validating internalized spec")

data, err := doc.MarshalJSON()
actual, err := doc.MarshalJSON()
require.NoError(t, err, "marshalling internalized spec")

// run a static check over the file, making sure each occurence of a
// reference is followed by a #
numRefs := len(regexpRef.FindAll(data, -1))
numInternalRefs := len(regexpRefInternal.FindAll(data, -1))
numRefs := len(regexpRef.FindAll(actual, -1))
numInternalRefs := len(regexpRefInternal.FindAll(actual, -1))
require.Equal(t, numRefs, numInternalRefs, "checking all references are internal")

// load from data, but with the path set to the current directory
doc2, err := sl.LoadFromData(data)
// load from actual, but with the path set to the current directory
doc2, err := sl.LoadFromData(actual)
require.NoError(t, err, "reloading spec")
err = doc2.Validate(ctx)
require.NoError(t, err, "validating reloaded spec")

// compare with expected
data0, err := ioutil.ReadFile(test.filename + ".internalized.yml")
expected, err := os.ReadFile(test.filename + ".internalized.yml")
require.NoError(t, err)
require.JSONEq(t, string(data), string(data0))
require.JSONEq(t, string(expected), string(actual))
})
}
}
5 changes: 5 additions & 0 deletions openapi3/loader.go
Expand Up @@ -409,6 +409,11 @@ func (loader *Loader) documentPathForRecursiveRef(current *url.URL, resolvedRef
if loader.rootDir == "" {
return current
}

if resolvedRef == "" {
return current
}

return &url.URL{Path: path.Join(loader.rootDir, resolvedRef)}
}

Expand Down
2 changes: 2 additions & 0 deletions openapi3/loader_recursive_ref_test.go
Expand Up @@ -14,6 +14,8 @@ func TestLoaderSupportsRecursiveReference(t *testing.T) {
err = doc.Validate(loader.Context)
require.NoError(t, err)
require.Equal(t, "bar", doc.Paths["/foo"].Get.Responses.Get(200).Value.Content.Get("application/json").Schema.Value.Properties["foo2"].Value.Properties["foo"].Value.Properties["bar"].Value.Example)
require.Equal(t, "ErrorDetails", doc.Paths["/foo"].Get.Responses.Get(400).Value.Content.Get("application/json").Schema.Value.Title)
require.Equal(t, "ErrorDetails", doc.Paths["/double-ref-foo"].Get.Responses.Get(400).Value.Content.Get("application/json").Schema.Value.Title)
}

func TestIssue447(t *testing.T) {
Expand Down
2 changes: 2 additions & 0 deletions openapi3/testdata/recursiveRef/components/models/error.yaml
@@ -0,0 +1,2 @@
type: object
title: ErrorDetails
16 changes: 16 additions & 0 deletions openapi3/testdata/recursiveRef/openapi.yml
Expand Up @@ -5,6 +5,13 @@ info:
paths:
/foo:
$ref: ./paths/foo.yml
/double-ref-foo:
get:
summary: Double ref response
description: Reference response with double reference.
responses:
"400":
$ref: "#/components/responses/400"
components:
schemas:
Foo:
Expand All @@ -15,3 +22,12 @@ components:
$ref: ./components/Bar.yml
Cat:
$ref: ./components/Cat.yml
Error:
$ref: ./components/models/error.yaml
responses:
"400":
description: 400 Bad Request
content:
application/json:
schema:
$ref: "#/components/schemas/Error"
50 changes: 42 additions & 8 deletions openapi3/testdata/recursiveRef/openapi.yml.internalized.yml
Expand Up @@ -9,11 +9,35 @@
}
}
},
"responses": {
"400": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
},
"description": "400 Bad Request"
}
},
"schemas": {
"Bar": {
"example": "bar",
"type": "string"
},
"Cat": {
"properties": {
"cat": {
"$ref": "#/components/schemas/Cat"
}
},
"type": "object"
},
"Error":{
"title":"ErrorDetails",
"type":"object"
},
"Foo": {
"properties": {
"bar": {
Expand All @@ -30,13 +54,9 @@
},
"type": "object"
},
"Cat": {
"properties": {
"cat": {
"$ref": "#/components/schemas/Cat"
}
},
"type": "object"
"error":{
"title":"ErrorDetails",
"type":"object"
}
}
},
Expand All @@ -46,6 +66,17 @@
},
"openapi": "3.0.3",
"paths": {
"/double-ref-foo": {
"get": {
"description": "Reference response with double reference.",
"responses": {
"400": {
"$ref": "#/components/responses/400"
}
},
"summary": "Double ref response"
}
},
"/foo": {
"get": {
"responses": {
Expand All @@ -63,6 +94,9 @@
}
},
"description": "OK"
},
"400": {
"$ref": "#/components/responses/400"
}
}
},
Expand All @@ -73,4 +107,4 @@
]
}
}
}
}
2 changes: 2 additions & 0 deletions openapi3/testdata/recursiveRef/paths/foo.yml
Expand Up @@ -11,3 +11,5 @@ get:
properties:
foo2:
$ref: ../openapi.yml#/components/schemas/Foo2
"400":
$ref: "../openapi.yml#/components/responses/400"

0 comments on commit 69e5596

Please sign in to comment.