fix: OpenAPI 3.1: Missing description when $ref used #1727

This commit is contained in:
Roman Marshevskyy 2021-10-11 10:53:28 +03:00
parent bccd21394e
commit 35f77878de
5 changed files with 73 additions and 1 deletions

View File

@ -174,6 +174,18 @@ export class OpenAPIParser {
return obj;
}
shallowDeref<T extends unknown>(obj: OpenAPIRef | T): T {
if (this.isRef(obj)) {
const schemaName = getDefinitionName(obj.$ref);
if (schemaName && this.options.ignoreNamedSchemas.has(schemaName)) {
return { type: 'object', title: schemaName } as T;
}
const resolved = this.byRef<T>(obj.$ref);
return this.allowMergeRefs ? this.mergeRefs(obj, resolved, false) : (resolved as T);
}
return obj;
}
mergeRefs(ref, resolved, mergeAsAllOf: boolean) {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { $ref, ...rest } = ref;

View File

@ -1,5 +1,6 @@
import { OpenAPIParser } from '../OpenAPIParser';
import { RedocNormalizedOptions } from '../RedocNormalizedOptions';
import { OpenAPIParameter, Referenced } from '../../types';
const opts = new RedocNormalizedOptions({});
@ -13,5 +14,18 @@ describe('Models', () => {
parser = new OpenAPIParser(spec, undefined, opts);
expect(parser.mergeAllOf(spec.components.schemas.test)).toMatchSnapshot();
});
test('should override description from $ref of the referenced component, when sibling description exists ', () => {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const spec = require('./fixtures/siblingRefDescription.json');
parser = new OpenAPIParser(spec, undefined, opts);
const schemaOrRef: Referenced<OpenAPIParameter> = {
$ref: '#/components/schemas/Test',
description: 'Overriden description',
};
expect(parser.shallowDeref(schemaOrRef)).toMatchSnapshot();
});
});
});

View File

@ -86,3 +86,10 @@ Object {
],
}
`;
exports[`Models Schema should override description from $ref of the referenced component, when sibling description exists 1`] = `
Object {
"description": "Overriden description",
"type": "object",
}
`;

View File

@ -0,0 +1,39 @@
{
"openapi": "3.1.0",
"info": {
"title": "AA",
"version": "1.0"
},
"paths": {
"/test": {
"get": {
"operationId": "test",
"responses": {
"200": {
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {
"testAttr": {
"description": "Overriden description",
"$ref": "#/components/schemas/Test"
}
}
}
}
}
}
}
}
}
},
"components": {
"schemas": {
"Test": {
"type": "object",
"description": "Refed description"
}
}
}
}

View File

@ -78,7 +78,7 @@ export class SchemaModel {
makeObservable(this);
this.pointer = schemaOrRef.$ref || pointer || '';
this.rawSchema = parser.deref(schemaOrRef, false, true);
this.rawSchema = parser.shallowDeref(schemaOrRef);
this.schema = parser.mergeAllOf(this.rawSchema, this.pointer, isChild);
this.init(parser, isChild);