Skip to content

Commit aea9ed9

Browse files
committed
feat(crystal): improve enum validation
1 parent 5d04282 commit aea9ed9

2 files changed

Lines changed: 53 additions & 20 deletions

File tree

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CrystalClientCodegen.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
import org.openapitools.codegen.model.OperationMap;
3030
import org.openapitools.codegen.model.OperationsMap;
3131
import org.openapitools.codegen.templating.mustache.PrefixWithHashLambda;
32+
import org.openapitools.codegen.templating.mustache.UppercaseLambda;
33+
import org.openapitools.codegen.templating.mustache.TitlecaseLambda;
3234
import org.openapitools.codegen.utils.ModelUtils;
3335
import org.slf4j.Logger;
3436
import org.slf4j.LoggerFactory;
@@ -297,6 +299,8 @@ public void processOpts() {
297299

298300
// add lambda for mustache templates
299301
additionalProperties.put("lambdaPrefixWithHash", new PrefixWithHashLambda());
302+
additionalProperties.put("lambdaUppercase", new UppercaseLambda());
303+
additionalProperties.put("lambdaTitlecase", new TitlecaseLambda());
300304

301305
}
302306

modules/openapi-generator/src/main/resources/crystal/partial_model_generic.mustache

Lines changed: 49 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -28,29 +28,48 @@
2828

2929
{{/optionalVars}}
3030
{{#hasEnums}}
31-
class EnumAttributeValidator
32-
getter datatype : String
33-
getter allowable_values : Array(String)
34-
35-
def initialize(datatype, allowable_values)
36-
@datatype = datatype
37-
@allowable_values = allowable_values.map do |value|
38-
case datatype.to_s
39-
when /Integer/i
40-
value.to_i
41-
when /Float/i
42-
value.to_f
43-
else
44-
value
45-
end
31+
abstract class EnumAttributeValidator
32+
def valid?(value)
33+
!value || @allowable_values.includes?(value)
34+
end
35+
36+
def message
37+
"invalid value for \"#{@attribute}\", must be one of #{@allowable_values}."
38+
end
39+
40+
def to(_type, value)
41+
case _type
42+
when Int32
43+
value.to_i32
44+
when Int64
45+
value.to_i64
46+
when Float32
47+
value.to_f32
48+
when Float64
49+
value.to_f64
50+
else
51+
value.to_s
4652
end
4753
end
54+
end
4855

49-
def valid?(value)
50-
!value || allowable_values.includes?(value)
56+
{{#vars}}
57+
{{#isEnum}}
58+
{{^isContainer}}
59+
class EnumAttributeValidatorFor{{#lambdaTitlecase}}{{{name}}}{{/lambdaTitlecase}} < EnumAttributeValidator
60+
@attribute : String
61+
@allowable_values : Array(Int32 | Int64 | Float32 | Float64 | String)
62+
63+
def initialize
64+
@attribute = "{{{name}}}"
65+
@allowable_values = [{{#allowableValues}}{{#enumVars}}{{{value}}}{{^-last}}, {{/-last}}{{/enumVars}}{{/allowableValues}}].map { |value| to({{{dataType}}}, value)}
5166
end
5267
end
5368

69+
{{/isContainer}}
70+
{{/isEnum}}
71+
{{/vars}}
72+
5473
{{/hasEnums}}
5574
{{#anyOf}}
5675
{{#-first}}
@@ -97,6 +116,16 @@
97116
def list_invalid_properties
98117
invalid_properties = {{^parent}}Array(String).new{{/parent}}{{#parent}}super{{/parent}}
99118
{{#vars}}
119+
{{#isEnum}}
120+
{{^isContainer}}
121+
{{{name}}}_validator = EnumAttributeValidatorFor{{#lambdaTitlecase}}{{{name}}}{{/lambdaTitlecase}}.new
122+
if !{{{name}}}_validator.valid?(@{{{name}}})
123+
message = {{{name}}}_validator.message
124+
invalid_properties.push(message)
125+
end
126+
127+
{{/isContainer}}
128+
{{/isEnum}}
100129
{{#hasValidation}}
101130
{{#maxLength}}
102131
if {{^required}}!@{{{name}}}.nil? && {{/required}}@{{{name}}}.try &.to_s.try &.size.try &.> {{{maxLength}}}
@@ -152,7 +181,7 @@
152181
{{#vars}}
153182
{{#isEnum}}
154183
{{^isContainer}}
155-
{{{name}}}_validator = EnumAttributeValidator.new("{{{dataType}}}", [{{#allowableValues}}{{#enumVars}}{{{value}}}{{^-last}}, {{/-last}}{{/enumVars}}{{/allowableValues}}])
184+
{{{name}}}_validator = EnumAttributeValidatorFor{{#lambdaTitlecase}}{{{name}}}{{/lambdaTitlecase}}.new
156185
return false unless {{{name}}}_validator.valid?(@{{{name}}})
157186
{{/isContainer}}
158187
{{/isEnum}}
@@ -205,9 +234,9 @@
205234
# Custom attribute writer method checking allowed values (enum).
206235
# @param [Object] {{{name}}} Object to be assigned
207236
def {{{name}}}=({{{name}}})
208-
validator = EnumAttributeValidator.new("{{{dataType}}}", [{{#allowableValues}}{{#enumVars}}{{{value}}}{{^-last}}, {{/-last}}{{/enumVars}}{{/allowableValues}}])
237+
validator = EnumAttributeValidatorFor{{#lambdaTitlecase}}{{{name}}}{{/lambdaTitlecase}}.new
209238
unless validator.valid?({{{name}}})
210-
raise ArgumentError.new("invalid value for \"{{{name}}}\", must be one of #{validator.allowable_values}.")
239+
raise ArgumentError.new(validator.message)
211240
end
212241
@{{{name}}} = {{{name}}}
213242
end

0 commit comments

Comments
 (0)