Skip to content

Commit f9c1310

Browse files
committed
Addressed issue #418
An edge case with a reference definition has now been handled. Also added a schema nil check, to prevent a NPE seenin the wild.
1 parent ad0ad33 commit f9c1310

File tree

5 files changed

+53
-2
lines changed

5 files changed

+53
-2
lines changed

document_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1454,3 +1454,20 @@ components:
14541454
_, errs := doc.BuildV3Model()
14551455
assert.Len(t, errs, 0)
14561456
}
1457+
1458+
func TestDocument_Issue418(t *testing.T) {
1459+
1460+
spec, _ := os.ReadFile("test_specs/nested_files/openapi-issue-418.yaml")
1461+
1462+
doc, err := NewDocumentWithConfiguration(spec, &datamodel.DocumentConfiguration{
1463+
AllowFileReferences: true,
1464+
BasePath: "test_specs/nested_files",
1465+
SpecFilePath: "test_specs/nested_files/openapi-issue-418.yaml",
1466+
})
1467+
if err != nil {
1468+
panic(err)
1469+
}
1470+
m, errs := doc.BuildV3Model()
1471+
assert.Len(t, errs, 0)
1472+
assert.Len(t, m.Model.Index.GetResolver().GetResolvingErrors(), 0)
1473+
}

index/search_index.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,12 @@ func (index *SpecIndex) SearchIndexForReferenceByReferenceWithContext(ctx contex
143143
abp := index.GetSpecAbsolutePath()
144144

145145
if b == sfn && roloLookup == abp {
146-
return nil, index, ctx
146+
// if the reference is the same as the spec file name, we should look through the index for the component
147+
var r *Reference
148+
if len(uri) == 2 {
149+
r = index.FindComponentInRoot(fmt.Sprintf("#/%s", uri[1]))
150+
}
151+
return r, index, ctx
147152
}
148153
rFile, err := index.rolodex.Open(roloLookup)
149154
if err != nil {
@@ -169,7 +174,7 @@ func (index *SpecIndex) SearchIndexForReferenceByReferenceWithContext(ctx contex
169174
}
170175
}
171176

172-
if strings.HasSuffix(refParsed, n) {
177+
if strings.HasSuffix(n, refParsed) {
173178
node, _ := rFile.GetContentAsYAMLNode()
174179
if node != nil {
175180
r := &Reference{
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# https://github.com/pb33f/libopenapi/issues/418
2+
get:
3+
parameters:
4+
- name: id
5+
in: path
6+
description: user id
7+
required: true
8+
schema:
9+
type: string
10+
responses:
11+
'200':
12+
$ref: '../openapi-issue-418.yaml#/components/responses/successResponse'
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
openapi: '3.0.3'
2+
paths:
3+
'/foo':
4+
$ref: './components/foo.yaml'
5+
components:
6+
responses:
7+
successResponse:
8+
description: "success"
9+
content:
10+
application/json:
11+
schema:
12+
properties:
13+
message:
14+
type: string

what-changed/model/schema.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ type SchemaChanges struct {
5050
}
5151

5252
func (s *SchemaChanges) GetPropertyChanges() []*Change {
53+
if s == nil {
54+
return nil
55+
}
5356
changes := s.Changes
5457
if s.SchemaPropertyChanges != nil {
5558
for n := range s.SchemaPropertyChanges {

0 commit comments

Comments
 (0)