- Fixed prefix value for enums to align with other generators

This commit is contained in:
Ferdi Koomen 2020-05-23 18:30:14 +02:00
parent a16dacaa95
commit de7b32c391
7 changed files with 44 additions and 24 deletions

View File

@ -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}'`,

View File

@ -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),

View File

@ -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}'`,

View File

@ -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),

View File

@ -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',
}

11
test/mock/README.md Normal file
View File

@ -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
```

9
test/mock/codegen.sh Executable file
View File

@ -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/