Skip to content

Commit a028773

Browse files
committed
fix: wired up nameConflictResolver and added tests and snapshots
1 parent 02a2cbd commit a028773

13 files changed

Lines changed: 369 additions & 5 deletions

File tree

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

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@ describe(`OpenAPI ${version}`, () => {
2121
version,
2222
typeof input === 'string' ? input : ((input?.path as string) ?? ''),
2323
);
24+
const output = userConfig.output instanceof Array ? userConfig.output[0] : userConfig.output;
25+
const outputPath = path.join(
26+
outputDir,
27+
typeof output === 'string' ? output : ((output?.path as string) ?? ''),
28+
);
29+
const nameConflictResolver =
30+
typeof output === 'string' ? undefined : output?.nameConflictResolver;
2431
return {
2532
plugins: ['@hey-api/typescript'],
2633
...userConfig,
@@ -34,7 +41,10 @@ describe(`OpenAPI ${version}`, () => {
3441
logs: {
3542
level: 'silent',
3643
},
37-
output: path.join(outputDir, typeof userConfig.output === 'string' ? userConfig.output : ''),
44+
output: {
45+
nameConflictResolver,
46+
path: outputPath,
47+
},
3848
} as const satisfies UserConfig;
3949
};
4050

@@ -263,6 +273,49 @@ describe(`OpenAPI ${version}`, () => {
263273
}),
264274
description: 'exports inline enums (TypeScript)',
265275
},
276+
{
277+
config: createConfig({
278+
input: 'enum-inline.json',
279+
output: {
280+
nameConflictResolver: ({ attempt, baseName }) =>
281+
attempt === 0 ? baseName : `${baseName}_N${attempt + 1}`,
282+
path: 'enum-inline-name-resolver',
283+
},
284+
parser: {
285+
transforms: {
286+
enums: 'root',
287+
},
288+
},
289+
plugins: [
290+
{
291+
enums: 'javascript',
292+
name: '@hey-api/typescript',
293+
},
294+
],
295+
}),
296+
description: 'exports inline enums with name conflict resolver',
297+
},
298+
{
299+
config: createConfig({
300+
input: 'enum-inline.json',
301+
output: {
302+
nameConflictResolver: () => null,
303+
path: 'enum-inline-name-resolver-null',
304+
},
305+
parser: {
306+
transforms: {
307+
enums: 'root',
308+
},
309+
},
310+
plugins: [
311+
{
312+
enums: 'javascript',
313+
name: '@hey-api/typescript',
314+
},
315+
],
316+
}),
317+
description: 'exports inline enums with name conflict resolver returning null',
318+
},
266319
{
267320
config: createConfig({
268321
input: 'enum-names-values.json',
@@ -673,7 +726,7 @@ describe(`OpenAPI ${version}`, () => {
673726
it.each(scenarios)('$description', async ({ config }) => {
674727
await createClient(config);
675728

676-
const filePaths = getFilePaths(config.output);
729+
const filePaths = getFilePaths(config.output.path);
677730

678731
await Promise.all(
679732
filePaths.map(async (filePath) => {

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

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ describe(`OpenAPI ${version}`, () => {
2222
typeof input === 'string' ? input : ((input?.path as string) ?? ''),
2323
);
2424
const output = userConfig.output instanceof Array ? userConfig.output[0] : userConfig.output;
25+
const outputPath = path.join(
26+
outputDir,
27+
typeof output === 'string' ? output : ((output?.path as string) ?? ''),
28+
);
29+
const nameConflictResolver =
30+
typeof output === 'string' ? undefined : output?.nameConflictResolver;
2531
return {
2632
plugins: ['@hey-api/typescript'],
2733
...userConfig,
@@ -35,7 +41,10 @@ describe(`OpenAPI ${version}`, () => {
3541
logs: {
3642
level: 'silent',
3743
},
38-
output: path.join(outputDir, typeof output === 'string' ? output : (output?.path ?? '')),
44+
output: {
45+
nameConflictResolver,
46+
path: outputPath,
47+
},
3948
} as const satisfies UserConfig;
4049
};
4150

@@ -297,6 +306,49 @@ describe(`OpenAPI ${version}`, () => {
297306
}),
298307
description: 'exports inline enums (TypeScript)',
299308
},
309+
{
310+
config: createConfig({
311+
input: 'enum-inline.yaml',
312+
output: {
313+
nameConflictResolver: ({ attempt, baseName }) =>
314+
attempt === 0 ? baseName : `${baseName}_N${attempt + 1}`,
315+
path: 'enum-inline-name-resolver',
316+
},
317+
parser: {
318+
transforms: {
319+
enums: 'root',
320+
},
321+
},
322+
plugins: [
323+
{
324+
enums: 'javascript',
325+
name: '@hey-api/typescript',
326+
},
327+
],
328+
}),
329+
description: 'exports inline enums with name conflict resolver',
330+
},
331+
{
332+
config: createConfig({
333+
input: 'enum-inline.yaml',
334+
output: {
335+
nameConflictResolver: () => null,
336+
path: 'enum-inline-name-resolver-null',
337+
},
338+
parser: {
339+
transforms: {
340+
enums: 'root',
341+
},
342+
},
343+
plugins: [
344+
{
345+
enums: 'javascript',
346+
name: '@hey-api/typescript',
347+
},
348+
],
349+
}),
350+
description: 'exports inline enums with name conflict resolver returning null',
351+
},
300352
{
301353
config: createConfig({
302354
input: 'enum-names-values.yaml',
@@ -991,7 +1043,7 @@ describe(`OpenAPI ${version}`, () => {
9911043
it.each(scenarios)('$description', async ({ config }) => {
9921044
await createClient(config);
9931045

994-
const filePaths = getFilePaths(config.output);
1046+
const filePaths = getFilePaths(config.output.path);
9951047

9961048
await Promise.all(
9971049
filePaths.map(async (filePath) => {
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// This file is auto-generated by @hey-api/openapi-ts
2+
3+
export { type ClientOptions, type Foo, TypeEnum } 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 ClientOptions = {
4+
baseUrl: `${string}://${string}` | (string & {});
5+
};
6+
7+
export type Foo = {
8+
type?: TypeEnum;
9+
};
10+
11+
export const TypeEnum = {
12+
FOO: 'foo',
13+
BAR: 'bar',
14+
FOO_BAR: 'FooBar',
15+
FOO_BAR2: 'fooBar',
16+
FOO_BAR3: 'foo bar'
17+
} as const;
18+
19+
export type TypeEnum = typeof TypeEnum[keyof typeof TypeEnum];
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// This file is auto-generated by @hey-api/openapi-ts
2+
3+
export { type ClientOptions, type Foo, TypeEnum } 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 ClientOptions = {
4+
baseUrl: `${string}://${string}` | (string & {});
5+
};
6+
7+
export type Foo = {
8+
type?: TypeEnum;
9+
};
10+
11+
export const TypeEnum = {
12+
FOO: 'foo',
13+
BAR: 'bar',
14+
FOO_BAR: 'FooBar',
15+
FOO_BAR_N2: 'fooBar',
16+
FOO_BAR_N3: 'foo bar'
17+
} as const;
18+
19+
export type TypeEnum = typeof TypeEnum[keyof typeof TypeEnum];
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// This file is auto-generated by @hey-api/openapi-ts
2+
3+
export { type ClientOptions, type Foo, TypeEnum } 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 ClientOptions = {
4+
baseUrl: `${string}://${string}` | (string & {});
5+
};
6+
7+
export type Foo = {
8+
type?: TypeEnum;
9+
};
10+
11+
export const TypeEnum = {
12+
FOO: 'foo',
13+
BAR: 'bar',
14+
FOO_BAR: 'FooBar',
15+
FOO_BAR2: 'fooBar',
16+
FOO_BAR3: 'foo bar'
17+
} as const;
18+
19+
export type TypeEnum = typeof TypeEnum[keyof typeof TypeEnum];
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// This file is auto-generated by @hey-api/openapi-ts
2+
3+
export { type Bar, Baz, type ClientOptions, type Foo, FooEnum, FooEnum2, type GetFooData, type GetFooResponse, type GetFooResponses, type PostFooData, type PostFooResponse, type PostFooResponses, type PutFooData, type PutFooResponse, type PutFooResponses, TypeEnum } from './types.gen';
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
// This file is auto-generated by @hey-api/openapi-ts
2+
3+
export type ClientOptions = {
4+
baseUrl: `${string}://${string}` | (string & {});
5+
};
6+
7+
export type Foo = {
8+
type?: TypeEnum;
9+
};
10+
11+
export type Bar = {
12+
type?: Baz;
13+
};
14+
15+
export const Baz = { QUX: 'qux', QUUX: 'quux' } as const;
16+
17+
export type Baz = typeof Baz[keyof typeof Baz];
18+
19+
export const FooEnum = {
20+
FOO: 'foo',
21+
BAR: 'bar',
22+
FOO_BAR: 'FooBar',
23+
FOO_BAR2: 'fooBar',
24+
FOO_BAR3: 'foo bar'
25+
} as const;
26+
27+
export type FooEnum = typeof FooEnum[keyof typeof FooEnum];
28+
29+
export const FooEnum2 = { BAZ: 'baz' } as const;
30+
31+
export type FooEnum2 = typeof FooEnum2[keyof typeof FooEnum2];
32+
33+
export const TypeEnum = { FOO: 'foo', BAR: 'bar' } as const;
34+
35+
export type TypeEnum = typeof TypeEnum[keyof typeof TypeEnum];
36+
37+
export type GetFooData = {
38+
body?: never;
39+
path?: never;
40+
query?: never;
41+
url: '/foo';
42+
};
43+
44+
export type GetFooResponses = {
45+
/**
46+
* OK
47+
*/
48+
200: {
49+
foo?: FooEnum;
50+
};
51+
};
52+
53+
export type GetFooResponse = GetFooResponses[keyof GetFooResponses];
54+
55+
export type PostFooData = {
56+
body?: never;
57+
path?: never;
58+
query?: never;
59+
url: '/foo';
60+
};
61+
62+
export type PostFooResponses = {
63+
/**
64+
* OK
65+
*/
66+
200: {
67+
foo?: FooEnum2;
68+
};
69+
};
70+
71+
export type PostFooResponse = PostFooResponses[keyof PostFooResponses];
72+
73+
export type PutFooData = {
74+
body?: never;
75+
path?: never;
76+
query?: never;
77+
url: '/foo';
78+
};
79+
80+
export type PutFooResponses = {
81+
/**
82+
* OK
83+
*/
84+
200: Baz;
85+
};
86+
87+
export type PutFooResponse = PutFooResponses[keyof PutFooResponses];

0 commit comments

Comments
 (0)