Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: schema oneOf title with const #2350

Merged
merged 1 commit into from
Jul 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions src/services/__tests__/models/Schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -562,5 +562,59 @@ describe('Models', () => {
`"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
)"
`);
});
});
});
5 changes: 3 additions & 2 deletions src/services/models/Schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,9 @@ export class SchemaModel {
const title =
isNamedDefinition(variant.$ref) && !merged.title
? 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(
parser,
// merge base schema into each of oneOf's subschemas
Expand Down