mirror of
https://github.com/Redocly/redoc.git
synced 2025-02-07 21:40:32 +03:00
fix: OpenAPI 3.1: Missing description when $ref used #1727
This commit is contained in:
parent
bccd21394e
commit
35f77878de
|
@ -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;
|
||||
|
|
|
@ -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();
|
||||
});
|
||||
|
||||
});
|
||||
});
|
||||
|
|
|
@ -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",
|
||||
}
|
||||
`;
|
||||
|
|
39
src/services/__tests__/fixtures/siblingRefDescription.json
Normal file
39
src/services/__tests__/fixtures/siblingRefDescription.json
Normal 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"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
|
|
Loading…
Reference in New Issue
Block a user