Handle boolean enum correctly in typescript-axios (#9025)

can't use enum because of TS18033
This commit is contained in:
Minjae Kim 2021-06-18 17:34:38 +09:00 committed by GitHub
parent 7389446354
commit 41afcd0f86
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 11 additions and 3 deletions

View File

@ -64,7 +64,7 @@ public class CodegenModel implements IJsonSchemaValidationProperties {
public String defaultValue;
public String arrayModelType;
public boolean isAlias; // Is this effectively an alias of another simple type
public boolean isString, isInteger, isLong, isNumber, isNumeric, isFloat, isDouble, isDate, isDateTime, isShort, isUnboundedInteger;
public boolean isString, isInteger, isLong, isNumber, isNumeric, isFloat, isDouble, isDate, isDateTime, isShort, isUnboundedInteger, isBoolean;
private boolean additionalPropertiesIsAnyType;
public List<CodegenProperty> vars = new ArrayList<CodegenProperty>(); // all properties (without parent's properties)
public List<CodegenProperty> allVars = new ArrayList<CodegenProperty>(); // all properties (with parent's properties)

View File

@ -728,7 +728,7 @@ public class DefaultCodegen implements CodegenConfig {
* @return the sanitized value for enum
*/
public String toEnumValue(String value, String datatype) {
if ("number".equalsIgnoreCase(datatype)) {
if ("number".equalsIgnoreCase(datatype) || "boolean".equalsIgnoreCase(datatype)) {
return value;
} else {
return "\"" + escapeText(value) + "\"";
@ -2578,6 +2578,8 @@ public class DefaultCodegen implements CodegenConfig {
} else { // type is number and without format
m.isNumber = Boolean.TRUE;
}
} else if (ModelUtils.isBooleanSchema(schema)) {
m.isBoolean = Boolean.TRUE;
} else if (ModelUtils.isFreeFormObject(openAPI, schema)) {
addAdditionPropertiesToCodeGenModel(m, schema);
}

View File

@ -663,7 +663,7 @@ public abstract class AbstractTypeScriptClientCodegen extends DefaultCodegen imp
@Override
public String toEnumValue(String value, String datatype) {
if ("number".equals(datatype)) {
if ("number".equals(datatype) || "boolean".equals(datatype)) {
return value;
} else {
return "\'" + escapeText(value) + "\'";

View File

@ -3,6 +3,11 @@
* @export
* @enum {string}
*/
{{#isBoolean}}
export type {{classname}} = {{#allowableValues}}{{#enumVars}}{{{value}}}{{^-last}} | {{/-last}}{{/enumVars}}{{/allowableValues}}
{{/isBoolean}}
{{^isBoolean}}
export enum {{classname}} {
{{#allowableValues}}
{{#enumVars}}
@ -15,3 +20,4 @@ export enum {{classname}} {
{{/enumVars}}
{{/allowableValues}}
}
{{/isBoolean}}