Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

openapi3: fix ref internalization #832

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 9 additions & 11 deletions openapi3/internalize_refs.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,32 +338,30 @@ func (doc *T) derefRequestBody(r RequestBody, refNameResolver RefNameResolver, p

func (doc *T) derefPaths(paths map[string]*PathItem, refNameResolver RefNameResolver, parentIsExternal bool) {
for _, ops := range paths {
if isExternalRef(ops.Ref, parentIsExternal) {
parentIsExternal = true
}
pathIsExternal := isExternalRef(ops.Ref, parentIsExternal)
// inline full operations
ops.Ref = ""

for _, param := range ops.Parameters {
doc.addParameterToSpec(param, refNameResolver, parentIsExternal)
doc.addParameterToSpec(param, refNameResolver, pathIsExternal)
}

for _, op := range ops.Operations() {
isExternal := doc.addRequestBodyToSpec(op.RequestBody, refNameResolver, parentIsExternal)
isExternal := doc.addRequestBodyToSpec(op.RequestBody, refNameResolver, pathIsExternal)
if op.RequestBody != nil && op.RequestBody.Value != nil {
doc.derefRequestBody(*op.RequestBody.Value, refNameResolver, parentIsExternal || isExternal)
doc.derefRequestBody(*op.RequestBody.Value, refNameResolver, pathIsExternal || isExternal)
}
for _, cb := range op.Callbacks {
isExternal := doc.addCallbackToSpec(cb, refNameResolver, parentIsExternal)
isExternal := doc.addCallbackToSpec(cb, refNameResolver, pathIsExternal)
if cb.Value != nil {
doc.derefPaths(*cb.Value, refNameResolver, parentIsExternal || isExternal)
doc.derefPaths(*cb.Value, refNameResolver, pathIsExternal || isExternal)
}
}
doc.derefResponses(op.Responses, refNameResolver, parentIsExternal)
doc.derefResponses(op.Responses, refNameResolver, pathIsExternal)
for _, param := range op.Parameters {
isExternal := doc.addParameterToSpec(param, refNameResolver, parentIsExternal)
isExternal := doc.addParameterToSpec(param, refNameResolver, pathIsExternal)
if param.Value != nil {
doc.derefParameter(*param.Value, refNameResolver, parentIsExternal || isExternal)
doc.derefParameter(*param.Value, refNameResolver, pathIsExternal || isExternal)
}
}
}
Expand Down
1 change: 1 addition & 0 deletions openapi3/internalize_refs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func TestInternalizeRefs(t *testing.T) {
{"testdata/recursiveRef/openapi.yml"},
{"testdata/spec.yaml"},
{"testdata/callbacks.yml"},
{"testdata/issue831/testref.internalizepath.openapi.yml"},
}

for _, test := range tests {
Expand Down
4 changes: 4 additions & 0 deletions openapi3/testdata/issue831/path.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
get:
responses:
"200":
description: OK
24 changes: 24 additions & 0 deletions openapi3/testdata/issue831/testref.internalizepath.openapi.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
openapi: "3.0.3"
info:
title: Recursive refs example
version: "1.0"
paths:
/bar:
$ref: ./path.yml
/foo:
post:
requestBody:
content:
application/json:
schema:
$ref: "#/components/requestBodies/test/content/application~1json/schema"
responses:
'200':
description: Expected response to a valid request
components:
requestBodies:
test:
content:
application/json:
schema:
type: string
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
"openapi": "3.0.3",
"info": {
"title": "Recursive refs example",
"version": "1.0"
},
"paths": {
"/bar": {
"get": {
"responses": {
"200": {
"description": "OK"
}
}
}
},
"/foo": {
"post": {
"requestBody": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/requestBodies/test/content/application~1json/schema"
}
}
}
},
"responses": {
"200": {
"description": "Expected response to a valid request"
}
}
}
}
},
"components": {
"requestBodies": {
"test": {
"content": {
"application/json": {
"schema": {
"type": "string"
}
}
}
}
}
}
}