From de7b32c391086b9f329ee6287228adda47c5fa58 Mon Sep 17 00:00:00 2001 From: Ferdi Koomen Date: Sat, 23 May 2020 18:30:14 +0200 Subject: [PATCH] - Fixed prefix value for enums to align with other generators --- src/openApi/v2/parser/getEnum.ts | 4 +-- .../v2/parser/getEnumFromDescription.ts | 2 +- src/openApi/v3/parser/getEnum.ts | 4 +-- .../v3/parser/getEnumFromDescription.ts | 2 +- test/__snapshots__/index.spec.js.snap | 36 +++++++++---------- test/mock/README.md | 11 ++++++ test/mock/codegen.sh | 9 +++++ 7 files changed, 44 insertions(+), 24 deletions(-) create mode 100644 test/mock/README.md create mode 100755 test/mock/codegen.sh diff --git a/src/openApi/v2/parser/getEnum.ts b/src/openApi/v2/parser/getEnum.ts index 858c7ef3..eb4469c1 100644 --- a/src/openApi/v2/parser/getEnum.ts +++ b/src/openApi/v2/parser/getEnum.ts @@ -10,7 +10,7 @@ export function getEnum(values?: (string | number)[]): Enum[] { .map(value => { if (typeof value === 'number') { return { - name: `$${value}`, + name: `_${value}`, value: String(value), type: PrimaryType.NUMBER, description: null, @@ -19,7 +19,7 @@ export function getEnum(values?: (string | number)[]): Enum[] { return { name: value .replace(/\W+/g, '_') - .replace(/^(\d+)/g, s => `$${s}`) + .replace(/^(\d+)/g, '_$1') .replace(/([a-z])([A-Z]+)/g, '$1_$2') .toUpperCase(), value: `'${value}'`, diff --git a/src/openApi/v2/parser/getEnumFromDescription.ts b/src/openApi/v2/parser/getEnumFromDescription.ts index 6020239f..45f35d43 100644 --- a/src/openApi/v2/parser/getEnumFromDescription.ts +++ b/src/openApi/v2/parser/getEnumFromDescription.ts @@ -16,7 +16,7 @@ export function getEnumFromDescription(description: string): Enum[] { symbols.push({ name: name .replace(/\W+/g, '_') - .replace(/^(\d+)/g, s => `$${s}`) + .replace(/^(\d+)/g, '_$1') .replace(/([a-z])([A-Z]+)/g, '$1_$2') .toUpperCase(), value: String(value), diff --git a/src/openApi/v3/parser/getEnum.ts b/src/openApi/v3/parser/getEnum.ts index 858c7ef3..eb4469c1 100644 --- a/src/openApi/v3/parser/getEnum.ts +++ b/src/openApi/v3/parser/getEnum.ts @@ -10,7 +10,7 @@ export function getEnum(values?: (string | number)[]): Enum[] { .map(value => { if (typeof value === 'number') { return { - name: `$${value}`, + name: `_${value}`, value: String(value), type: PrimaryType.NUMBER, description: null, @@ -19,7 +19,7 @@ export function getEnum(values?: (string | number)[]): Enum[] { return { name: value .replace(/\W+/g, '_') - .replace(/^(\d+)/g, s => `$${s}`) + .replace(/^(\d+)/g, '_$1') .replace(/([a-z])([A-Z]+)/g, '$1_$2') .toUpperCase(), value: `'${value}'`, diff --git a/src/openApi/v3/parser/getEnumFromDescription.ts b/src/openApi/v3/parser/getEnumFromDescription.ts index 6020239f..45f35d43 100644 --- a/src/openApi/v3/parser/getEnumFromDescription.ts +++ b/src/openApi/v3/parser/getEnumFromDescription.ts @@ -16,7 +16,7 @@ export function getEnumFromDescription(description: string): Enum[] { symbols.push({ name: name .replace(/\W+/g, '_') - .replace(/^(\d+)/g, s => `$${s}`) + .replace(/^(\d+)/g, '_$1') .replace(/([a-z])([A-Z]+)/g, '$1_$2') .toUpperCase(), value: String(value), diff --git a/test/__snapshots__/index.spec.js.snap b/test/__snapshots__/index.spec.js.snap index bef686a9..ffce43b0 100644 --- a/test/__snapshots__/index.spec.js.snap +++ b/test/__snapshots__/index.spec.js.snap @@ -791,9 +791,9 @@ exports[`generation v2 file(./test/result/v2/models/EnumWithNumbers.ts): ./test/ * This is a simple enum with numbers */ export enum EnumWithNumbers { - $1 = 1, - $2 = 2, - $3 = 3, + _1 = 1, + _2 = 2, + _3 = 3, }" `; @@ -1017,12 +1017,12 @@ export namespace ModelWithEnum { * These are the HTTP error code enums */ export enum statusCode { - $100 = '100', - $200_FOO = '200 FOO', - $300_FOO_BAR = '300 FOO_BAR', - $400_FOO_BAR = '400 foo-bar', - $500_FOO_BAR = '500 foo.bar', - $600_FOO_BAR = '600 foo&bar', + _100 = '100', + _200_FOO = '200 FOO', + _300_FOO_BAR = '300 FOO_BAR', + _400_FOO_BAR = '400 foo-bar', + _500_FOO_BAR = '500 foo.bar', + _600_FOO_BAR = '600 foo&bar', } @@ -3432,9 +3432,9 @@ exports[`generation v3 file(./test/result/v3/models/EnumWithNumbers.ts): ./test/ * This is a simple enum with numbers */ export enum EnumWithNumbers { - $1 = 1, - $2 = 2, - $3 = 3, + _1 = 1, + _2 = 2, + _3 = 3, }" `; @@ -3680,12 +3680,12 @@ export namespace ModelWithEnum { * These are the HTTP error code enums */ export enum statusCode { - $100 = '100', - $200_FOO = '200 FOO', - $300_FOO_BAR = '300 FOO_BAR', - $400_FOO_BAR = '400 foo-bar', - $500_FOO_BAR = '500 foo.bar', - $600_FOO_BAR = '600 foo&bar', + _100 = '100', + _200_FOO = '200 FOO', + _300_FOO_BAR = '300 FOO_BAR', + _400_FOO_BAR = '400 foo-bar', + _500_FOO_BAR = '500 foo.bar', + _600_FOO_BAR = '600 foo&bar', } diff --git a/test/mock/README.md b/test/mock/README.md new file mode 100644 index 00000000..781fe793 --- /dev/null +++ b/test/mock/README.md @@ -0,0 +1,11 @@ +If you want to generate and compare the output of the +`openapi-typescript-codegen` vs the official `swagger-codegen`. +Then the easiest way to do this is to install the swagger codegen +using Homebrew: + +```shell script +brew cask install homebrew/cask-versions/adoptopenjdk8 +brew install swagger-codegen +./codegen.sh +``` + diff --git a/test/mock/codegen.sh b/test/mock/codegen.sh new file mode 100755 index 00000000..c28edc1c --- /dev/null +++ b/test/mock/codegen.sh @@ -0,0 +1,9 @@ +#!/bin/sh + +swagger-codegen generate --input-spec v2/spec.json --lang java --output examples/v2/java/ +swagger-codegen generate --input-spec v2/spec.json --lang typescript-angular --output examples/v2/typescript-angular/ +swagger-codegen generate --input-spec v2/spec.json --lang javascript --output examples/v2/javascript/ + +swagger-codegen generate --input-spec v3/spec.json --lang java --output examples/v3/java/ +swagger-codegen generate --input-spec v3/spec.json --lang typescript-angular --output examples/v3/typescript-angular/ +swagger-codegen generate --input-spec v3/spec.json --lang javascript --output examples/v3/javascript/