chore: fix deref for non-schemas

This commit is contained in:
romanhotsiy 2021-06-09 15:42:32 +03:00
parent fa6957f22f
commit 5332e33c9f
No known key found for this signature in database
GPG Key ID: 0BC2221278CCBBB8
4 changed files with 20 additions and 53 deletions

View File

@ -9,7 +9,7 @@ const swagger = window.location.search.indexOf('swagger') > -1;
const userUrl = window.location.search.match(/url=(.*)$/); const userUrl = window.location.search.match(/url=(.*)$/);
const specUrl = 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 }; const options: RedocRawOptions = { nativeScrollbars: false, maxDisplayedEnumValues: 3 };

View File

@ -148,7 +148,7 @@ export class OpenAPIParser {
* @param obj object to dereference * @param obj object to dereference
* @param forceCircular whether to dereference even if it is circular ref * @param forceCircular whether to dereference even if it is circular ref
*/ */
deref<T extends object>(obj: OpenAPIRef | T, forceCircular = false): T { deref<T extends object>(obj: OpenAPIRef | T, forceCircular = false, mergeAsAllOf = false): T {
if (this.isRef(obj)) { if (this.isRef(obj)) {
const schemaName = getDefinitionName(obj.$ref); const schemaName = getDefinitionName(obj.$ref);
if (schemaName && this.options.ignoreNamedSchemas.has(schemaName)) { if (schemaName && this.options.ignoreNamedSchemas.has(schemaName)) {
@ -166,22 +166,22 @@ export class OpenAPIParser {
// deref again in case one more $ref is here // deref again in case one more $ref is here
let result = resolved; let result = resolved;
if (this.isRef(resolved)) { if (this.isRef(resolved)) {
result = this.deref(resolved); result = this.deref(resolved, false, mergeAsAllOf);
this.exitRef(resolved); this.exitRef(resolved);
} }
return this.allowMergeRefs ? this.mergeRefs(obj, resolved) : result; return this.allowMergeRefs ? this.mergeRefs(obj, resolved, mergeAsAllOf) : result;
} }
return obj; return obj;
} }
mergeRefs(ref, resolved) { mergeRefs(ref, resolved, mergeAsAllOf: boolean) {
// eslint-disable-next-line @typescript-eslint/no-unused-vars // eslint-disable-next-line @typescript-eslint/no-unused-vars
const { $ref, ...rest } = ref; const { $ref, ...rest } = ref;
const keys = Object.keys(rest); const keys = Object.keys(rest);
if (keys.length === 0) { if (keys.length === 0) {
return resolved; return resolved;
} }
if (keys.some((k) => k !== 'description' && k !== 'title' && k !== 'externalDocs')) { if (mergeAsAllOf && keys.some((k) => k !== 'description' && k !== 'title' && k !== 'externalDocs')) {
return { return {
allOf: [resolved, rest], allOf: [resolved, rest],
}; };
@ -244,7 +244,7 @@ export class OpenAPIParser {
return undefined; return undefined;
} }
const resolved = this.deref(subSchema, forceCircular); const resolved = this.deref(subSchema, forceCircular, true);
const subRef = subSchema.$ref || undefined; const subRef = subSchema.$ref || undefined;
const subMerged = this.mergeAllOf(resolved, subRef, forceCircular, used$Refs); const subMerged = this.mergeAllOf(resolved, subRef, forceCircular, used$Refs);
receiver.parentRefs!.push(...(subMerged.parentRefs || [])); receiver.parentRefs!.push(...(subMerged.parentRefs || []));

View File

@ -76,7 +76,7 @@ export class SchemaModel {
makeObservable(this); makeObservable(this);
this.pointer = schemaOrRef.$ref || pointer || ''; 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.schema = parser.mergeAllOf(this.rawSchema, this.pointer, isChild);
this.init(parser, isChild); this.init(parser, isChild);
@ -193,7 +193,7 @@ export class SchemaModel {
private initOneOf(oneOf: OpenAPISchema[], parser: OpenAPIParser) { private initOneOf(oneOf: OpenAPISchema[], parser: OpenAPIParser) {
this.oneOf = oneOf!.map((variant, idx) => { 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); const merged = parser.mergeAllOf(derefVariant, this.pointer + '/oneOf/' + idx);

View File

@ -1864,15 +1864,9 @@ Object {
"content": Object { "content": Object {
"application/json": Object { "application/json": Object {
"schema": Object { "schema": Object {
"allOf": Array [ "$ref": "#/components/schemas/Pet",
Object { "description": "My Pet",
"description": "My Pet", "title": "Pettie",
"title": "Pettie",
},
Object {
"$ref": "#/components/schemas/Pet",
},
],
}, },
}, },
"application/xml": Object { "application/xml": Object {
@ -1952,11 +1946,7 @@ Object {
"Category": Object { "Category": Object {
"properties": Object { "properties": Object {
"id": Object { "id": Object {
"allOf": Array [ "$ref": "#/components/schemas/Id",
Object {
"$ref": "#/components/schemas/Id",
},
],
"description": "Category ID", "description": "Category ID",
}, },
"name": Object { "name": Object {
@ -2039,19 +2029,11 @@ Object {
"type": "boolean", "type": "boolean",
}, },
"id": Object { "id": Object {
"allOf": Array [ "$ref": "#/components/schemas/Id",
Object {
"$ref": "#/components/schemas/Id",
},
],
"description": "Order ID", "description": "Order ID",
}, },
"petId": Object { "petId": Object {
"allOf": Array [ "$ref": "#/components/schemas/Id",
Object {
"$ref": "#/components/schemas/Id",
},
],
"description": "Pet ID", "description": "Pet ID",
}, },
"quantity": Object { "quantity": Object {
@ -2096,26 +2078,14 @@ Object {
}, },
"properties": Object { "properties": Object {
"category": Object { "category": Object {
"allOf": Array [ "$ref": "#/components/schemas/Category",
Object {
"$ref": "#/components/schemas/Category",
},
],
"description": "Categories this pet belongs to", "description": "Categories this pet belongs to",
}, },
"friend": Object { "friend": Object {
"allOf": Array [ "$ref": "#/components/schemas/Pet",
Object {
"$ref": "#/components/schemas/Pet",
},
],
}, },
"id": Object { "id": Object {
"allOf": Array [ "$ref": "#/components/schemas/Id",
Object {
"$ref": "#/components/schemas/Id",
},
],
"description": "Pet ID", "description": "Pet ID",
"externalDocs": Object { "externalDocs": Object {
"description": "Find more info here", "description": "Find more info here",
@ -2186,11 +2156,7 @@ Object {
"Tag": Object { "Tag": Object {
"properties": Object { "properties": Object {
"id": Object { "id": Object {
"allOf": Array [ "$ref": "#/components/schemas/Id",
Object {
"$ref": "#/components/schemas/Id",
},
],
"description": "Tag ID", "description": "Tag ID",
}, },
"name": Object { "name": Object {
@ -2239,6 +2205,7 @@ Object {
"oneOf": Array [ "oneOf": Array [
Object { Object {
"$ref": "#/components/schemas/Pet", "$ref": "#/components/schemas/Pet",
"title": "Pettie",
}, },
Object { Object {
"$ref": "#/components/schemas/Tag", "$ref": "#/components/schemas/Tag",