Skip to content

Commit 2ff4840

Browse files
committed
Added allFieldsRequired flag for cases when you don't need optional fields (using nulls only)
1 parent fbcde03 commit 2ff4840

File tree

5 files changed

+20
-7
lines changed

5 files changed

+20
-7
lines changed

bin/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ program
1717
.option('--exportServices <value>', 'Generate services', true)
1818
.option('--exportModels <value>', 'Generate models', true)
1919
.option('--exportSchemas <value>', 'Generate schemas', false)
20+
.option('--allFieldsRequired', 'Set all response required (key:? val -> key: val)')
2021
.parse(process.argv);
2122

2223
const OpenAPI = require(path.resolve(__dirname, '../dist/index.js'));
@@ -32,6 +33,7 @@ if (OpenAPI) {
3233
exportServices: JSON.parse(program.exportServices) === true,
3334
exportModels: JSON.parse(program.exportModels) === true,
3435
exportSchemas: JSON.parse(program.exportSchemas) === true,
36+
allFieldsRequired: program.allFieldsRequired,
3537
})
3638
.then(() => {
3739
process.exit(0);

src/index.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export interface Options {
2222
exportServices?: boolean;
2323
exportModels?: boolean;
2424
exportSchemas?: boolean;
25+
allFieldsRequired?: boolean;
2526
write?: boolean;
2627
}
2728

@@ -38,6 +39,7 @@ export interface Options {
3839
* @param exportServices: Generate services.
3940
* @param exportModels: Generate models.
4041
* @param exportSchemas: Generate schemas.
42+
* @param allFieldsRequired Set all fields required (key:? val -> key: val).
4143
* @param write Write the files to disk (true or false).
4244
*/
4345
export async function generate({
@@ -50,6 +52,7 @@ export async function generate({
5052
exportServices = true,
5153
exportModels = true,
5254
exportSchemas = false,
55+
allFieldsRequired = false,
5356
write = true,
5457
}: Options): Promise<void> {
5558
// Load the specification, read the OpenAPI version and load the
@@ -63,7 +66,7 @@ export async function generate({
6366
const client = parseV2(openApi);
6467
const clientFinal = postProcessClient(client, useUnionTypes);
6568
if (write) {
66-
await writeClient(clientFinal, templates, output, httpClient, useOptions, exportCore, exportServices, exportModels, exportSchemas);
69+
await writeClient(clientFinal, templates, output, httpClient, useOptions, exportCore, exportServices, exportModels, exportSchemas, allFieldsRequired);
6770
}
6871
break;
6972
}
@@ -72,7 +75,7 @@ export async function generate({
7275
const client = parseV3(openApi);
7376
const clientFinal = postProcessClient(client, useUnionTypes);
7477
if (write) {
75-
await writeClient(clientFinal, templates, output, httpClient, useOptions, exportCore, exportServices, exportModels, exportSchemas);
78+
await writeClient(clientFinal, templates, output, httpClient, useOptions, exportCore, exportServices, exportModels, exportSchemas, allFieldsRequired);
7679
}
7780
break;
7881
}

src/templates/partials/isRequired.hbs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
{{#if @root.allFieldsRequired}}
2+
{{else}}
13
{{#if @root.useOptions}}
2-
{{~#unless isRequired}}?{{else if default}}?{{/unless~}}
4+
{{~#unless isRequired}}?{{else if default}}{{else if default}}?{{/unless~}}
35
{{else}}
46
{{~#unless isRequired}}{{#unless default}}?{{/unless}}{{/unless~}}
57
{{/if}}
8+
{{/if}}

src/utils/writeClient.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ async function copySupportFile(filePath: string, outputPath: string): Promise<vo
2525
* @param exportServices: Generate services.
2626
* @param exportModels: Generate models.
2727
* @param exportSchemas: Generate schemas.
28+
* @param allFieldsRequired: Require all response fields.
2829
*/
2930
export async function writeClient(
3031
client: Client,
@@ -35,7 +36,8 @@ export async function writeClient(
3536
exportCore: boolean,
3637
exportServices: boolean,
3738
exportModels: boolean,
38-
exportSchemas: boolean
39+
exportSchemas: boolean,
40+
allFieldsRequired: boolean
3941
): Promise<void> {
4042
const outputPath = path.resolve(process.cwd(), output);
4143
const outputPathCore = path.resolve(outputPath, 'core');
@@ -74,7 +76,7 @@ export async function writeClient(
7476
if (exportModels) {
7577
await mkdir(outputPathModels);
7678
await copySupportFile('models/Dictionary.ts', outputPath);
77-
await writeClientModels(client.models, templates, outputPathModels);
79+
await writeClientModels(client.models, templates, outputPathModels, allFieldsRequired);
7880
}
7981

8082
await writeClientIndex(client, templates, outputPath, exportCore, exportServices, exportModels, exportSchemas);

src/utils/writeClientModels.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,13 @@ import { Templates } from './registerHandlebarTemplates';
1111
* @param templates The loaded handlebar templates.
1212
* @param outputPath Directory to write the generated files to.
1313
*/
14-
export async function writeClientModels(models: Model[], templates: Templates, outputPath: string): Promise<void> {
14+
export async function writeClientModels(models: Model[], templates: Templates, outputPath: string, allFieldsRequired: boolean): Promise<void> {
1515
for (const model of models) {
1616
const file = path.resolve(outputPath, `${model.name}.ts`);
17-
const templateResult = templates.model(model);
17+
const templateResult = templates.model({
18+
...model,
19+
allFieldsRequired,
20+
});
1821
await writeFile(file, format(templateResult));
1922
}
2023
}

0 commit comments

Comments
 (0)