Skip to content

Commit dbebff8

Browse files
committed
fix: handle discriminator with multiple mappings to the same schema
1 parent e62235d commit dbebff8

File tree

14 files changed

+260
-50
lines changed

14 files changed

+260
-50
lines changed

.changeset/twenty-gifts-battle.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@hey-api/openapi-ts': patch
3+
---
4+
5+
fix: handle discriminator with multiple mappings to the same schema

packages/openapi-ts-tests/test/3.0.x.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,13 @@ describe(`OpenAPI ${version}`, () => {
166166
}),
167167
description: 'handles discriminator with and without mapping',
168168
},
169+
{
170+
config: createConfig({
171+
input: 'discriminator-mapped-many.yaml',
172+
output: 'discriminator-mapped-many',
173+
}),
174+
description: 'handles discriminator with multiple mappings',
175+
},
169176
{
170177
config: createConfig({
171178
input: 'discriminator-one-of.yaml',

packages/openapi-ts-tests/test/3.1.x.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,13 @@ describe(`OpenAPI ${version}`, () => {
173173
}),
174174
description: 'handles discriminator with and without mapping',
175175
},
176+
{
177+
config: createConfig({
178+
input: 'discriminator-mapped-many.yaml',
179+
output: 'discriminator-mapped-many',
180+
}),
181+
description: 'handles discriminator with multiple mappings',
182+
},
176183
{
177184
config: createConfig({
178185
input: 'discriminator-one-of.yaml',
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// This file is auto-generated by @hey-api/openapi-ts
2+
export * from './types.gen';
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// This file is auto-generated by @hey-api/openapi-ts
2+
3+
export type Foo = ({
4+
foo?: 'one' | 'two';
5+
} & Bar) | ({
6+
foo?: 'three';
7+
} & Baz);
8+
9+
export type Bar = {
10+
foo?: 'one' | 'two';
11+
};
12+
13+
export type Baz = {
14+
foo?: 'three';
15+
};
16+
17+
export type ClientOptions = {
18+
baseUrl: `${string}://${string}` | (string & {});
19+
};
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// This file is auto-generated by @hey-api/openapi-ts
2+
export * from './types.gen';
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// This file is auto-generated by @hey-api/openapi-ts
2+
3+
export type Foo = ({
4+
foo?: 'one' | 'two';
5+
} & Bar) | ({
6+
foo?: 'three';
7+
} & Baz);
8+
9+
export type Bar = {
10+
foo?: 'one' | 'two';
11+
};
12+
13+
export type Baz = {
14+
foo?: 'three';
15+
};
16+
17+
export type ClientOptions = {
18+
baseUrl: `${string}://${string}` | (string & {});
19+
};

packages/openapi-ts-tests/test/openapi-ts.config.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,12 @@ export default defineConfig(() => {
3636
// openapi: '3.1.0',
3737
// paths: {},
3838
// },
39-
path: path.resolve(__dirname, 'spec', '3.1.x', 'enum-null.json'),
39+
path: path.resolve(
40+
__dirname,
41+
'spec',
42+
'3.1.x',
43+
'discriminator-mapped-many.yaml',
44+
),
4045
// path: 'http://localhost:4000/',
4146
// path: 'https://get.heyapi.dev/',
4247
// path: 'https://get.heyapi.dev/hey-api/backend?branch=main&version=1.0.0',
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
openapi: 3.0.0
2+
info:
3+
title: OpenAPI 3.0.0 discriminator mapped many example
4+
version: 1
5+
components:
6+
schemas:
7+
Foo:
8+
oneOf:
9+
- $ref: '#/components/schemas/Bar'
10+
- $ref: '#/components/schemas/Baz'
11+
discriminator:
12+
propertyName: foo
13+
mapping:
14+
one: '#/components/schemas/Bar'
15+
two: '#/components/schemas/Bar'
16+
three: '#/components/schemas/Baz'
17+
Bar:
18+
type: object
19+
properties:
20+
foo:
21+
type: string
22+
enum:
23+
- one
24+
- two
25+
Baz:
26+
type: object
27+
properties:
28+
foo:
29+
type: string
30+
enum:
31+
- three
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
openapi: 3.1.0
2+
info:
3+
title: OpenAPI 3.1.0 discriminator mapped many example
4+
version: 1
5+
components:
6+
schemas:
7+
Foo:
8+
oneOf:
9+
- $ref: '#/components/schemas/Bar'
10+
- $ref: '#/components/schemas/Baz'
11+
discriminator:
12+
propertyName: foo
13+
mapping:
14+
one: '#/components/schemas/Bar'
15+
two: '#/components/schemas/Bar'
16+
three: '#/components/schemas/Baz'
17+
Bar:
18+
type: object
19+
properties:
20+
foo:
21+
type: string
22+
enum:
23+
- one
24+
- two
25+
Baz:
26+
type: object
27+
properties:
28+
foo:
29+
type: string
30+
enum:
31+
- three

0 commit comments

Comments
 (0)