diff --git a/demo/playground/hmr-playground.tsx b/demo/playground/hmr-playground.tsx index 3901cfbd..efd843ab 100644 --- a/demo/playground/hmr-playground.tsx +++ b/demo/playground/hmr-playground.tsx @@ -9,7 +9,7 @@ const swagger = window.location.search.indexOf('swagger') > -1; const userUrl = window.location.search.match(/url=(.*)$/); const specUrl = - (userUrl && userUrl[1]) || (swagger ? 'swagger.yaml' : big ? 'big-openapi.json' : 'openapi-3-1.yaml'); + (userUrl && userUrl[1]) || (swagger ? 'swagger.yaml' : big ? 'big-openapi.json' : 'openapi.yaml'); const options: RedocRawOptions = { nativeScrollbars: false, maxDisplayedEnumValues: 3 }; diff --git a/src/services/OpenAPIParser.ts b/src/services/OpenAPIParser.ts index 83a65fda..11a001aa 100644 --- a/src/services/OpenAPIParser.ts +++ b/src/services/OpenAPIParser.ts @@ -148,7 +148,7 @@ export class OpenAPIParser { * @param obj object to dereference * @param forceCircular whether to dereference even if it is circular ref */ - deref(obj: OpenAPIRef | T, forceCircular = false): T { + deref(obj: OpenAPIRef | T, forceCircular = false, mergeAsAllOf = false): T { if (this.isRef(obj)) { const schemaName = getDefinitionName(obj.$ref); if (schemaName && this.options.ignoreNamedSchemas.has(schemaName)) { @@ -166,22 +166,22 @@ export class OpenAPIParser { // deref again in case one more $ref is here let result = resolved; if (this.isRef(resolved)) { - result = this.deref(resolved); + result = this.deref(resolved, false, mergeAsAllOf); this.exitRef(resolved); } - return this.allowMergeRefs ? this.mergeRefs(obj, resolved) : result; + return this.allowMergeRefs ? this.mergeRefs(obj, resolved, mergeAsAllOf) : result; } return obj; } - mergeRefs(ref, resolved) { + mergeRefs(ref, resolved, mergeAsAllOf: boolean) { // eslint-disable-next-line @typescript-eslint/no-unused-vars const { $ref, ...rest } = ref; const keys = Object.keys(rest); if (keys.length === 0) { return resolved; } - if (keys.some((k) => k !== 'description' && k !== 'title' && k !== 'externalDocs')) { + if (mergeAsAllOf && keys.some((k) => k !== 'description' && k !== 'title' && k !== 'externalDocs')) { return { allOf: [resolved, rest], }; @@ -244,7 +244,7 @@ export class OpenAPIParser { return undefined; } - const resolved = this.deref(subSchema, forceCircular); + const resolved = this.deref(subSchema, forceCircular, true); const subRef = subSchema.$ref || undefined; const subMerged = this.mergeAllOf(resolved, subRef, forceCircular, used$Refs); receiver.parentRefs!.push(...(subMerged.parentRefs || [])); diff --git a/src/services/models/Schema.ts b/src/services/models/Schema.ts index f8971ac4..adb2de19 100644 --- a/src/services/models/Schema.ts +++ b/src/services/models/Schema.ts @@ -76,7 +76,7 @@ export class SchemaModel { makeObservable(this); this.pointer = schemaOrRef.$ref || pointer || ''; - this.rawSchema = parser.deref(schemaOrRef); + this.rawSchema = parser.deref(schemaOrRef, false, true); this.schema = parser.mergeAllOf(this.rawSchema, this.pointer, isChild); this.init(parser, isChild); @@ -193,7 +193,7 @@ export class SchemaModel { private initOneOf(oneOf: OpenAPISchema[], parser: OpenAPIParser) { this.oneOf = oneOf!.map((variant, idx) => { - const derefVariant = parser.deref(variant); + const derefVariant = parser.deref(variant, false, true); const merged = parser.mergeAllOf(derefVariant, this.pointer + '/oneOf/' + idx); diff --git a/src/utils/__tests__/__snapshots__/loadAndBundleSpec.test.ts.snap b/src/utils/__tests__/__snapshots__/loadAndBundleSpec.test.ts.snap index c8788037..1aa507f9 100644 --- a/src/utils/__tests__/__snapshots__/loadAndBundleSpec.test.ts.snap +++ b/src/utils/__tests__/__snapshots__/loadAndBundleSpec.test.ts.snap @@ -1864,15 +1864,9 @@ Object { "content": Object { "application/json": Object { "schema": Object { - "allOf": Array [ - Object { - "description": "My Pet", - "title": "Pettie", - }, - Object { - "$ref": "#/components/schemas/Pet", - }, - ], + "$ref": "#/components/schemas/Pet", + "description": "My Pet", + "title": "Pettie", }, }, "application/xml": Object { @@ -1952,11 +1946,7 @@ Object { "Category": Object { "properties": Object { "id": Object { - "allOf": Array [ - Object { - "$ref": "#/components/schemas/Id", - }, - ], + "$ref": "#/components/schemas/Id", "description": "Category ID", }, "name": Object { @@ -2039,19 +2029,11 @@ Object { "type": "boolean", }, "id": Object { - "allOf": Array [ - Object { - "$ref": "#/components/schemas/Id", - }, - ], + "$ref": "#/components/schemas/Id", "description": "Order ID", }, "petId": Object { - "allOf": Array [ - Object { - "$ref": "#/components/schemas/Id", - }, - ], + "$ref": "#/components/schemas/Id", "description": "Pet ID", }, "quantity": Object { @@ -2096,26 +2078,14 @@ Object { }, "properties": Object { "category": Object { - "allOf": Array [ - Object { - "$ref": "#/components/schemas/Category", - }, - ], + "$ref": "#/components/schemas/Category", "description": "Categories this pet belongs to", }, "friend": Object { - "allOf": Array [ - Object { - "$ref": "#/components/schemas/Pet", - }, - ], + "$ref": "#/components/schemas/Pet", }, "id": Object { - "allOf": Array [ - Object { - "$ref": "#/components/schemas/Id", - }, - ], + "$ref": "#/components/schemas/Id", "description": "Pet ID", "externalDocs": Object { "description": "Find more info here", @@ -2186,11 +2156,7 @@ Object { "Tag": Object { "properties": Object { "id": Object { - "allOf": Array [ - Object { - "$ref": "#/components/schemas/Id", - }, - ], + "$ref": "#/components/schemas/Id", "description": "Tag ID", }, "name": Object { @@ -2239,6 +2205,7 @@ Object { "oneOf": Array [ Object { "$ref": "#/components/schemas/Pet", + "title": "Pettie", }, Object { "$ref": "#/components/schemas/Tag",