forked from loafle/openapi-generator-original
[julia] improve enum support (#20016)
* [julia] improve enum support Improved enum support. Added an override for `postProcessModels` method in julia code generator to do enum post processing. Added an override for `toEnumValue` method in julia code generator to generate enum values correctly based on julia types. Updated templates to use `#enumVars` for generating enum values. * generated samples
This commit is contained in:
parent
e1bccbfe47
commit
06547b7b82
@ -25,6 +25,7 @@ import org.openapitools.codegen.meta.features.ParameterFeature;
|
||||
import org.openapitools.codegen.meta.features.SchemaSupportFeature;
|
||||
import org.openapitools.codegen.meta.features.SecurityFeature;
|
||||
import org.openapitools.codegen.meta.features.WireFormatFeature;
|
||||
import org.openapitools.codegen.model.ModelsMap;
|
||||
|
||||
import java.io.File;
|
||||
import java.text.SimpleDateFormat;
|
||||
@ -70,6 +71,25 @@ public abstract class AbstractJuliaCodegen extends DefaultCodegen {
|
||||
protected final DateTimeFormatter OFFSET_DATE_TIME_FORMAT = DateTimeFormatter.ISO_OFFSET_DATE_TIME;
|
||||
protected final SimpleDateFormat DATE_TIME_FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX", Locale.ROOT);
|
||||
protected final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd", Locale.ROOT);
|
||||
protected final List<String> UNQUOTED_DATATYPES = Arrays.asList(
|
||||
"int",
|
||||
"integer",
|
||||
"long",
|
||||
"short",
|
||||
"byte",
|
||||
"float",
|
||||
"double",
|
||||
"number",
|
||||
"decimal",
|
||||
"boolean",
|
||||
"Int64",
|
||||
"Int32",
|
||||
"UInt8",
|
||||
"Float32",
|
||||
"Float64",
|
||||
"Bool"
|
||||
);
|
||||
|
||||
|
||||
public AbstractJuliaCodegen() {
|
||||
super();
|
||||
@ -549,4 +569,28 @@ public abstract class AbstractJuliaCodegen extends DefaultCodegen {
|
||||
return super.addMustacheLambdas()
|
||||
.put("escapeDollar", new EscapeChar("(?<!\\\\)\\$", "\\\\\\$"));
|
||||
}
|
||||
|
||||
// override with any special post-processing
|
||||
@Override
|
||||
@SuppressWarnings("static-method")
|
||||
public ModelsMap postProcessModels(ModelsMap objs) {
|
||||
objs = super.postProcessModels(objs);
|
||||
return postProcessModelsEnum(objs);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the enum value in the language specified format
|
||||
* e.g. status becomes "status"
|
||||
*
|
||||
* @param value enum variable name
|
||||
* @param datatype data type
|
||||
* @return the sanitized value for enum
|
||||
*/
|
||||
public String toEnumValue(String value, String datatype) {
|
||||
if (datatype != null && UNQUOTED_DATATYPES.contains(datatype)) {
|
||||
return value;
|
||||
} else {
|
||||
return "\"" + escapeText(value) + "\"";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -38,11 +38,11 @@ end
|
||||
|
||||
function OpenAPI.validate_property(::Type{ {{classname}} }, name::Symbol, val)
|
||||
{{#allVars}}
|
||||
{{#isEnum}}
|
||||
{{#isEnum}}{{#allowableValues}}
|
||||
if name === Symbol("{{#lambda.escapeDollar}}{{baseName}}{{/lambda.escapeDollar}}")
|
||||
OpenAPI.validate_param(name, "{{classname}}", :enum, val, [{{#_enum}}{{#isString}}"{{.}}"{{/isString}}{{^isString}}{{.}}{{/isString}}{{^-last}}, {{/-last}}{{/_enum}}])
|
||||
OpenAPI.validate_param(name, "{{classname}}", :enum, val, [{{#enumVars}}{{{value}}}{{^-last}}, {{/-last}}{{/enumVars}}])
|
||||
end
|
||||
{{/isEnum}}
|
||||
{{/allowableValues}}{{/isEnum}}
|
||||
{{^isEnum}}
|
||||
{{#format}}
|
||||
if name === Symbol("{{#lambda.escapeDollar}}{{baseName}}{{/lambda.escapeDollar}}")
|
||||
|
@ -38,11 +38,11 @@ end
|
||||
|
||||
function OpenAPI.validate_property(::Type{ {{classname}} }, name::Symbol, val)
|
||||
{{#allVars}}
|
||||
{{#isEnum}}
|
||||
{{#isEnum}}{{#allowableValues}}
|
||||
if name === Symbol("{{#lambda.escapeDollar}}{{baseName}}{{/lambda.escapeDollar}}")
|
||||
OpenAPI.validate_param(name, "{{classname}}", :enum, val, [{{#_enum}}{{#isString}}"{{.}}"{{/isString}}{{^isString}}{{.}}{{/isString}}{{^-last}}, {{/-last}}{{/_enum}}])
|
||||
OpenAPI.validate_param(name, "{{classname}}", :enum, val, [{{#enumVars}}{{{value}}}{{^-last}}, {{/-last}}{{/enumVars}}])
|
||||
end
|
||||
{{/isEnum}}
|
||||
{{/allowableValues}}{{/isEnum}}
|
||||
{{^isEnum}}
|
||||
{{#format}}
|
||||
if name === Symbol("{{#lambda.escapeDollar}}{{baseName}}{{/lambda.escapeDollar}}")
|
||||
|
@ -36,7 +36,10 @@ function check_required(o::ApiResponse)
|
||||
end
|
||||
|
||||
function OpenAPI.validate_property(::Type{ ApiResponse }, name::Symbol, val)
|
||||
|
||||
if name === Symbol("code")
|
||||
OpenAPI.validate_param(name, "ApiResponse", :format, val, "int32")
|
||||
end
|
||||
|
||||
|
||||
end
|
||||
|
@ -32,9 +32,11 @@ function check_required(o::Category)
|
||||
end
|
||||
|
||||
function OpenAPI.validate_property(::Type{ Category }, name::Symbol, val)
|
||||
|
||||
if name === Symbol("id")
|
||||
OpenAPI.validate_param(name, "Category", :format, val, "int64")
|
||||
end
|
||||
|
||||
if name === Symbol("name")
|
||||
OpenAPI.validate_param(name, "Category", :pattern, val, r"^[a-zA-Z0-9]+[a-zA-Z0-9\.\-_]*[a-zA-Z0-9]+$")
|
||||
end
|
||||
|
@ -32,9 +32,11 @@ function check_required(o::MappedModel)
|
||||
end
|
||||
|
||||
function OpenAPI.validate_property(::Type{ MappedModel }, name::Symbol, val)
|
||||
|
||||
if name === Symbol("another_property")
|
||||
OpenAPI.validate_param(name, "MappedModel", :format, val, "int32")
|
||||
end
|
||||
|
||||
if name === Symbol("uuid_default_value")
|
||||
OpenAPI.validate_param(name, "MappedModel", :format, val, "uuid")
|
||||
end
|
||||
|
@ -48,19 +48,26 @@ function check_required(o::Order)
|
||||
end
|
||||
|
||||
function OpenAPI.validate_property(::Type{ Order }, name::Symbol, val)
|
||||
|
||||
if name === Symbol("id")
|
||||
OpenAPI.validate_param(name, "Order", :format, val, "int64")
|
||||
end
|
||||
|
||||
if name === Symbol("petId")
|
||||
OpenAPI.validate_param(name, "Order", :format, val, "int64")
|
||||
end
|
||||
|
||||
if name === Symbol("quantity")
|
||||
OpenAPI.validate_param(name, "Order", :format, val, "int32")
|
||||
end
|
||||
|
||||
if name === Symbol("shipDate")
|
||||
OpenAPI.validate_param(name, "Order", :format, val, "date-time")
|
||||
end
|
||||
|
||||
if name === Symbol("status")
|
||||
OpenAPI.validate_param(name, "Order", :enum, val, ["placed", "approved", "delivered"])
|
||||
end
|
||||
|
||||
|
||||
end
|
||||
|
@ -50,10 +50,17 @@ function check_required(o::Pet)
|
||||
end
|
||||
|
||||
function OpenAPI.validate_property(::Type{ Pet }, name::Symbol, val)
|
||||
|
||||
if name === Symbol("id")
|
||||
OpenAPI.validate_param(name, "Pet", :format, val, "int64")
|
||||
end
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if name === Symbol("status")
|
||||
OpenAPI.validate_param(name, "Pet", :enum, val, ["available", "pending", "sold"])
|
||||
end
|
||||
|
||||
end
|
||||
|
@ -32,7 +32,9 @@ function check_required(o::Tag)
|
||||
end
|
||||
|
||||
function OpenAPI.validate_property(::Type{ Tag }, name::Symbol, val)
|
||||
|
||||
if name === Symbol("id")
|
||||
OpenAPI.validate_param(name, "Tag", :format, val, "int64")
|
||||
end
|
||||
|
||||
end
|
||||
|
@ -56,9 +56,17 @@ function check_required(o::User)
|
||||
end
|
||||
|
||||
function OpenAPI.validate_property(::Type{ User }, name::Symbol, val)
|
||||
|
||||
if name === Symbol("id")
|
||||
OpenAPI.validate_param(name, "User", :format, val, "int64")
|
||||
end
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if name === Symbol("userStatus")
|
||||
OpenAPI.validate_param(name, "User", :format, val, "int32")
|
||||
end
|
||||
|
@ -32,9 +32,11 @@ function check_required(o::AnotherModel)
|
||||
end
|
||||
|
||||
function OpenAPI.validate_property(::Type{ AnotherModel }, name::Symbol, val)
|
||||
|
||||
if name === Symbol("another_property")
|
||||
OpenAPI.validate_param(name, "AnotherModel", :format, val, "int32")
|
||||
end
|
||||
|
||||
if name === Symbol("uuid_default_value")
|
||||
OpenAPI.validate_param(name, "AnotherModel", :format, val, "uuid")
|
||||
end
|
||||
|
@ -36,7 +36,10 @@ function check_required(o::ApiResponse)
|
||||
end
|
||||
|
||||
function OpenAPI.validate_property(::Type{ ApiResponse }, name::Symbol, val)
|
||||
|
||||
if name === Symbol("code")
|
||||
OpenAPI.validate_param(name, "ApiResponse", :format, val, "int32")
|
||||
end
|
||||
|
||||
|
||||
end
|
||||
|
@ -32,9 +32,11 @@ function check_required(o::Category)
|
||||
end
|
||||
|
||||
function OpenAPI.validate_property(::Type{ Category }, name::Symbol, val)
|
||||
|
||||
if name === Symbol("id")
|
||||
OpenAPI.validate_param(name, "Category", :format, val, "int64")
|
||||
end
|
||||
|
||||
if name === Symbol("name")
|
||||
OpenAPI.validate_param(name, "Category", :pattern, val, r"^[a-zA-Z0-9]+[a-zA-Z0-9\.\-_]*[a-zA-Z0-9]+$")
|
||||
end
|
||||
|
@ -48,19 +48,26 @@ function check_required(o::Order)
|
||||
end
|
||||
|
||||
function OpenAPI.validate_property(::Type{ Order }, name::Symbol, val)
|
||||
|
||||
if name === Symbol("id")
|
||||
OpenAPI.validate_param(name, "Order", :format, val, "int64")
|
||||
end
|
||||
|
||||
if name === Symbol("petId")
|
||||
OpenAPI.validate_param(name, "Order", :format, val, "int64")
|
||||
end
|
||||
|
||||
if name === Symbol("quantity")
|
||||
OpenAPI.validate_param(name, "Order", :format, val, "int32")
|
||||
end
|
||||
|
||||
if name === Symbol("shipDate")
|
||||
OpenAPI.validate_param(name, "Order", :format, val, "date-time")
|
||||
end
|
||||
|
||||
if name === Symbol("status")
|
||||
OpenAPI.validate_param(name, "Order", :enum, val, ["placed", "approved", "delivered"])
|
||||
end
|
||||
|
||||
|
||||
end
|
||||
|
@ -50,10 +50,17 @@ function check_required(o::Pet)
|
||||
end
|
||||
|
||||
function OpenAPI.validate_property(::Type{ Pet }, name::Symbol, val)
|
||||
|
||||
if name === Symbol("id")
|
||||
OpenAPI.validate_param(name, "Pet", :format, val, "int64")
|
||||
end
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if name === Symbol("status")
|
||||
OpenAPI.validate_param(name, "Pet", :enum, val, ["available", "pending", "sold"])
|
||||
end
|
||||
|
||||
end
|
||||
|
@ -32,7 +32,9 @@ function check_required(o::Tag)
|
||||
end
|
||||
|
||||
function OpenAPI.validate_property(::Type{ Tag }, name::Symbol, val)
|
||||
|
||||
if name === Symbol("id")
|
||||
OpenAPI.validate_param(name, "Tag", :format, val, "int64")
|
||||
end
|
||||
|
||||
end
|
||||
|
@ -56,9 +56,17 @@ function check_required(o::User)
|
||||
end
|
||||
|
||||
function OpenAPI.validate_property(::Type{ User }, name::Symbol, val)
|
||||
|
||||
if name === Symbol("id")
|
||||
OpenAPI.validate_param(name, "User", :format, val, "int64")
|
||||
end
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if name === Symbol("userStatus")
|
||||
OpenAPI.validate_param(name, "User", :format, val, "int32")
|
||||
end
|
||||
|
Loading…
x
Reference in New Issue
Block a user