Skip to content

Commit

Permalink
[julia] improve enum support (#20016)
Browse files Browse the repository at this point in the history
* [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
  • Loading branch information
tanmaykm authored Nov 6, 2024
1 parent e1bccbf commit 06547b7
Show file tree
Hide file tree
Showing 17 changed files with 112 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -549,4 +569,28 @@ protected ImmutableMap.Builder<String, Lambda> addMustacheLambdas() {
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) + "\"";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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}}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}}")
Expand Down
3 changes: 3 additions & 0 deletions samples/client/petstore/julia/src/models/model_ApiResponse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions samples/client/petstore/julia/src/models/model_Category.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions samples/client/petstore/julia/src/models/model_MappedModel.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions samples/client/petstore/julia/src/models/model_Order.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
7 changes: 7 additions & 0 deletions samples/client/petstore/julia/src/models/model_Pet.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions samples/client/petstore/julia/src/models/model_Tag.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 8 additions & 0 deletions samples/client/petstore/julia/src/models/model_User.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions samples/server/petstore/julia/src/models/model_ApiResponse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions samples/server/petstore/julia/src/models/model_Category.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions samples/server/petstore/julia/src/models/model_Order.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
7 changes: 7 additions & 0 deletions samples/server/petstore/julia/src/models/model_Pet.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions samples/server/petstore/julia/src/models/model_Tag.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 8 additions & 0 deletions samples/server/petstore/julia/src/models/model_User.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 06547b7

Please sign in to comment.