diff --git a/refs.go b/refs.go index 4809c9e..cd1172f 100644 --- a/refs.go +++ b/refs.go @@ -442,7 +442,7 @@ func (resolver *refResolver) expand(n node) error { // /components are expanded only on demand (when targeted by a link) // because expanding unused components would inject their external // content into the final document. - if k == "components" && n.loc.Ptr == "" { + if k == "components" && n.loc.Ptr == "" && !hasDirective(obj[k]) { continue } if err := resolver.expandProperty(n.loc, obj, k); err != nil { diff --git a/testdata/47-inline-components/input.yml b/testdata/47-inline-components/input.yml new file mode 100644 index 0000000..2265ea9 --- /dev/null +++ b/testdata/47-inline-components/input.yml @@ -0,0 +1,16 @@ +openapi: 3.0.2 +info: + title: Test + version: "0.0.1" +paths: + /: + get: + responses: + '200': + description: OK + content: + application/json: + schema: + $ref: '#/components/schemas/Pet' +components: + $inline: schemas.yml diff --git a/testdata/47-inline-components/result.json b/testdata/47-inline-components/result.json new file mode 100644 index 0000000..a1d91cc --- /dev/null +++ b/testdata/47-inline-components/result.json @@ -0,0 +1,37 @@ +{ + "components": { + "schemas": { + "Pet": { + "properties": { + "name": { + "type": "string" + } + }, + "type": "object" + } + } + }, + "info": { + "title": "Test", + "version": "0.0.1" + }, + "openapi": "3.0.2", + "paths": { + "/": { + "get": { + "responses": { + "200": { + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Pet" + } + } + }, + "description": "OK" + } + } + } + } + } +} diff --git a/testdata/47-inline-components/schemas.yml b/testdata/47-inline-components/schemas.yml new file mode 100644 index 0000000..a1c6763 --- /dev/null +++ b/testdata/47-inline-components/schemas.yml @@ -0,0 +1,6 @@ +schemas: + Pet: + type: object + properties: + name: + type: string diff --git a/util.go b/util.go index fd72fe4..90f4bec 100644 --- a/util.go +++ b/util.go @@ -21,3 +21,14 @@ func stringProp(obj map[string]any, key string) (value string, ok bool) { value, ok = v.(string) return } + +func hasDirective(v any) bool { + m, ok := v.(map[string]any) + if !ok { + return false + } + _, hasRef := m["$ref"] + _, hasInline := m["$inline"] + _, hasMerge := m["$merge"] + return hasRef || hasInline || hasMerge +}