Skip to content

Commit

Permalink
openapi3: fix ref internalization (#832)
Browse files Browse the repository at this point in the history
  • Loading branch information
radwaretaltr authored Aug 30, 2023
1 parent 8c08c70 commit 14c8565
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 11 deletions.
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"
}
}
}
}
}
}
}

0 comments on commit 14c8565

Please sign in to comment.