Skip to content

Commit 4386867

Browse files
authored
fix: schema oneOf title with const (#2350)
1 parent 7e05202 commit 4386867

File tree

2 files changed

+57
-2
lines changed

2 files changed

+57
-2
lines changed

src/services/__tests__/models/Schema.test.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,5 +562,59 @@ describe('Models', () => {
562562
`"testAttr: <object> (Refed description)"`,
563563
);
564564
});
565+
566+
test('should correct get title from in oneOf ->const', () => {
567+
const spec = parseYaml(outdent`
568+
openapi: 3.0.0
569+
paths:
570+
/test:
571+
get:
572+
operationId: test
573+
responses:
574+
'200':
575+
content:
576+
application/json:
577+
schema:
578+
type: object
579+
properties:
580+
data:
581+
type: object
582+
properties:
583+
response_code:
584+
type: integer
585+
description: A numeric response code
586+
oneOf:
587+
- const: 0
588+
description: >
589+
Description for const 0
590+
- const: 1
591+
description: >
592+
Description for const 1
593+
- const: 2
594+
description: >
595+
Description for const 2
596+
`) as any;
597+
598+
parser = new OpenAPIParser(spec, undefined, opts);
599+
const name = 'application/json';
600+
const mediaType = new MediaTypeModel(
601+
parser,
602+
name,
603+
true,
604+
spec.paths['/test'].get.responses['200'].content[name],
605+
opts,
606+
);
607+
608+
expect(printSchema(mediaType?.schema as any)).toMatchInlineSnapshot(`
609+
"data:
610+
response_code: oneOf
611+
0 -> <integer> (Description for const 0
612+
)
613+
1 -> <integer> (Description for const 1
614+
)
615+
2 -> <integer> (Description for const 2
616+
)"
617+
`);
618+
});
565619
});
566620
});

src/services/models/Schema.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,8 +243,9 @@ export class SchemaModel {
243243
const title =
244244
isNamedDefinition(variant.$ref) && !merged.title
245245
? JsonPointer.baseName(variant.$ref)
246-
: `${merged.title || ''}${(merged.const && JSON.stringify(merged.const)) || ''}`;
247-
246+
: `${merged.title || ''}${
247+
(typeof merged.const !== 'undefined' && JSON.stringify(merged.const)) || ''
248+
}`;
248249
const schema = new SchemaModel(
249250
parser,
250251
// merge base schema into each of oneOf's subschemas

0 commit comments

Comments
 (0)