forked from loafle/openapi-generator-original
[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:
parent
3502afb2e4
commit
d9ae80f76f
@ -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-with-npm-version.bat
|
||||
call bin\windows\typescript-axios-petstore-interfaces.bat
|
||||
call bin\windows\typescript-axios-petstore-with-npm-version-and-separate-models-and-api.bat
|
@ -19,3 +19,4 @@ sidebar_label: typescript-axios
|
||||
|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|
|
||||
|withSeparateModelsAndApi|Put the model and api in separate folders and in separate classes| |false|
|
||||
|withoutPrefixEnums|Dont prefix enum names with class names| |false|
|
||||
|
@ -23,6 +23,7 @@ import io.swagger.v3.parser.util.SchemaTypeUtil;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.openapitools.codegen.CliOption;
|
||||
import org.openapitools.codegen.CodegenModel;
|
||||
import org.openapitools.codegen.CodegenProperty;
|
||||
import org.openapitools.codegen.CodegenOperation;
|
||||
import org.openapitools.codegen.SupportingFile;
|
||||
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 WITH_INTERFACES = "withInterfaces";
|
||||
public static final String SEPARATE_MODELS_AND_API = "withSeparateModelsAndApi";
|
||||
public static final String WITHOUT_PREFIX_ENUMS = "withoutPrefixEnums";
|
||||
|
||||
protected String npmName = null;
|
||||
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(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(WITHOUT_PREFIX_ENUMS, "Dont prefix enum names with class names", SchemaTypeUtil.BOOLEAN_TYPE).defaultValue(Boolean.FALSE.toString()));
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -193,22 +196,47 @@ public class TypeScriptAxiosClientCodegen extends AbstractTypeScriptClientCodege
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public Map<String, Object> postProcessModels(Map<String, Object> objs) {
|
||||
Map<String, Object> ret = super.postProcessModels(objs);
|
||||
// Deduce the model file name in kebab case
|
||||
List<Map<String, Object>> models = (List<Map<String, Object>>) ret.get("models");
|
||||
for (Map<String, Object> m : models) {
|
||||
CodegenModel model = (CodegenModel) m.get("model");
|
||||
model.classFilename = model.classname.replaceAll("([a-z0-9])([A-Z])", "$1-$2").toLowerCase(Locale.ROOT);
|
||||
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
|
||||
cm.classFilename = cm.classname.replaceAll("([a-z0-9])([A-Z])", "$1-$2").toLowerCase(Locale.ROOT);
|
||||
|
||||
//processed enum names
|
||||
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
|
||||
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 tsImport = tsModelPackage + "/" + javaImport;
|
||||
m.put("tsImport", tsImport);
|
||||
m.put("class", javaImport);
|
||||
m.put("filename", javaImport.replaceAll("([a-z0-9])([A-Z])", "$1-$2").toLowerCase(Locale.ROOT));
|
||||
}
|
||||
return ret;
|
||||
return objs;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -18,24 +18,19 @@ export interface {{classname}} {{#parent}}extends {{{parent}}} {{/parent}}{
|
||||
{{/vars}}
|
||||
}{{#hasEnums}}
|
||||
|
||||
/**
|
||||
* @export
|
||||
* @namespace {{classname}}
|
||||
*/
|
||||
export namespace {{classname}} {
|
||||
{{#vars}}
|
||||
{{#isEnum}}
|
||||
/**
|
||||
* @export
|
||||
* @enum {string}
|
||||
*/
|
||||
export enum {{enumName}} {
|
||||
{{#allowableValues}}
|
||||
{{#enumVars}}
|
||||
{{{name}}} = {{{value}}}{{^-last}},{{/-last}}
|
||||
{{/enumVars}}
|
||||
{{/allowableValues}}
|
||||
}
|
||||
{{/isEnum}}
|
||||
{{#isEnum}}
|
||||
/**
|
||||
* @export
|
||||
* @enum {string}
|
||||
*/
|
||||
export enum {{enumName}} {
|
||||
{{#allowableValues}}
|
||||
{{#enumVars}}
|
||||
{{{name}}} = {{{value}}}{{^-last}},{{/-last}}
|
||||
{{/enumVars}}
|
||||
{{/allowableValues}}
|
||||
}
|
||||
{{/isEnum}}
|
||||
{{/vars}}
|
||||
}{{/hasEnums}}
|
||||
{{/hasEnums}}
|
||||
|
@ -43,7 +43,6 @@ export interface ApiResponse {
|
||||
*/
|
||||
message?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* A category for a pet
|
||||
* @export
|
||||
@ -63,7 +62,6 @@ export interface Category {
|
||||
*/
|
||||
name?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* An order for a pets from the pet store
|
||||
* @export
|
||||
@ -99,7 +97,7 @@ export interface Order {
|
||||
* @type {string}
|
||||
* @memberof Order
|
||||
*/
|
||||
status?: Order.StatusEnum;
|
||||
status?: OrderStatusEnum;
|
||||
/**
|
||||
*
|
||||
* @type {boolean}
|
||||
@ -109,19 +107,13 @@ export interface Order {
|
||||
}
|
||||
|
||||
/**
|
||||
* @export
|
||||
* @namespace Order
|
||||
*/
|
||||
export namespace Order {
|
||||
/**
|
||||
* @export
|
||||
* @enum {string}
|
||||
*/
|
||||
export enum StatusEnum {
|
||||
Placed = 'placed',
|
||||
Approved = 'approved',
|
||||
Delivered = 'delivered'
|
||||
}
|
||||
* @export
|
||||
* @enum {string}
|
||||
*/
|
||||
export enum OrderStatusEnum {
|
||||
Placed = 'placed',
|
||||
Approved = 'approved',
|
||||
Delivered = 'delivered'
|
||||
}
|
||||
|
||||
/**
|
||||
@ -165,23 +157,17 @@ export interface Pet {
|
||||
* @type {string}
|
||||
* @memberof Pet
|
||||
*/
|
||||
status?: Pet.StatusEnum;
|
||||
status?: PetStatusEnum;
|
||||
}
|
||||
|
||||
/**
|
||||
* @export
|
||||
* @namespace Pet
|
||||
*/
|
||||
export namespace Pet {
|
||||
/**
|
||||
* @export
|
||||
* @enum {string}
|
||||
*/
|
||||
export enum StatusEnum {
|
||||
Available = 'available',
|
||||
Pending = 'pending',
|
||||
Sold = 'sold'
|
||||
}
|
||||
* @export
|
||||
* @enum {string}
|
||||
*/
|
||||
export enum PetStatusEnum {
|
||||
Available = 'available',
|
||||
Pending = 'pending',
|
||||
Sold = 'sold'
|
||||
}
|
||||
|
||||
/**
|
||||
@ -203,7 +189,6 @@ export interface Tag {
|
||||
*/
|
||||
name?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* A User who is purchasing from the pet store
|
||||
* @export
|
||||
@ -260,7 +245,6 @@ export interface User {
|
||||
userStatus?: number;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* PetApi - axios parameter creator
|
||||
* @export
|
||||
|
@ -43,7 +43,6 @@ export interface ApiResponse {
|
||||
*/
|
||||
message?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* A category for a pet
|
||||
* @export
|
||||
@ -63,7 +62,6 @@ export interface Category {
|
||||
*/
|
||||
name?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* An order for a pets from the pet store
|
||||
* @export
|
||||
@ -99,7 +97,7 @@ export interface Order {
|
||||
* @type {string}
|
||||
* @memberof Order
|
||||
*/
|
||||
status?: Order.StatusEnum;
|
||||
status?: OrderStatusEnum;
|
||||
/**
|
||||
*
|
||||
* @type {boolean}
|
||||
@ -109,19 +107,13 @@ export interface Order {
|
||||
}
|
||||
|
||||
/**
|
||||
* @export
|
||||
* @namespace Order
|
||||
*/
|
||||
export namespace Order {
|
||||
/**
|
||||
* @export
|
||||
* @enum {string}
|
||||
*/
|
||||
export enum StatusEnum {
|
||||
Placed = 'placed',
|
||||
Approved = 'approved',
|
||||
Delivered = 'delivered'
|
||||
}
|
||||
* @export
|
||||
* @enum {string}
|
||||
*/
|
||||
export enum OrderStatusEnum {
|
||||
Placed = 'placed',
|
||||
Approved = 'approved',
|
||||
Delivered = 'delivered'
|
||||
}
|
||||
|
||||
/**
|
||||
@ -165,23 +157,17 @@ export interface Pet {
|
||||
* @type {string}
|
||||
* @memberof Pet
|
||||
*/
|
||||
status?: Pet.StatusEnum;
|
||||
status?: PetStatusEnum;
|
||||
}
|
||||
|
||||
/**
|
||||
* @export
|
||||
* @namespace Pet
|
||||
*/
|
||||
export namespace Pet {
|
||||
/**
|
||||
* @export
|
||||
* @enum {string}
|
||||
*/
|
||||
export enum StatusEnum {
|
||||
Available = 'available',
|
||||
Pending = 'pending',
|
||||
Sold = 'sold'
|
||||
}
|
||||
* @export
|
||||
* @enum {string}
|
||||
*/
|
||||
export enum PetStatusEnum {
|
||||
Available = 'available',
|
||||
Pending = 'pending',
|
||||
Sold = 'sold'
|
||||
}
|
||||
|
||||
/**
|
||||
@ -203,7 +189,6 @@ export interface Tag {
|
||||
*/
|
||||
name?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* A User who is purchasing from the pet store
|
||||
* @export
|
||||
@ -260,7 +245,6 @@ export interface User {
|
||||
userStatus?: number;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* PetApi - axios parameter creator
|
||||
* @export
|
||||
|
@ -43,7 +43,6 @@ export interface ApiResponse {
|
||||
*/
|
||||
message?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* A category for a pet
|
||||
* @export
|
||||
@ -63,7 +62,6 @@ export interface Category {
|
||||
*/
|
||||
name?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* An order for a pets from the pet store
|
||||
* @export
|
||||
@ -99,7 +97,7 @@ export interface Order {
|
||||
* @type {string}
|
||||
* @memberof Order
|
||||
*/
|
||||
status?: Order.StatusEnum;
|
||||
status?: OrderStatusEnum;
|
||||
/**
|
||||
*
|
||||
* @type {boolean}
|
||||
@ -109,19 +107,13 @@ export interface Order {
|
||||
}
|
||||
|
||||
/**
|
||||
* @export
|
||||
* @namespace Order
|
||||
*/
|
||||
export namespace Order {
|
||||
/**
|
||||
* @export
|
||||
* @enum {string}
|
||||
*/
|
||||
export enum StatusEnum {
|
||||
Placed = 'placed',
|
||||
Approved = 'approved',
|
||||
Delivered = 'delivered'
|
||||
}
|
||||
* @export
|
||||
* @enum {string}
|
||||
*/
|
||||
export enum OrderStatusEnum {
|
||||
Placed = 'placed',
|
||||
Approved = 'approved',
|
||||
Delivered = 'delivered'
|
||||
}
|
||||
|
||||
/**
|
||||
@ -165,23 +157,17 @@ export interface Pet {
|
||||
* @type {string}
|
||||
* @memberof Pet
|
||||
*/
|
||||
status?: Pet.StatusEnum;
|
||||
status?: PetStatusEnum;
|
||||
}
|
||||
|
||||
/**
|
||||
* @export
|
||||
* @namespace Pet
|
||||
*/
|
||||
export namespace Pet {
|
||||
/**
|
||||
* @export
|
||||
* @enum {string}
|
||||
*/
|
||||
export enum StatusEnum {
|
||||
Available = 'available',
|
||||
Pending = 'pending',
|
||||
Sold = 'sold'
|
||||
}
|
||||
* @export
|
||||
* @enum {string}
|
||||
*/
|
||||
export enum PetStatusEnum {
|
||||
Available = 'available',
|
||||
Pending = 'pending',
|
||||
Sold = 'sold'
|
||||
}
|
||||
|
||||
/**
|
||||
@ -203,7 +189,6 @@ export interface Tag {
|
||||
*/
|
||||
name?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* A User who is purchasing from the pet store
|
||||
* @export
|
||||
@ -260,7 +245,6 @@ export interface User {
|
||||
userStatus?: number;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* PetApi - axios parameter creator
|
||||
* @export
|
||||
|
@ -41,4 +41,3 @@ export interface ApiResponse {
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
@ -35,4 +35,3 @@ export interface Category {
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
@ -49,7 +49,7 @@ export interface Order {
|
||||
* @type {string}
|
||||
* @memberof Order
|
||||
*/
|
||||
status?: Order.StatusEnum;
|
||||
status?: OrderStatusEnum;
|
||||
/**
|
||||
*
|
||||
* @type {boolean}
|
||||
@ -59,19 +59,13 @@ export interface Order {
|
||||
}
|
||||
|
||||
/**
|
||||
* @export
|
||||
* @namespace Order
|
||||
*/
|
||||
export namespace Order {
|
||||
/**
|
||||
* @export
|
||||
* @enum {string}
|
||||
*/
|
||||
export enum StatusEnum {
|
||||
Placed = 'placed',
|
||||
Approved = 'approved',
|
||||
Delivered = 'delivered'
|
||||
}
|
||||
* @export
|
||||
* @enum {string}
|
||||
*/
|
||||
export enum OrderStatusEnum {
|
||||
Placed = 'placed',
|
||||
Approved = 'approved',
|
||||
Delivered = 'delivered'
|
||||
}
|
||||
|
||||
|
||||
|
@ -57,23 +57,17 @@ export interface Pet {
|
||||
* @type {string}
|
||||
* @memberof Pet
|
||||
*/
|
||||
status?: Pet.StatusEnum;
|
||||
status?: PetStatusEnum;
|
||||
}
|
||||
|
||||
/**
|
||||
* @export
|
||||
* @namespace Pet
|
||||
*/
|
||||
export namespace Pet {
|
||||
/**
|
||||
* @export
|
||||
* @enum {string}
|
||||
*/
|
||||
export enum StatusEnum {
|
||||
Available = 'available',
|
||||
Pending = 'pending',
|
||||
Sold = 'sold'
|
||||
}
|
||||
* @export
|
||||
* @enum {string}
|
||||
*/
|
||||
export enum PetStatusEnum {
|
||||
Available = 'available',
|
||||
Pending = 'pending',
|
||||
Sold = 'sold'
|
||||
}
|
||||
|
||||
|
||||
|
@ -35,4 +35,3 @@ export interface Tag {
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
@ -71,4 +71,3 @@ export interface User {
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
@ -43,7 +43,6 @@ export interface ApiResponse {
|
||||
*/
|
||||
message?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* A category for a pet
|
||||
* @export
|
||||
@ -63,7 +62,6 @@ export interface Category {
|
||||
*/
|
||||
name?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* An order for a pets from the pet store
|
||||
* @export
|
||||
@ -99,7 +97,7 @@ export interface Order {
|
||||
* @type {string}
|
||||
* @memberof Order
|
||||
*/
|
||||
status?: Order.StatusEnum;
|
||||
status?: OrderStatusEnum;
|
||||
/**
|
||||
*
|
||||
* @type {boolean}
|
||||
@ -109,19 +107,13 @@ export interface Order {
|
||||
}
|
||||
|
||||
/**
|
||||
* @export
|
||||
* @namespace Order
|
||||
*/
|
||||
export namespace Order {
|
||||
/**
|
||||
* @export
|
||||
* @enum {string}
|
||||
*/
|
||||
export enum StatusEnum {
|
||||
Placed = 'placed',
|
||||
Approved = 'approved',
|
||||
Delivered = 'delivered'
|
||||
}
|
||||
* @export
|
||||
* @enum {string}
|
||||
*/
|
||||
export enum OrderStatusEnum {
|
||||
Placed = 'placed',
|
||||
Approved = 'approved',
|
||||
Delivered = 'delivered'
|
||||
}
|
||||
|
||||
/**
|
||||
@ -165,23 +157,17 @@ export interface Pet {
|
||||
* @type {string}
|
||||
* @memberof Pet
|
||||
*/
|
||||
status?: Pet.StatusEnum;
|
||||
status?: PetStatusEnum;
|
||||
}
|
||||
|
||||
/**
|
||||
* @export
|
||||
* @namespace Pet
|
||||
*/
|
||||
export namespace Pet {
|
||||
/**
|
||||
* @export
|
||||
* @enum {string}
|
||||
*/
|
||||
export enum StatusEnum {
|
||||
Available = 'available',
|
||||
Pending = 'pending',
|
||||
Sold = 'sold'
|
||||
}
|
||||
* @export
|
||||
* @enum {string}
|
||||
*/
|
||||
export enum PetStatusEnum {
|
||||
Available = 'available',
|
||||
Pending = 'pending',
|
||||
Sold = 'sold'
|
||||
}
|
||||
|
||||
/**
|
||||
@ -203,7 +189,6 @@ export interface Tag {
|
||||
*/
|
||||
name?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* A User who is purchasing from the pet store
|
||||
* @export
|
||||
@ -260,7 +245,6 @@ export interface User {
|
||||
userStatus?: number;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* PetApi - axios parameter creator
|
||||
* @export
|
||||
|
@ -1,5 +1,5 @@
|
||||
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";
|
||||
|
||||
describe("PetApi", () => {
|
||||
@ -85,7 +85,7 @@ function createTestFixture(ts = Date.now()) {
|
||||
name: `pet${ts}`,
|
||||
category: category,
|
||||
photoUrls: ["http://foo.bar.com/1", "http://foo.bar.com/2"],
|
||||
status: Pet.StatusEnum.Available,
|
||||
status: PetStatusEnum.Available,
|
||||
tags: []
|
||||
};
|
||||
|
||||
|
@ -2,6 +2,7 @@ import { expect } from "chai";
|
||||
import {
|
||||
PetApiFactory,
|
||||
Pet,
|
||||
PetStatusEnum,
|
||||
Category
|
||||
} from "@swagger/typescript-axios-petstore";
|
||||
import { Configuration } from "@swagger/typescript-axios-petstore";
|
||||
@ -106,7 +107,7 @@ function createTestFixture(ts = Date.now()) {
|
||||
name: `pet${ts}`,
|
||||
category: category,
|
||||
photoUrls: ["http://foo.bar.com/1", "http://foo.bar.com/2"],
|
||||
status: Pet.StatusEnum.Available,
|
||||
status: PetStatusEnum.Available,
|
||||
tags: []
|
||||
};
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user