From 2ff484045a123e88b9c7145e56e23c2aebee83ac Mon Sep 17 00:00:00 2001 From: Konstantin Barabanov Date: Thu, 17 Sep 2020 18:53:39 +0300 Subject: [PATCH 1/2] Added allFieldsRequired flag for cases when you don't need optional fields (using nulls only) --- bin/index.js | 2 ++ src/index.ts | 7 +++++-- src/templates/partials/isRequired.hbs | 5 ++++- src/utils/writeClient.ts | 6 ++++-- src/utils/writeClientModels.ts | 7 +++++-- 5 files changed, 20 insertions(+), 7 deletions(-) diff --git a/bin/index.js b/bin/index.js index aa2cfbed8..4ebf31f05 100755 --- a/bin/index.js +++ b/bin/index.js @@ -17,6 +17,7 @@ program .option('--exportServices ', 'Generate services', true) .option('--exportModels ', 'Generate models', true) .option('--exportSchemas ', 'Generate schemas', false) + .option('--allFieldsRequired', 'Set all response required (key:? val -> key: val)') .parse(process.argv); const OpenAPI = require(path.resolve(__dirname, '../dist/index.js')); @@ -32,6 +33,7 @@ if (OpenAPI) { exportServices: JSON.parse(program.exportServices) === true, exportModels: JSON.parse(program.exportModels) === true, exportSchemas: JSON.parse(program.exportSchemas) === true, + allFieldsRequired: program.allFieldsRequired, }) .then(() => { process.exit(0); diff --git a/src/index.ts b/src/index.ts index 191183596..6576700e5 100644 --- a/src/index.ts +++ b/src/index.ts @@ -22,6 +22,7 @@ export interface Options { exportServices?: boolean; exportModels?: boolean; exportSchemas?: boolean; + allFieldsRequired?: boolean; write?: boolean; } @@ -38,6 +39,7 @@ export interface Options { * @param exportServices: Generate services. * @param exportModels: Generate models. * @param exportSchemas: Generate schemas. + * @param allFieldsRequired Set all fields required (key:? val -> key: val). * @param write Write the files to disk (true or false). */ export async function generate({ @@ -50,6 +52,7 @@ export async function generate({ exportServices = true, exportModels = true, exportSchemas = false, + allFieldsRequired = false, write = true, }: Options): Promise { // Load the specification, read the OpenAPI version and load the @@ -63,7 +66,7 @@ export async function generate({ const client = parseV2(openApi); const clientFinal = postProcessClient(client, useUnionTypes); if (write) { - await writeClient(clientFinal, templates, output, httpClient, useOptions, exportCore, exportServices, exportModels, exportSchemas); + await writeClient(clientFinal, templates, output, httpClient, useOptions, exportCore, exportServices, exportModels, exportSchemas, allFieldsRequired); } break; } @@ -72,7 +75,7 @@ export async function generate({ const client = parseV3(openApi); const clientFinal = postProcessClient(client, useUnionTypes); if (write) { - await writeClient(clientFinal, templates, output, httpClient, useOptions, exportCore, exportServices, exportModels, exportSchemas); + await writeClient(clientFinal, templates, output, httpClient, useOptions, exportCore, exportServices, exportModels, exportSchemas, allFieldsRequired); } break; } diff --git a/src/templates/partials/isRequired.hbs b/src/templates/partials/isRequired.hbs index dbecae99c..00ff9f394 100644 --- a/src/templates/partials/isRequired.hbs +++ b/src/templates/partials/isRequired.hbs @@ -1,5 +1,8 @@ +{{#if @root.allFieldsRequired}} +{{else}} {{#if @root.useOptions}} -{{~#unless isRequired}}?{{else if default}}?{{/unless~}} +{{~#unless isRequired}}?{{else if default}}{{else if default}}?{{/unless~}} {{else}} {{~#unless isRequired}}{{#unless default}}?{{/unless}}{{/unless~}} {{/if}} +{{/if}} diff --git a/src/utils/writeClient.ts b/src/utils/writeClient.ts index fc607e4a1..caa440aee 100644 --- a/src/utils/writeClient.ts +++ b/src/utils/writeClient.ts @@ -25,6 +25,7 @@ async function copySupportFile(filePath: string, outputPath: string): Promise { const outputPath = path.resolve(process.cwd(), output); const outputPathCore = path.resolve(outputPath, 'core'); @@ -74,7 +76,7 @@ export async function writeClient( if (exportModels) { await mkdir(outputPathModels); await copySupportFile('models/Dictionary.ts', outputPath); - await writeClientModels(client.models, templates, outputPathModels); + await writeClientModels(client.models, templates, outputPathModels, allFieldsRequired); } await writeClientIndex(client, templates, outputPath, exportCore, exportServices, exportModels, exportSchemas); diff --git a/src/utils/writeClientModels.ts b/src/utils/writeClientModels.ts index 49697e60f..0831a98cf 100644 --- a/src/utils/writeClientModels.ts +++ b/src/utils/writeClientModels.ts @@ -11,10 +11,13 @@ import { Templates } from './registerHandlebarTemplates'; * @param templates The loaded handlebar templates. * @param outputPath Directory to write the generated files to. */ -export async function writeClientModels(models: Model[], templates: Templates, outputPath: string): Promise { +export async function writeClientModels(models: Model[], templates: Templates, outputPath: string, allFieldsRequired: boolean): Promise { for (const model of models) { const file = path.resolve(outputPath, `${model.name}.ts`); - const templateResult = templates.model(model); + const templateResult = templates.model({ + ...model, + allFieldsRequired, + }); await writeFile(file, format(templateResult)); } } From 4ef05d910aa93083c36ffbb817f5fde263bff822 Mon Sep 17 00:00:00 2001 From: Konstantin Barabanov Date: Thu, 17 Sep 2020 19:06:52 +0300 Subject: [PATCH 2/2] web-bee-ru fork initial commit --- package.json | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index 7fa25fa95..f03d7bcc9 100644 --- a/package.json +++ b/package.json @@ -1,15 +1,15 @@ { - "name": "openapi-typescript-codegen", - "version": "0.4.11", + "name": "@web-bee-ru/openapi-typescript-codegen", + "version": "0.4.12-rc.1", "description": "NodeJS library that generates Typescript or Javascript clients based on the OpenAPI specification.", "author": "Ferdi Koomen", - "homepage": "https://github.com/ferdikoomen/openapi-typescript-codegen", + "homepage": "https://github.com/web-bee-ru/openapi-typescript-codegen", "repository": { "type": "git", - "url": "git+https://github.com/ferdikoomen/openapi-typescript-codegen.git" + "url": "git+https://github.com/web-bee-ru/openapi-typescript-codegen.git" }, "bugs": { - "url": "https://github.com/ferdikoomen/openapi-typescript-codegen/issues" + "url": "https://github.com/web-bee-ru/openapi-typescript-codegen/issues" }, "license": "MIT", "keywords": [