Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Typescript] Enum types #18531

Merged
merged 8 commits into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions bin/configs/typescript-consolidated-browser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ additionalProperties:
projectName: ts-petstore-client
moduleName: petstore
supportsES6: true
enumType: stringUnion
1 change: 1 addition & 0 deletions bin/configs/typescript-consolidated-deno.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ additionalProperties:
npmName: ts-petstore-client
projectName: ts-petstore-client
moduleName: petstore
enumType: stringUnion
1 change: 1 addition & 0 deletions bin/configs/typescript-consolidated-jquery.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ additionalProperties:
npmName: ts-petstore-client
projectName: ts-petstore-client
moduleName: petstore
enumType: stringUnion
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ additionalProperties:
useObjectParameters: true
projectName: ts-petstore-client
moduleName: petstore
enumType: stringUnion
1 change: 1 addition & 0 deletions docs/generators/typescript.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
|enumNameSuffix|Suffix that will be appended to all enum names.| |Enum|
|enumPropertyNaming|Naming convention for enum properties: 'camelCase', 'PascalCase', 'snake_case', 'UPPERCASE', and 'original'| |PascalCase|
|enumPropertyNamingReplaceSpecialChar|Set to true to replace '-' and '+' symbols with 'minus_' and 'plus_' in enum of type string| |false|
|enumType|Specify the enum type which should be used in the client code.|<dl><dt>**stringUnion**</dt><dd>Union of literal string types</dd><dt>**enum**</dt><dd>Typescript's [string enums](https://www.typescriptlang.org/docs/handbook/enums.html#string-enums)</dd></dl>|enum|
|enumUnknownDefaultCase|If the server adds new enum cases, that are unknown by an old spec/client, the client will fail to parse the network response.With this option enabled, each enum will have a new case, 'unknown_default_open_api', so that when the server sends an enum case that is not known by the client/spec, they can safely fallback to this case.|<dl><dt>**false**</dt><dd>No changes to the enum's are made, this is the default option.</dd><dt>**true**</dt><dd>With this option enabled, each enum will have a new case, 'unknown_default_open_api', so that when the enum case sent by the server is not known by the client/spec, can safely be decoded to this case.</dd></dl>|false|
|fileContentDataType|Specifies the type to use for the content of a file - i.e. Blob (Browser, Deno) / Buffer (node)| |Buffer|
|framework|Specify the framework which should be used in the client code.|<dl><dt>**fetch-api**</dt><dd>fetch-api</dd><dt>**jquery**</dt><dd>jquery</dd></dl>|fetch-api|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ public class TypeScriptClientCodegen extends AbstractTypeScriptClientCodegen imp

private static final String USE_OBJECT_PARAMS_SWITCH = "useObjectParameters";
private static final String USE_OBJECT_PARAMS_DESC = "Use aggregate parameter objects as function arguments for api operations instead of passing each parameter as a separate function argument.";
private static final String ENUM_TYPE_SWITCH = "enumType";
private static final String ENUM_TYPE_SWITCH_DESC = "Specify the enum type which should be used in the client code.";
private static final String[][] ENUM_TYPES = {{"stringUnion", "Union of literal string types"}, {"enum", "Typescript's [string enums](https://www.typescriptlang.org/docs/handbook/enums.html#string-enums)"}};

private final Map<String, String> frameworkToHttpLibMap;

Expand Down Expand Up @@ -135,6 +138,13 @@ public TypeScriptClientCodegen() {

cliOptions.add(platformOption);

CliOption enumTypeOption = new CliOption(TypeScriptClientCodegen.ENUM_TYPE_SWITCH, TypeScriptClientCodegen.ENUM_TYPE_SWITCH_DESC);
for (String[] option : TypeScriptClientCodegen.ENUM_TYPES) {
enumTypeOption.addEnum(option[0], option[1]);
}
enumTypeOption.defaultValue(ENUM_TYPES[1][0]);
cliOptions.add(enumTypeOption);

// Set property naming to camelCase
supportModelPropertyNaming(CodegenConstants.MODEL_PROPERTY_NAMING_TYPE.camelCase);

Expand Down Expand Up @@ -426,6 +436,15 @@ public void processOpts() {
"http", httpLibName + ".ts"
));

additionalProperties.putIfAbsent(ENUM_TYPE_SWITCH, ENUM_TYPES[1][0]);
Object propEnumType = additionalProperties.get(ENUM_TYPE_SWITCH);

Map<String, Boolean> enumTypes = new HashMap<>();
for (String[] option : ENUM_TYPES) {
enumTypes.put(option[0], option[0].equals(propEnumType));
}
additionalProperties.put("enumTypes", enumTypes);

Object propPlatform = additionalProperties.get(PLATFORM_SWITCH);
if (propPlatform == null) {
propPlatform = "browser";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,26 +71,40 @@ export class {{classname}} {{#parent}}extends {{{.}}} {{/parent}}{

{{#vars}}
{{#isEnum}}
{{#enumTypes}}
{{#enum}}
export enum {{classname}}{{enumName}} {
{{#allowableValues}}
{{#enumVars}}
{{name}} = {{{value}}}{{^-last}},{{/-last}}
{{/enumVars}}
{{/allowableValues}}
}
{{/enum}}
{{#stringUnion}}
export type {{classname}}{{enumName}} ={{#allowableValues}}{{#values}} "{{.}}" {{^-last}}|{{/-last}}{{/values}}{{/allowableValues}};
{{/stringUnion}}
{{/enumTypes}}
{{/isEnum}}
{{/vars}}

{{/hasEnums}}
{{/isEnum}}
{{#isEnum}}
{{#enumTypes}}
{{#enum}}
export enum {{classname}} {
{{#allowableValues}}
{{#enumVars}}
{{name}} = {{{value}}}{{^-last}},{{/-last}}
{{/enumVars}}
{{/allowableValues}}
}
{{/enum}}
{{#stringUnion}}
export type {{classname}} ={{#allowableValues}}{{#values}} "{{.}}" {{^-last}}|{{/-last}}{{/values}}{{/allowableValues}};
{{/stringUnion}}
{{/enumTypes}}
{{/isEnum}}
{{/model}}
{{/models}}
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,5 @@ export class Order {
}


export enum OrderStatusEnum {
Placed = 'placed',
Approved = 'approved',
Delivered = 'delivered'
}
export type OrderStatusEnum = "placed" | "approved" | "delivered" ;

Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,5 @@ export class Pet {
}


export enum PetStatusEnum {
Available = 'available',
Pending = 'pending',
Sold = 'sold'
}
export type PetStatusEnum = "available" | "pending" | "sold" ;

Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,5 @@ export class Order {
}


export enum OrderStatusEnum {
Placed = 'placed',
Approved = 'approved',
Delivered = 'delivered'
}
export type OrderStatusEnum = "placed" | "approved" | "delivered" ;

Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,5 @@ export class Pet {
}


export enum PetStatusEnum {
Available = 'available',
Pending = 'pending',
Sold = 'sold'
}
export type PetStatusEnum = "available" | "pending" | "sold" ;

Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,5 @@ export class Order {
}


export enum OrderStatusEnum {
Placed = 'placed',
Approved = 'approved',
Delivered = 'delivered'
}
export type OrderStatusEnum = "placed" | "approved" | "delivered" ;

Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,5 @@ export class Pet {
}


export enum PetStatusEnum {
Available = 'available',
Pending = 'pending',
Sold = 'sold'
}
export type PetStatusEnum = "available" | "pending" | "sold" ;

Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,5 @@ export class Order {
}


export enum OrderStatusEnum {
Placed = 'placed',
Approved = 'approved',
Delivered = 'delivered'
}
export type OrderStatusEnum = "placed" | "approved" | "delivered" ;

Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,5 @@ export class Pet {
}


export enum PetStatusEnum {
Available = 'available',
Pending = 'pending',
Sold = 'sold'
}
export type PetStatusEnum = "available" | "pending" | "sold" ;

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect } from '@esm-bundle/chai';
import { ServerConfiguration, createConfiguration, PetApi, Tag, Pet, PetStatusEnum, ApiException, RequiredError } from 'ts-petstore-client'
import { ServerConfiguration, createConfiguration, PetApi, Tag, Pet, ApiException, RequiredError } from 'ts-petstore-client'
import image from "./pet";

const configuration = createConfiguration({
Expand All @@ -20,7 +20,7 @@ function createPet() {
pet.id = Math.floor(Math.random() * 100000)
pet.name = "PetName"
pet.photoUrls = []
pet.status = PetStatusEnum.Available
pet.status = 'available'
pet.tags = [ tag ]
return pet as Required<Pet>;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ describe("ObjectSerializer", () => {
});

it ("Enum", () => {
const input = "available"
expect(ObjectSerializer.serialize(input, "Pet.StatusEnum", "")).to.equal("available")
const input = petstore.PetStatusEnum.Available
expect(ObjectSerializer.serialize(input, "Pet.StatusEnum", "")).to.equal(petstore.PetStatusEnum.Available)
})

it("Complex Class", () => {
Expand Down Expand Up @@ -103,7 +103,7 @@ describe("ObjectSerializer", () => {
"name": category.name
},
"photoUrls": [ "url", "other url"],
"status": "available",
"status": petstore.PetStatusEnum.Available,
"tags": tagResult
})
})
Expand Down Expand Up @@ -173,8 +173,8 @@ describe("ObjectSerializer", () => {
});

it ("Enum", () => {
const input = "available"
expect(ObjectSerializer.deserialize("available", "Pet.StatusEnum", "")).to.equal(input)
const input = petstore.PetStatusEnum.Available
expect(ObjectSerializer.deserialize(petstore.PetStatusEnum.Available, "Pet.StatusEnum", "")).to.equal(input)
})

it("Complex Class", () => {
Expand Down Expand Up @@ -210,7 +210,7 @@ describe("ObjectSerializer", () => {
"name": category.name
},
"photoUrls": [ "url", "other url"],
"status": "available",
"status": petstore.PetStatusEnum.Available,
"tags": tagResult
}, "Pet", "") as petstore.Pet

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const petId = Math.floor(Math.random() * 100000);
pet.id = petId;
pet.name = "PetName";
pet.photoUrls = [];
pet.status = petstore.PetStatusEnum.Available;
pet.status = 'available';
pet.tags = [tag];

Deno.test({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const pet = new petstore.Pet()
pet.id = Math.floor(Math.random() * 100000)
pet.name = "PetName"
pet.photoUrls = []
pet.status = petstore.PetStatusEnum.Available
pet.status = 'available'
pet.tags = [ tag ]

QUnit.module("PetApi")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const pet = new petstore.Pet()
pet.id = Math.floor(Math.random() * 100000)
pet.name = "PetName"
pet.photoUrls = []
pet.status = petstore.PetStatusEnum.Available
pet.status = 'available'
pet.tags = [ tag ]

describe("PetApi", () =>{
Expand Down