fix: schema oneOf title with const (#2350)

This commit is contained in:
Alex Varchuk 2023-07-11 15:33:36 +03:00 committed by GitHub
parent 7e052028d9
commit 4386867d90
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 57 additions and 2 deletions

View File

@ -562,5 +562,59 @@ describe('Models', () => {
`"testAttr: <object> (Refed description)"`, `"testAttr: <object> (Refed description)"`,
); );
}); });
test('should correct get title from in oneOf ->const', () => {
const spec = parseYaml(outdent`
openapi: 3.0.0
paths:
/test:
get:
operationId: test
responses:
'200':
content:
application/json:
schema:
type: object
properties:
data:
type: object
properties:
response_code:
type: integer
description: A numeric response code
oneOf:
- const: 0
description: >
Description for const 0
- const: 1
description: >
Description for const 1
- const: 2
description: >
Description for const 2
`) as any;
parser = new OpenAPIParser(spec, undefined, opts);
const name = 'application/json';
const mediaType = new MediaTypeModel(
parser,
name,
true,
spec.paths['/test'].get.responses['200'].content[name],
opts,
);
expect(printSchema(mediaType?.schema as any)).toMatchInlineSnapshot(`
"data:
response_code: oneOf
0 -> <integer> (Description for const 0
)
1 -> <integer> (Description for const 1
)
2 -> <integer> (Description for const 2
)"
`);
});
}); });
}); });

View File

@ -243,8 +243,9 @@ export class SchemaModel {
const title = const title =
isNamedDefinition(variant.$ref) && !merged.title isNamedDefinition(variant.$ref) && !merged.title
? JsonPointer.baseName(variant.$ref) ? JsonPointer.baseName(variant.$ref)
: `${merged.title || ''}${(merged.const && JSON.stringify(merged.const)) || ''}`; : `${merged.title || ''}${
(typeof merged.const !== 'undefined' && JSON.stringify(merged.const)) || ''
}`;
const schema = new SchemaModel( const schema = new SchemaModel(
parser, parser,
// merge base schema into each of oneOf's subschemas // merge base schema into each of oneOf's subschemas