Skip to content

Commit 2a26cb7

Browse files
authored
Merge pull request ferdikoomen#1141 from markbrockhoff/add-model-postfix
Add model postfix
2 parents 4379538 + cf4f2f9 commit 2a26cb7

File tree

12 files changed

+107
-140
lines changed

12 files changed

+107
-140
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ $ openapi --help
4848
--exportModels <value> Write models to disk (default: true)
4949
--exportSchemas <value> Write schemas to disk (default: false)
5050
--indent <value> Indentation options [4, 2, tab] (default: "4")
51-
--postfix <value> Service name postfix (default: "Service")
51+
--postfixServices Service name postfix (default: "Service")
52+
--postfixModels Model name postfix
5253
--request <value> Path to custom request file
5354
-h, --help display help for command
5455

bin/index.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ const params = program
2121
.option('--exportModels <value>', 'Write models to disk', true)
2222
.option('--exportSchemas <value>', 'Write schemas to disk', false)
2323
.option('--indent <value>', 'Indentation options [4, 2, tabs]', '4')
24-
.option('--postfix <value>', 'Service name postfix', 'Service')
24+
.option('--postfix <value>', 'Deprecated: Use --postfixServices instead. Service name postfix', 'Service')
25+
.option('--postfixServices <value>', 'Service name postfix', 'Service')
26+
.option('--postfixModels <value>', 'Model name postfix')
2527
.option('--request <value>', 'Path to custom request file')
2628
.parse(process.argv)
2729
.opts();
@@ -41,7 +43,8 @@ if (OpenAPI) {
4143
exportModels: JSON.parse(params.exportModels) === true,
4244
exportSchemas: JSON.parse(params.exportSchemas) === true,
4345
indent: params.indent,
44-
postfix: params.postfix,
46+
postfixServices: params.postfixServices ?? params.postfix,
47+
postfixModels: params.postfixModels,
4548
request: params.request,
4649
})
4750
.then(() => {

bin/index.spec.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,10 @@ describe('bin', () => {
3434
'true',
3535
'--indent',
3636
'4',
37-
'--postfix',
37+
'--postfixServices',
3838
'Service',
39+
'--postfixModels',
40+
'Dto',
3941
]);
4042
expect(result.stdout.toString()).toBe('');
4143
expect(result.stderr.toString()).toBe('');
@@ -67,4 +69,18 @@ describe('bin', () => {
6769
expect(result.stdout.toString()).toContain(`-o, --output <value>`);
6870
expect(result.stderr.toString()).toBe('');
6971
});
72+
73+
it('should still support the deprecated --postfix parameter', () => {
74+
const result = crossSpawn.sync('node', [
75+
'./bin/index.js',
76+
'--input',
77+
'./test/spec/v3.json',
78+
'--output',
79+
'./test/generated/bin',
80+
'--postfix',
81+
'Service',
82+
]);
83+
expect(result.stdout.toString()).toBe('');
84+
expect(result.stderr.toString()).toBe('');
85+
});
7086
});

docs/basic-usage.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ $ openapi --help
1818
--exportModels <value> Write models to disk (default: true)
1919
--exportSchemas <value> Write schemas to disk (default: false)
2020
--indent <value> Indentation options [4, 2, tab] (default: "4")
21-
--postfix <value> Service name postfix (default: "Service")
21+
--postfixServices Service name postfix (default: "Service")
22+
--postfixModels Model name postfix
2223
--request <value> Path to custom request file
2324
-h, --help display help for command
2425

src/index.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ export type Options = {
2424
exportModels?: boolean;
2525
exportSchemas?: boolean;
2626
indent?: Indent;
27-
postfix?: string;
27+
postfixServices?: string;
28+
postfixModels?: string;
2829
request?: string;
2930
write?: boolean;
3031
};
@@ -44,7 +45,8 @@ export type Options = {
4445
* @param exportModels Generate models
4546
* @param exportSchemas Generate schemas
4647
* @param indent Indentation options (4, 2 or tab)
47-
* @param postfix Service name postfix
48+
* @param postfixServices Service name postfix
49+
* @param postfixModels Model name postfix
4850
* @param request Path to custom request file
4951
* @param write Write the files to disk (true or false)
5052
*/
@@ -60,7 +62,8 @@ export const generate = async ({
6062
exportModels = true,
6163
exportSchemas = false,
6264
indent = Indent.SPACE_4,
63-
postfix = 'Service',
65+
postfixServices = 'Service',
66+
postfixModels = '',
6467
request,
6568
write = true,
6669
}: Options): Promise<void> => {
@@ -89,7 +92,8 @@ export const generate = async ({
8992
exportModels,
9093
exportSchemas,
9194
indent,
92-
postfix,
95+
postfixServices,
96+
postfixModels,
9397
clientName,
9498
request
9599
);
@@ -112,7 +116,8 @@ export const generate = async ({
112116
exportModels,
113117
exportSchemas,
114118
indent,
115-
postfix,
119+
postfixServices,
120+
postfixModels,
116121
clientName,
117122
request
118123
);

src/templates/index.hbs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ export type { OpenAPIConfig } from './core/OpenAPI';
1818

1919
{{#each models}}
2020
{{#if @root.useUnionTypes}}
21-
export type { {{{name}}} } from './models/{{{name}}}';
21+
export type { {{{name}}}{{#if @root.postfixModels}} as {{{name}}}{{{@root.postfixModels}}}{{/if}} } from './models/{{{name}}}';
2222
{{else if enum}}
23-
export { {{{name}}} } from './models/{{{name}}}';
23+
export { {{{name}}}{{#if @root.postfixModels}} as {{{name}}}{{{@root.postfixModels}}}{{/if}} } from './models/{{{name}}}';
2424
{{else if enums}}
25-
export { {{{name}}} } from './models/{{{name}}}';
25+
export { {{{name}}}{{#if @root.postfixModels}} as {{{name}}}{{{@root.postfixModels}}}{{/if}} } from './models/{{{name}}}';
2626
{{else}}
27-
export type { {{{name}}} } from './models/{{{name}}}';
27+
export type { {{{name}}}{{#if @root.postfixModels}} as {{{name}}}{{{@root.postfixModels}}}{{/if}} } from './models/{{{name}}}';
2828
{{/if}}
2929
{{/each}}
3030
{{/if}}
@@ -41,7 +41,7 @@ export { ${{{name}}} } from './schemas/${{{name}}}';
4141
{{#if services}}
4242

4343
{{#each services}}
44-
export { {{{name}}}{{{@root.postfix}}} } from './services/{{{name}}}{{{@root.postfix}}}';
44+
export { {{{name}}}{{{@root.postfixServices}}} } from './services/{{{name}}}{{{@root.postfixServices}}}';
4545
{{/each}}
4646
{{/if}}
4747
{{/if}}

src/utils/writeClient.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ import { writeClientServices } from './writeClientServices';
2828
* @param exportSchemas Generate schemas
2929
* @param exportSchemas Generate schemas
3030
* @param indent Indentation options (4, 2 or tab)
31-
* @param postfix Service name postfix
31+
* @param postfixServices Service name postfix
32+
* @param postfixModels Model name postfix
3233
* @param clientName Custom client class name
3334
* @param request Path to custom request file
3435
*/
@@ -44,7 +45,8 @@ export const writeClient = async (
4445
exportModels: boolean,
4546
exportSchemas: boolean,
4647
indent: Indent,
47-
postfix: string,
48+
postfixServices: string,
49+
postfixModels: string,
4850
clientName?: string,
4951
request?: string
5052
): Promise<void> => {
@@ -75,7 +77,7 @@ export const writeClient = async (
7577
useUnionTypes,
7678
useOptions,
7779
indent,
78-
postfix,
80+
postfixServices,
7981
clientName
8082
);
8183
}
@@ -94,7 +96,7 @@ export const writeClient = async (
9496

9597
if (isDefined(clientName)) {
9698
await mkdir(outputPath);
97-
await writeClientClass(client, templates, outputPath, httpClient, clientName, indent, postfix);
99+
await writeClientClass(client, templates, outputPath, httpClient, clientName, indent, postfixServices);
98100
}
99101

100102
if (exportCore || exportServices || exportSchemas || exportModels) {
@@ -108,7 +110,8 @@ export const writeClient = async (
108110
exportServices,
109111
exportModels,
110112
exportSchemas,
111-
postfix,
113+
postfixServices,
114+
postfixModels,
112115
clientName
113116
);
114117
}

src/utils/writeClientIndex.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ describe('writeClientIndex', () => {
3434
},
3535
};
3636

37-
await writeClientIndex(client, templates, '/', true, true, true, true, true, 'Service');
37+
await writeClientIndex(client, templates, '/', true, true, true, true, true, 'Service', '');
3838

3939
expect(writeFile).toBeCalledWith('/index.ts', 'index');
4040
});

src/utils/writeClientIndex.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ import { sortServicesByName } from './sortServicesByName';
1919
* @param exportServices Generate services
2020
* @param exportModels Generate models
2121
* @param exportSchemas Generate schemas
22-
* @param postfix Service name postfix
22+
* @param postfixServices Service name postfix
23+
* @param postfixModels Model name postfix
2324
* @param clientName Custom client class name
2425
*/
2526
export const writeClientIndex = async (
@@ -31,7 +32,8 @@ export const writeClientIndex = async (
3132
exportServices: boolean,
3233
exportModels: boolean,
3334
exportSchemas: boolean,
34-
postfix: string,
35+
postfixServices: string,
36+
postfixModels: string,
3537
clientName?: string
3638
): Promise<void> => {
3739
const templateResult = templates.index({
@@ -40,7 +42,8 @@ export const writeClientIndex = async (
4042
exportModels,
4143
exportSchemas,
4244
useUnionTypes,
43-
postfix,
45+
postfixServices,
46+
postfixModels,
4447
clientName,
4548
server: client.server,
4649
version: client.version,

0 commit comments

Comments
 (0)