Skip to content

Commit

Permalink
fix: schema oneOf title with const (Redocly#2350)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexVarchuk authored and ckoegel committed Nov 1, 2023
1 parent 64dd225 commit dd3714d
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 2 deletions.
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

0 comments on commit dd3714d

Please sign in to comment.