Skip to content

Commit 14c8565

Browse files
authored
openapi3: fix ref internalization (#832)
1 parent 8c08c70 commit 14c8565

File tree

5 files changed

+87
-11
lines changed

5 files changed

+87
-11
lines changed

openapi3/internalize_refs.go

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -338,32 +338,30 @@ func (doc *T) derefRequestBody(r RequestBody, refNameResolver RefNameResolver, p
338338

339339
func (doc *T) derefPaths(paths map[string]*PathItem, refNameResolver RefNameResolver, parentIsExternal bool) {
340340
for _, ops := range paths {
341-
if isExternalRef(ops.Ref, parentIsExternal) {
342-
parentIsExternal = true
343-
}
341+
pathIsExternal := isExternalRef(ops.Ref, parentIsExternal)
344342
// inline full operations
345343
ops.Ref = ""
346344

347345
for _, param := range ops.Parameters {
348-
doc.addParameterToSpec(param, refNameResolver, parentIsExternal)
346+
doc.addParameterToSpec(param, refNameResolver, pathIsExternal)
349347
}
350348

351349
for _, op := range ops.Operations() {
352-
isExternal := doc.addRequestBodyToSpec(op.RequestBody, refNameResolver, parentIsExternal)
350+
isExternal := doc.addRequestBodyToSpec(op.RequestBody, refNameResolver, pathIsExternal)
353351
if op.RequestBody != nil && op.RequestBody.Value != nil {
354-
doc.derefRequestBody(*op.RequestBody.Value, refNameResolver, parentIsExternal || isExternal)
352+
doc.derefRequestBody(*op.RequestBody.Value, refNameResolver, pathIsExternal || isExternal)
355353
}
356354
for _, cb := range op.Callbacks {
357-
isExternal := doc.addCallbackToSpec(cb, refNameResolver, parentIsExternal)
355+
isExternal := doc.addCallbackToSpec(cb, refNameResolver, pathIsExternal)
358356
if cb.Value != nil {
359-
doc.derefPaths(*cb.Value, refNameResolver, parentIsExternal || isExternal)
357+
doc.derefPaths(*cb.Value, refNameResolver, pathIsExternal || isExternal)
360358
}
361359
}
362-
doc.derefResponses(op.Responses, refNameResolver, parentIsExternal)
360+
doc.derefResponses(op.Responses, refNameResolver, pathIsExternal)
363361
for _, param := range op.Parameters {
364-
isExternal := doc.addParameterToSpec(param, refNameResolver, parentIsExternal)
362+
isExternal := doc.addParameterToSpec(param, refNameResolver, pathIsExternal)
365363
if param.Value != nil {
366-
doc.derefParameter(*param.Value, refNameResolver, parentIsExternal || isExternal)
364+
doc.derefParameter(*param.Value, refNameResolver, pathIsExternal || isExternal)
367365
}
368366
}
369367
}

openapi3/internalize_refs_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ func TestInternalizeRefs(t *testing.T) {
2222
{"testdata/recursiveRef/openapi.yml"},
2323
{"testdata/spec.yaml"},
2424
{"testdata/callbacks.yml"},
25+
{"testdata/issue831/testref.internalizepath.openapi.yml"},
2526
}
2627

2728
for _, test := range tests {

openapi3/testdata/issue831/path.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
get:
2+
responses:
3+
"200":
4+
description: OK
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
openapi: "3.0.3"
2+
info:
3+
title: Recursive refs example
4+
version: "1.0"
5+
paths:
6+
/bar:
7+
$ref: ./path.yml
8+
/foo:
9+
post:
10+
requestBody:
11+
content:
12+
application/json:
13+
schema:
14+
$ref: "#/components/requestBodies/test/content/application~1json/schema"
15+
responses:
16+
'200':
17+
description: Expected response to a valid request
18+
components:
19+
requestBodies:
20+
test:
21+
content:
22+
application/json:
23+
schema:
24+
type: string
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
{
2+
"openapi": "3.0.3",
3+
"info": {
4+
"title": "Recursive refs example",
5+
"version": "1.0"
6+
},
7+
"paths": {
8+
"/bar": {
9+
"get": {
10+
"responses": {
11+
"200": {
12+
"description": "OK"
13+
}
14+
}
15+
}
16+
},
17+
"/foo": {
18+
"post": {
19+
"requestBody": {
20+
"content": {
21+
"application/json": {
22+
"schema": {
23+
"$ref": "#/components/requestBodies/test/content/application~1json/schema"
24+
}
25+
}
26+
}
27+
},
28+
"responses": {
29+
"200": {
30+
"description": "Expected response to a valid request"
31+
}
32+
}
33+
}
34+
}
35+
},
36+
"components": {
37+
"requestBodies": {
38+
"test": {
39+
"content": {
40+
"application/json": {
41+
"schema": {
42+
"type": "string"
43+
}
44+
}
45+
}
46+
}
47+
}
48+
}
49+
}

0 commit comments

Comments
 (0)