Skip to content

Commit b3ba14a

Browse files
committed
- Fixed enum generation for new composition types
1 parent 834ae43 commit b3ba14a

File tree

9 files changed

+61
-9
lines changed

9 files changed

+61
-9
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "openapi-typescript-codegen",
3-
"version": "0.7.0-beta-2",
3+
"version": "0.7.0-beta-3",
44
"description": "Library that generates Typescript clients based on the OpenAPI specification.",
55
"author": "Ferdi Koomen",
66
"homepage": "https://github.com/ferdikoomen/openapi-typescript-codegen",

src/templates/exportModel.hbs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ import type { {{{this}}} } from './{{{this}}}';
99

1010
{{#equals export 'interface'}}
1111
{{>exportInterface}}
12+
{{else equals export 'one-of'}}
13+
{{>exportComposition}}
14+
{{else equals export 'any-of'}}
15+
{{>exportComposition}}
16+
{{else equals export 'all-of'}}
17+
{{>exportComposition}}
1218
{{else equals export 'enum'}}
1319
{{>exportEnum}}
1420
{{else}}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{{#if description}}
2+
/**
3+
* {{{description}}}
4+
*/
5+
{{/if}}
6+
export type {{{name}}} = {{>type parent=name}};
7+
{{#if enums}}
8+
{{#unless @root.useUnionTypes}}
9+
10+
export namespace {{{name}}} {
11+
12+
{{#each enums}}
13+
{{#if description}}
14+
/**
15+
* {{{description}}}
16+
*/
17+
{{/if}}
18+
export enum {{{name}}} {
19+
{{#each enum}}
20+
{{{name}}} = {{{value}}},
21+
{{/each}}
22+
}
23+
24+
{{/each}}
25+
26+
}
27+
{{/unless}}
28+
{{/if}}

src/templates/partials/typeInterface.hbs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@
66
* {{{description}}}
77
*/
88
{{/if}}
9+
{{#if ../parent}}
10+
{{>isReadOnly}}{{{name}}}{{>isRequired}}: {{>type parent=../parent}},
11+
{{else}}
912
{{>isReadOnly}}{{{name}}}{{>isRequired}}: {{>type}},
13+
{{/if}}
1014
{{/each}}
1115
}{{>isNullable}}
1216
{{~else~}}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1+
{{~#if parent~}}
2+
({{#each properties}}{{>type parent=../parent}}{{#unless @last}} & {{/unless}}{{/each}}){{>isNullable}}
3+
{{~else~}}
14
({{#each properties}}{{>type}}{{#unless @last}} & {{/unless}}{{/each}}){{>isNullable}}
5+
{{~/if~}}

src/templates/partials/typeUnion.hbs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1+
{{~#if parent~}}
2+
({{#each properties}}{{>type parent=../parent}}{{#unless @last}} | {{/unless}}{{/each}}){{>isNullable}}
3+
{{~else~}}
14
({{#each properties}}{{>type}}{{#unless @last}} | {{/unless}}{{/each}}){{>isNullable}}
5+
{{~/if~}}

src/utils/registerHandlebarTemplates.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import templateExportSchema from '../templates/exportSchema.hbs';
3939
import templateExportService from '../templates/exportService.hbs';
4040
import templateIndex from '../templates/index.hbs';
4141
import partialBase from '../templates/partials/base.hbs';
42+
import partialExportComposition from '../templates/partials/exportComposition.hbs';
4243
import partialExportEnum from '../templates/partials/exportEnum.hbs';
4344
import partialExportInterface from '../templates/partials/exportInterface.hbs';
4445
import partialExportType from '../templates/partials/exportType.hbs';
@@ -109,6 +110,7 @@ export function registerHandlebarTemplates(): Templates {
109110
// Partials for the generations of the models, services, etc.
110111
Handlebars.registerPartial('exportEnum', Handlebars.template(partialExportEnum));
111112
Handlebars.registerPartial('exportInterface', Handlebars.template(partialExportInterface));
113+
Handlebars.registerPartial('exportComposition', Handlebars.template(partialExportComposition));
112114
Handlebars.registerPartial('exportType', Handlebars.template(partialExportType));
113115
Handlebars.registerPartial('header', Handlebars.template(partialHeader));
114116
Handlebars.registerPartial('isNullable', Handlebars.template(partialIsNullable));

test/__snapshots__/index.spec.js.snap

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,8 @@ import type { ModelWithString } from './ModelWithString';
619619
export type ModelThatExtends = (ModelWithString & {
620620
propExtendsA?: string,
621621
propExtendsB?: ModelWithString,
622-
});"
622+
});
623+
"
623624
`;
624625

625626
exports[`v2 should generate: ./test/generated/v2/models/ModelThatExtendsExtends.ts 1`] = `
@@ -636,7 +637,8 @@ import type { ModelWithString } from './ModelWithString';
636637
export type ModelThatExtendsExtends = (ModelWithString & ModelThatExtends & {
637638
propExtendsC?: string,
638639
propExtendsD?: ModelWithString,
639-
});"
640+
});
641+
"
640642
`;
641643

642644
exports[`v2 should generate: ./test/generated/v2/models/ModelWithArray.ts 1`] = `
@@ -2992,7 +2994,8 @@ import type { ModelWithString } from './ModelWithString';
29922994
export type ModelThatExtends = (ModelWithString & {
29932995
propExtendsA?: string,
29942996
propExtendsB?: ModelWithString,
2995-
});"
2997+
});
2998+
"
29962999
`;
29973000

29983001
exports[`v3 should generate: ./test/generated/v3/models/ModelThatExtendsExtends.ts 1`] = `
@@ -3009,7 +3012,8 @@ import type { ModelWithString } from './ModelWithString';
30093012
export type ModelThatExtendsExtends = (ModelWithString & ModelThatExtends & {
30103013
propExtendsC?: string,
30113014
propExtendsD?: ModelWithString,
3012-
});"
3015+
});
3016+
"
30133017
`;
30143018

30153019
exports[`v3 should generate: ./test/generated/v3/models/ModelWithArray.ts 1`] = `

test/index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ async function generateV2() {
77
input: './test/spec/v2.json',
88
output: './test/generated/v2/',
99
httpClient: OpenAPI.HttpClient.FETCH,
10-
useOptions: true,
11-
useUnionTypes: true,
10+
useOptions: false,
11+
useUnionTypes: false,
1212
exportCore: true,
1313
exportSchemas: true,
1414
exportModels: true,
@@ -21,8 +21,8 @@ async function generateV3() {
2121
input: './test/spec/v3.json',
2222
output: './test/generated/v3/',
2323
httpClient: OpenAPI.HttpClient.FETCH,
24-
useOptions: true,
25-
useUnionTypes: true,
24+
useOptions: false,
25+
useUnionTypes: false,
2626
exportCore: true,
2727
exportSchemas: true,
2828
exportModels: true,

0 commit comments

Comments
 (0)