[typescript-axios] removing namespaces from enums (#2502)

* remove namespaces in typescript-axios

* clean-up

* update samples

* update tests

* add with-npm-version-and-separate-models-and-api to all.bat

* update with-npm-version-and-separate-models-and-api samples

* update typescript-axios docs
This commit is contained in:
Deniz 2019-04-05 02:18:47 +03:00 committed by William Cheng
parent 3502afb2e4
commit d9ae80f76f
16 changed files with 136 additions and 190 deletions

View File

@ -4,3 +4,4 @@ call bin\windows\typescript-axios-petstore.bat
call bin\windows\typescript-axios-petstore-target-es6.bat call bin\windows\typescript-axios-petstore-target-es6.bat
call bin\windows\typescript-axios-petstore-with-npm-version.bat call bin\windows\typescript-axios-petstore-with-npm-version.bat
call bin\windows\typescript-axios-petstore-interfaces.bat call bin\windows\typescript-axios-petstore-interfaces.bat
call bin\windows\typescript-axios-petstore-with-npm-version-and-separate-models-and-api.bat

View File

@ -19,3 +19,4 @@ sidebar_label: typescript-axios
|snapshot|When setting this property to true the version will be suffixed with -SNAPSHOT.yyyyMMddHHmm| |false| |snapshot|When setting this property to true the version will be suffixed with -SNAPSHOT.yyyyMMddHHmm| |false|
|withInterfaces|Setting this property to true will generate interfaces next to the default class implementations.| |false| |withInterfaces|Setting this property to true will generate interfaces next to the default class implementations.| |false|
|withSeparateModelsAndApi|Put the model and api in separate folders and in separate classes| |false| |withSeparateModelsAndApi|Put the model and api in separate folders and in separate classes| |false|
|withoutPrefixEnums|Dont prefix enum names with class names| |false|

View File

@ -23,6 +23,7 @@ import io.swagger.v3.parser.util.SchemaTypeUtil;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.openapitools.codegen.CliOption; import org.openapitools.codegen.CliOption;
import org.openapitools.codegen.CodegenModel; import org.openapitools.codegen.CodegenModel;
import org.openapitools.codegen.CodegenProperty;
import org.openapitools.codegen.CodegenOperation; import org.openapitools.codegen.CodegenOperation;
import org.openapitools.codegen.SupportingFile; import org.openapitools.codegen.SupportingFile;
import org.openapitools.codegen.utils.ModelUtils; import org.openapitools.codegen.utils.ModelUtils;
@ -38,6 +39,7 @@ public class TypeScriptAxiosClientCodegen extends AbstractTypeScriptClientCodege
public static final String SNAPSHOT = "snapshot"; public static final String SNAPSHOT = "snapshot";
public static final String WITH_INTERFACES = "withInterfaces"; public static final String WITH_INTERFACES = "withInterfaces";
public static final String SEPARATE_MODELS_AND_API = "withSeparateModelsAndApi"; public static final String SEPARATE_MODELS_AND_API = "withSeparateModelsAndApi";
public static final String WITHOUT_PREFIX_ENUMS = "withoutPrefixEnums";
protected String npmName = null; protected String npmName = null;
protected String npmVersion = "1.0.0"; protected String npmVersion = "1.0.0";
@ -61,6 +63,7 @@ public class TypeScriptAxiosClientCodegen extends AbstractTypeScriptClientCodege
this.cliOptions.add(new CliOption(SNAPSHOT, "When setting this property to true the version will be suffixed with -SNAPSHOT.yyyyMMddHHmm", SchemaTypeUtil.BOOLEAN_TYPE).defaultValue(Boolean.FALSE.toString())); this.cliOptions.add(new CliOption(SNAPSHOT, "When setting this property to true the version will be suffixed with -SNAPSHOT.yyyyMMddHHmm", SchemaTypeUtil.BOOLEAN_TYPE).defaultValue(Boolean.FALSE.toString()));
this.cliOptions.add(new CliOption(WITH_INTERFACES, "Setting this property to true will generate interfaces next to the default class implementations.", SchemaTypeUtil.BOOLEAN_TYPE).defaultValue(Boolean.FALSE.toString())); this.cliOptions.add(new CliOption(WITH_INTERFACES, "Setting this property to true will generate interfaces next to the default class implementations.", SchemaTypeUtil.BOOLEAN_TYPE).defaultValue(Boolean.FALSE.toString()));
this.cliOptions.add(new CliOption(SEPARATE_MODELS_AND_API, "Put the model and api in separate folders and in separate classes", SchemaTypeUtil.BOOLEAN_TYPE).defaultValue(Boolean.FALSE.toString())); this.cliOptions.add(new CliOption(SEPARATE_MODELS_AND_API, "Put the model and api in separate folders and in separate classes", SchemaTypeUtil.BOOLEAN_TYPE).defaultValue(Boolean.FALSE.toString()));
this.cliOptions.add(new CliOption(WITHOUT_PREFIX_ENUMS, "Dont prefix enum names with class names", SchemaTypeUtil.BOOLEAN_TYPE).defaultValue(Boolean.FALSE.toString()));
} }
@Override @Override
@ -193,22 +196,47 @@ public class TypeScriptAxiosClientCodegen extends AbstractTypeScriptClientCodege
@Override @Override
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public Map<String, Object> postProcessModels(Map<String, Object> objs) { public Map<String, Object> postProcessModels(Map<String, Object> objs) {
Map<String, Object> ret = super.postProcessModels(objs); List<Object> models = (List<Object>) postProcessModelsEnum(objs).get("models");
boolean withoutPrefixEnums = (boolean)additionalProperties.getOrDefault(WITHOUT_PREFIX_ENUMS, false);
for (Object _mo : models) {
Map<String, Object> mo = (Map<String, Object>) _mo;
CodegenModel cm = (CodegenModel) mo.get("model");
// Deduce the model file name in kebab case // Deduce the model file name in kebab case
List<Map<String, Object>> models = (List<Map<String, Object>>) ret.get("models"); cm.classFilename = cm.classname.replaceAll("([a-z0-9])([A-Z])", "$1-$2").toLowerCase(Locale.ROOT);
for (Map<String, Object> m : models) {
CodegenModel model = (CodegenModel) m.get("model"); //processed enum names
model.classFilename = model.classname.replaceAll("([a-z0-9])([A-Z])", "$1-$2").toLowerCase(Locale.ROOT); if(!withoutPrefixEnums) {
cm.imports = new TreeSet(cm.imports);
// name enum with model name, e.g. StatusEnum => PetStatusEnum
for (CodegenProperty var : cm.vars) {
if (Boolean.TRUE.equals(var.isEnum)) {
var.datatypeWithEnum = var.datatypeWithEnum.replace(var.enumName, cm.classname + var.enumName);
var.enumName = var.enumName.replace(var.enumName, cm.classname + var.enumName);
} }
}
if (cm.parent != null) {
for (CodegenProperty var : cm.allVars) {
if (Boolean.TRUE.equals(var.isEnum)) {
var.datatypeWithEnum = var.datatypeWithEnum.replace(var.enumName, cm.classname + var.enumName);
var.enumName = var.enumName.replace(var.enumName, cm.classname + var.enumName);
}
}
}
}
}
// Apply the model file name to the imports as well // Apply the model file name to the imports as well
for (Map<String, String> m : (List<Map<String, String>>) ret.get("imports")) { for (Map<String, String> m : (List<Map<String, String>>) objs.get("imports")) {
String javaImport = m.get("import").substring(modelPackage.length() + 1); String javaImport = m.get("import").substring(modelPackage.length() + 1);
String tsImport = tsModelPackage + "/" + javaImport; String tsImport = tsModelPackage + "/" + javaImport;
m.put("tsImport", tsImport); m.put("tsImport", tsImport);
m.put("class", javaImport); m.put("class", javaImport);
m.put("filename", javaImport.replaceAll("([a-z0-9])([A-Z])", "$1-$2").toLowerCase(Locale.ROOT)); m.put("filename", javaImport.replaceAll("([a-z0-9])([A-Z])", "$1-$2").toLowerCase(Locale.ROOT));
} }
return ret; return objs;
} }
@Override @Override

View File

@ -18,11 +18,6 @@ export interface {{classname}} {{#parent}}extends {{{parent}}} {{/parent}}{
{{/vars}} {{/vars}}
}{{#hasEnums}} }{{#hasEnums}}
/**
* @export
* @namespace {{classname}}
*/
export namespace {{classname}} {
{{#vars}} {{#vars}}
{{#isEnum}} {{#isEnum}}
/** /**
@ -38,4 +33,4 @@ export namespace {{classname}} {
} }
{{/isEnum}} {{/isEnum}}
{{/vars}} {{/vars}}
}{{/hasEnums}} {{/hasEnums}}

View File

@ -43,7 +43,6 @@ export interface ApiResponse {
*/ */
message?: string; message?: string;
} }
/** /**
* A category for a pet * A category for a pet
* @export * @export
@ -63,7 +62,6 @@ export interface Category {
*/ */
name?: string; name?: string;
} }
/** /**
* An order for a pets from the pet store * An order for a pets from the pet store
* @export * @export
@ -99,7 +97,7 @@ export interface Order {
* @type {string} * @type {string}
* @memberof Order * @memberof Order
*/ */
status?: Order.StatusEnum; status?: OrderStatusEnum;
/** /**
* *
* @type {boolean} * @type {boolean}
@ -108,21 +106,15 @@ export interface Order {
complete?: boolean; complete?: boolean;
} }
/**
* @export
* @namespace Order
*/
export namespace Order {
/** /**
* @export * @export
* @enum {string} * @enum {string}
*/ */
export enum StatusEnum { export enum OrderStatusEnum {
Placed = 'placed', Placed = 'placed',
Approved = 'approved', Approved = 'approved',
Delivered = 'delivered' Delivered = 'delivered'
} }
}
/** /**
* A pet for sale in the pet store * A pet for sale in the pet store
@ -165,24 +157,18 @@ export interface Pet {
* @type {string} * @type {string}
* @memberof Pet * @memberof Pet
*/ */
status?: Pet.StatusEnum; status?: PetStatusEnum;
} }
/**
* @export
* @namespace Pet
*/
export namespace Pet {
/** /**
* @export * @export
* @enum {string} * @enum {string}
*/ */
export enum StatusEnum { export enum PetStatusEnum {
Available = 'available', Available = 'available',
Pending = 'pending', Pending = 'pending',
Sold = 'sold' Sold = 'sold'
} }
}
/** /**
* A tag for a pet * A tag for a pet
@ -203,7 +189,6 @@ export interface Tag {
*/ */
name?: string; name?: string;
} }
/** /**
* A User who is purchasing from the pet store * A User who is purchasing from the pet store
* @export * @export
@ -260,7 +245,6 @@ export interface User {
userStatus?: number; userStatus?: number;
} }
/** /**
* PetApi - axios parameter creator * PetApi - axios parameter creator
* @export * @export

View File

@ -43,7 +43,6 @@ export interface ApiResponse {
*/ */
message?: string; message?: string;
} }
/** /**
* A category for a pet * A category for a pet
* @export * @export
@ -63,7 +62,6 @@ export interface Category {
*/ */
name?: string; name?: string;
} }
/** /**
* An order for a pets from the pet store * An order for a pets from the pet store
* @export * @export
@ -99,7 +97,7 @@ export interface Order {
* @type {string} * @type {string}
* @memberof Order * @memberof Order
*/ */
status?: Order.StatusEnum; status?: OrderStatusEnum;
/** /**
* *
* @type {boolean} * @type {boolean}
@ -108,21 +106,15 @@ export interface Order {
complete?: boolean; complete?: boolean;
} }
/**
* @export
* @namespace Order
*/
export namespace Order {
/** /**
* @export * @export
* @enum {string} * @enum {string}
*/ */
export enum StatusEnum { export enum OrderStatusEnum {
Placed = 'placed', Placed = 'placed',
Approved = 'approved', Approved = 'approved',
Delivered = 'delivered' Delivered = 'delivered'
} }
}
/** /**
* A pet for sale in the pet store * A pet for sale in the pet store
@ -165,24 +157,18 @@ export interface Pet {
* @type {string} * @type {string}
* @memberof Pet * @memberof Pet
*/ */
status?: Pet.StatusEnum; status?: PetStatusEnum;
} }
/**
* @export
* @namespace Pet
*/
export namespace Pet {
/** /**
* @export * @export
* @enum {string} * @enum {string}
*/ */
export enum StatusEnum { export enum PetStatusEnum {
Available = 'available', Available = 'available',
Pending = 'pending', Pending = 'pending',
Sold = 'sold' Sold = 'sold'
} }
}
/** /**
* A tag for a pet * A tag for a pet
@ -203,7 +189,6 @@ export interface Tag {
*/ */
name?: string; name?: string;
} }
/** /**
* A User who is purchasing from the pet store * A User who is purchasing from the pet store
* @export * @export
@ -260,7 +245,6 @@ export interface User {
userStatus?: number; userStatus?: number;
} }
/** /**
* PetApi - axios parameter creator * PetApi - axios parameter creator
* @export * @export

View File

@ -43,7 +43,6 @@ export interface ApiResponse {
*/ */
message?: string; message?: string;
} }
/** /**
* A category for a pet * A category for a pet
* @export * @export
@ -63,7 +62,6 @@ export interface Category {
*/ */
name?: string; name?: string;
} }
/** /**
* An order for a pets from the pet store * An order for a pets from the pet store
* @export * @export
@ -99,7 +97,7 @@ export interface Order {
* @type {string} * @type {string}
* @memberof Order * @memberof Order
*/ */
status?: Order.StatusEnum; status?: OrderStatusEnum;
/** /**
* *
* @type {boolean} * @type {boolean}
@ -108,21 +106,15 @@ export interface Order {
complete?: boolean; complete?: boolean;
} }
/**
* @export
* @namespace Order
*/
export namespace Order {
/** /**
* @export * @export
* @enum {string} * @enum {string}
*/ */
export enum StatusEnum { export enum OrderStatusEnum {
Placed = 'placed', Placed = 'placed',
Approved = 'approved', Approved = 'approved',
Delivered = 'delivered' Delivered = 'delivered'
} }
}
/** /**
* A pet for sale in the pet store * A pet for sale in the pet store
@ -165,24 +157,18 @@ export interface Pet {
* @type {string} * @type {string}
* @memberof Pet * @memberof Pet
*/ */
status?: Pet.StatusEnum; status?: PetStatusEnum;
} }
/**
* @export
* @namespace Pet
*/
export namespace Pet {
/** /**
* @export * @export
* @enum {string} * @enum {string}
*/ */
export enum StatusEnum { export enum PetStatusEnum {
Available = 'available', Available = 'available',
Pending = 'pending', Pending = 'pending',
Sold = 'sold' Sold = 'sold'
} }
}
/** /**
* A tag for a pet * A tag for a pet
@ -203,7 +189,6 @@ export interface Tag {
*/ */
name?: string; name?: string;
} }
/** /**
* A User who is purchasing from the pet store * A User who is purchasing from the pet store
* @export * @export
@ -260,7 +245,6 @@ export interface User {
userStatus?: number; userStatus?: number;
} }
/** /**
* PetApi - axios parameter creator * PetApi - axios parameter creator
* @export * @export

View File

@ -49,7 +49,7 @@ export interface Order {
* @type {string} * @type {string}
* @memberof Order * @memberof Order
*/ */
status?: Order.StatusEnum; status?: OrderStatusEnum;
/** /**
* *
* @type {boolean} * @type {boolean}
@ -58,21 +58,15 @@ export interface Order {
complete?: boolean; complete?: boolean;
} }
/**
* @export
* @namespace Order
*/
export namespace Order {
/** /**
* @export * @export
* @enum {string} * @enum {string}
*/ */
export enum StatusEnum { export enum OrderStatusEnum {
Placed = 'placed', Placed = 'placed',
Approved = 'approved', Approved = 'approved',
Delivered = 'delivered' Delivered = 'delivered'
} }
}

View File

@ -57,24 +57,18 @@ export interface Pet {
* @type {string} * @type {string}
* @memberof Pet * @memberof Pet
*/ */
status?: Pet.StatusEnum; status?: PetStatusEnum;
} }
/**
* @export
* @namespace Pet
*/
export namespace Pet {
/** /**
* @export * @export
* @enum {string} * @enum {string}
*/ */
export enum StatusEnum { export enum PetStatusEnum {
Available = 'available', Available = 'available',
Pending = 'pending', Pending = 'pending',
Sold = 'sold' Sold = 'sold'
} }
}

View File

@ -43,7 +43,6 @@ export interface ApiResponse {
*/ */
message?: string; message?: string;
} }
/** /**
* A category for a pet * A category for a pet
* @export * @export
@ -63,7 +62,6 @@ export interface Category {
*/ */
name?: string; name?: string;
} }
/** /**
* An order for a pets from the pet store * An order for a pets from the pet store
* @export * @export
@ -99,7 +97,7 @@ export interface Order {
* @type {string} * @type {string}
* @memberof Order * @memberof Order
*/ */
status?: Order.StatusEnum; status?: OrderStatusEnum;
/** /**
* *
* @type {boolean} * @type {boolean}
@ -108,21 +106,15 @@ export interface Order {
complete?: boolean; complete?: boolean;
} }
/**
* @export
* @namespace Order
*/
export namespace Order {
/** /**
* @export * @export
* @enum {string} * @enum {string}
*/ */
export enum StatusEnum { export enum OrderStatusEnum {
Placed = 'placed', Placed = 'placed',
Approved = 'approved', Approved = 'approved',
Delivered = 'delivered' Delivered = 'delivered'
} }
}
/** /**
* A pet for sale in the pet store * A pet for sale in the pet store
@ -165,24 +157,18 @@ export interface Pet {
* @type {string} * @type {string}
* @memberof Pet * @memberof Pet
*/ */
status?: Pet.StatusEnum; status?: PetStatusEnum;
} }
/**
* @export
* @namespace Pet
*/
export namespace Pet {
/** /**
* @export * @export
* @enum {string} * @enum {string}
*/ */
export enum StatusEnum { export enum PetStatusEnum {
Available = 'available', Available = 'available',
Pending = 'pending', Pending = 'pending',
Sold = 'sold' Sold = 'sold'
} }
}
/** /**
* A tag for a pet * A tag for a pet
@ -203,7 +189,6 @@ export interface Tag {
*/ */
name?: string; name?: string;
} }
/** /**
* A User who is purchasing from the pet store * A User who is purchasing from the pet store
* @export * @export
@ -260,7 +245,6 @@ export interface User {
userStatus?: number; userStatus?: number;
} }
/** /**
* PetApi - axios parameter creator * PetApi - axios parameter creator
* @export * @export

View File

@ -1,5 +1,5 @@
import { expect } from "chai"; import { expect } from "chai";
import { PetApi, Pet, Category } from "@swagger/typescript-axios-petstore"; import { PetApi, Pet, PetStatusEnum, Category } from "@swagger/typescript-axios-petstore";
import axios, {AxiosInstance, AxiosResponse} from "axios"; import axios, {AxiosInstance, AxiosResponse} from "axios";
describe("PetApi", () => { describe("PetApi", () => {
@ -85,7 +85,7 @@ function createTestFixture(ts = Date.now()) {
name: `pet${ts}`, name: `pet${ts}`,
category: category, category: category,
photoUrls: ["http://foo.bar.com/1", "http://foo.bar.com/2"], photoUrls: ["http://foo.bar.com/1", "http://foo.bar.com/2"],
status: Pet.StatusEnum.Available, status: PetStatusEnum.Available,
tags: [] tags: []
}; };

View File

@ -2,6 +2,7 @@ import { expect } from "chai";
import { import {
PetApiFactory, PetApiFactory,
Pet, Pet,
PetStatusEnum,
Category Category
} from "@swagger/typescript-axios-petstore"; } from "@swagger/typescript-axios-petstore";
import { Configuration } from "@swagger/typescript-axios-petstore"; import { Configuration } from "@swagger/typescript-axios-petstore";
@ -106,7 +107,7 @@ function createTestFixture(ts = Date.now()) {
name: `pet${ts}`, name: `pet${ts}`,
category: category, category: category,
photoUrls: ["http://foo.bar.com/1", "http://foo.bar.com/2"], photoUrls: ["http://foo.bar.com/1", "http://foo.bar.com/2"],
status: Pet.StatusEnum.Available, status: PetStatusEnum.Available,
tags: [] tags: []
}; };