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 5 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 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>stringUnion</dd><dt>**enum**</dt><dd>enum</dd></dl>|stringUnion|
|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", "enum"};

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, option);
}
enumTypeOption.defaultValue(ENUM_TYPES[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[0]);
Object propEnumType = additionalProperties.get(ENUM_TYPE_SWITCH);

Map<String, Boolean> enumTypes = new HashMap<>();
for (String enumType : ENUM_TYPES) {
enumTypes.put(enumType, enumType.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 @@ -41,10 +41,5 @@ export class Dog {
}


export enum DogBreedEnum {
Dingo = 'Dingo',
Husky = 'Husky',
Retriever = 'Retriever',
Shepherd = 'Shepherd'
}
export type DogBreedEnum = "Dingo" | "Husky" | "Retriever" | "Shepherd" ;

Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,5 @@ export class PetByType {
}


export enum PetByTypePetTypeEnum {
Cat = 'Cat',
Dog = 'Dog'
}
export type PetByTypePetTypeEnum = "Cat" | "Dog" ;

Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,5 @@ export class PetsFilteredPatchRequest {
}


export enum PetsFilteredPatchRequestPetTypeEnum {
Cat = 'Cat',
Dog = 'Dog'
}
export type PetsFilteredPatchRequestPetTypeEnum = "Cat" | "Dog" ;

Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,5 @@ export class PetsPatchRequest {
}


export enum PetsPatchRequestBreedEnum {
Dingo = 'Dingo',
Husky = 'Husky',
Retriever = 'Retriever',
Shepherd = 'Shepherd'
}
export type PetsPatchRequestBreedEnum = "Dingo" | "Husky" | "Retriever" | "Shepherd" ;

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
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 @@ -18,7 +18,7 @@ describe("PetApi", () => {
pet.id = Math.floor(Math.random() * 100000)
pet.name = "PetName"
pet.photoUrls = []
pet.status = petstore.PetStatusEnum.Available
pet.status = 'available'
pet.tags = [ tag ]

await petApi.addPet(pet);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ describe("ObjectSerializer", () => {
pet.category = category
pet.name = "PetName"
pet.photoUrls = [ "url", "other url"]
pet.status = petstore.PetStatusEnum.Available
pet.status = "available"
pet.tags = tags

expect(ObjectSerializer.serialize(pet, "Pet", "")).to.deep.equal({
Expand Down Expand Up @@ -199,7 +199,7 @@ describe("ObjectSerializer", () => {
pet.category = category
pet.name = "PetName"
pet.photoUrls = [ "url", "other url"]
pet.status = petstore.PetStatusEnum.Available
pet.status = "available"
pet.tags = tags

const deserialized = ObjectSerializer.deserialize({
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