Skip to content

Commit de0bffb

Browse files
authored
[Ruby] Update Model Creation and Attribute Validation (#16114)
* update attribute validation and model creation * generate samples * update tests * update other tests * fix custom pet_spec tests * fix autoload tests
1 parent 4ece8e9 commit de0bffb

File tree

137 files changed

+1893
-2220
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

137 files changed

+1893
-2220
lines changed

modules/openapi-generator/src/main/resources/ruby-client/base_object.mustache

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,33 @@
22
# @param [Hash] attributes Model attributes in the form of hash
33
# @return [Object] Returns the model itself
44
def self.build_from_hash(attributes)
5-
new.build_from_hash(attributes)
6-
end
7-
8-
# Builds the object from hash
9-
# @param [Hash] attributes Model attributes in the form of hash
10-
# @return [Object] Returns the model itself
11-
def build_from_hash(attributes)
125
return nil unless attributes.is_a?(Hash)
136
{{#parent}}
147
super(attributes)
158
{{/parent}}
169
attributes = attributes.transform_keys(&:to_sym)
17-
self.class.openapi_types.each_pair do |key, type|
18-
if attributes[self.class.attribute_map[key]].nil? && self.class.openapi_nullable.include?(key)
19-
self.send("#{key}=", nil)
10+
transformed_hash = {}
11+
openapi_types.each_pair do |key, type|
12+
if attributes.key?(attribute_map[key]) && attributes[attribute_map[key]].nil?
13+
transformed_hash["#{key}"] = nil
2014
elsif type =~ /\AArray<(.*)>/i
2115
# check to ensure the input is an array given that the attribute
2216
# is documented as an array but the input is not
23-
if attributes[self.class.attribute_map[key]].is_a?(Array)
24-
self.send("#{key}=", attributes[self.class.attribute_map[key]].map { |v| _deserialize($1, v) })
17+
if attributes[attribute_map[key]].is_a?(Array)
18+
transformed_hash["#{key}"] = attributes[attribute_map[key]].map { |v| _deserialize($1, v) }
2519
end
26-
elsif !attributes[self.class.attribute_map[key]].nil?
27-
self.send("#{key}=", _deserialize(type, attributes[self.class.attribute_map[key]]))
20+
elsif !attributes[attribute_map[key]].nil?
21+
transformed_hash["#{key}"] = _deserialize(type, attributes[attribute_map[key]])
2822
end
2923
end
30-
31-
self
24+
new(transformed_hash)
3225
end
3326

3427
# Deserializes the data based on type
3528
# @param string type Data type
3629
# @param string value Value to be deserialized
3730
# @return [Object] Deserialized data
38-
def _deserialize(type, value)
31+
def self._deserialize(type, value)
3932
case type.to_sym
4033
when :Time
4134
Time.parse(value)

modules/openapi-generator/src/main/resources/ruby-client/partial_model_generic.mustache

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -146,13 +146,20 @@
146146
else
147147
self.{{{name}}} = {{{defaultValue}}}
148148
{{/defaultValue}}
149+
{{^defaultValue}}
150+
{{#required}}
151+
else
152+
self.{{{name}}} = nil
153+
{{/required}}
154+
{{/defaultValue}}
149155
end
150156
{{/vars}}
151157
end
152158

153159
# Show invalid properties with the reasons. Usually used together with valid?
154160
# @return Array for valid properties with the reasons
155161
def list_invalid_properties
162+
warn '[DEPRECATED] the `list_invalid_properties` method is obsolete'
156163
invalid_properties = {{^parent}}Array.new{{/parent}}{{#parent}}super{{/parent}}
157164
{{#vars}}
158165
{{^isNullable}}
@@ -215,6 +222,7 @@
215222
# Check to see if the all the properties in the model are valid
216223
# @return true if the model is valid
217224
def valid?
225+
warn '[DEPRECATED] the `valid?` method is obsolete'
218226
{{#vars}}
219227
{{^isNullable}}
220228
{{#required}}
@@ -291,52 +299,50 @@
291299
# @param [Object] {{{name}}} Value to be assigned
292300
def {{{name}}}=({{{name}}})
293301
{{^isNullable}}
294-
{{#required}}
295302
if {{{name}}}.nil?
296303
fail ArgumentError, '{{{name}}} cannot be nil'
297304
end
298305

299-
{{/required}}
300306
{{/isNullable}}
301307
{{#maxLength}}
302-
if {{^required}}!{{{name}}}.nil? && {{/required}}{{{name}}}.to_s.length > {{{maxLength}}}
308+
if {{#isNullable}}!{{{name}}}.nil? && {{/isNullable}}{{{name}}}.to_s.length > {{{maxLength}}}
303309
fail ArgumentError, 'invalid value for "{{{name}}}", the character length must be smaller than or equal to {{{maxLength}}}.'
304310
end
305311

306312
{{/maxLength}}
307313
{{#minLength}}
308-
if {{^required}}!{{{name}}}.nil? && {{/required}}{{{name}}}.to_s.length < {{{minLength}}}
314+
if {{#isNullable}}!{{{name}}}.nil? && {{/isNullable}}{{{name}}}.to_s.length < {{{minLength}}}
309315
fail ArgumentError, 'invalid value for "{{{name}}}", the character length must be great than or equal to {{{minLength}}}.'
310316
end
311317

312318
{{/minLength}}
313319
{{#maximum}}
314-
if {{^required}}!{{{name}}}.nil? && {{/required}}{{{name}}} >{{#exclusiveMaximum}}={{/exclusiveMaximum}} {{{maximum}}}
320+
if {{#isNullable}}!{{{name}}}.nil? && {{/isNullable}}{{{name}}} >{{#exclusiveMaximum}}={{/exclusiveMaximum}} {{{maximum}}}
315321
fail ArgumentError, 'invalid value for "{{{name}}}", must be smaller than {{^exclusiveMaximum}}or equal to {{/exclusiveMaximum}}{{{maximum}}}.'
316322
end
317323

318324
{{/maximum}}
319325
{{#minimum}}
320-
if {{^required}}!{{{name}}}.nil? && {{/required}}{{{name}}} <{{#exclusiveMinimum}}={{/exclusiveMinimum}} {{{minimum}}}
326+
if {{#isNullable}}!{{{name}}}.nil? && {{/isNullable}}{{{name}}} <{{#exclusiveMinimum}}={{/exclusiveMinimum}} {{{minimum}}}
321327
fail ArgumentError, 'invalid value for "{{{name}}}", must be greater than {{^exclusiveMinimum}}or equal to {{/exclusiveMinimum}}{{{minimum}}}.'
322328
end
323329

324330
{{/minimum}}
325331
{{#pattern}}
326332
pattern = Regexp.new({{{pattern}}})
327-
if {{^required}}!{{{name}}}.nil? && {{/required}}{{{name}}} !~ pattern
333+
if {{#isNullable}}!{{{name}}}.nil? && {{/isNullable}}{{{name}}} !~ pattern
328334
fail ArgumentError, "invalid value for \"{{{name}}}\", must conform to the pattern #{pattern}."
329335
end
330336

331337
{{/pattern}}
332338
{{#maxItems}}
333-
if {{^required}}!{{{name}}}.nil? && {{/required}}{{{name}}}.length > {{{maxItems}}}
339+
if {{#isNullable}}!{{{name}}}.nil? && {{/isNullable}}{{{name}}}.length > {{{maxItems}}}
334340
fail ArgumentError, 'invalid value for "{{{name}}}", number of items must be less than or equal to {{{maxItems}}}.'
335341
end
336342

337343
{{/maxItems}}
338344
{{#minItems}}
339-
if {{^required}}!{{{name}}}.nil? && {{/required}}{{{name}}}.length < {{{minItems}}}
345+
if {{#isNullable}}!{{{name}}}.nil? && {{/isNullable}}{{{name}}}.length < {{{minItems}}}
340346
fail ArgumentError, 'invalid value for "{{{name}}}", number of items must be greater than or equal to {{{minItems}}}.'
341347
end
342348

samples/client/petstore/ruby-autoload/lib/petstore/models/additional_properties_class.rb

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,15 @@ def initialize(attributes = {})
7777
# Show invalid properties with the reasons. Usually used together with valid?
7878
# @return Array for valid properties with the reasons
7979
def list_invalid_properties
80+
warn '[DEPRECATED] the `list_invalid_properties` method is obsolete'
8081
invalid_properties = Array.new
8182
invalid_properties
8283
end
8384

8485
# Check to see if the all the properties in the model are valid
8586
# @return true if the model is valid
8687
def valid?
88+
warn '[DEPRECATED] the `valid?` method is obsolete'
8789
true
8890
end
8991

@@ -112,37 +114,30 @@ def hash
112114
# @param [Hash] attributes Model attributes in the form of hash
113115
# @return [Object] Returns the model itself
114116
def self.build_from_hash(attributes)
115-
new.build_from_hash(attributes)
116-
end
117-
118-
# Builds the object from hash
119-
# @param [Hash] attributes Model attributes in the form of hash
120-
# @return [Object] Returns the model itself
121-
def build_from_hash(attributes)
122117
return nil unless attributes.is_a?(Hash)
123118
attributes = attributes.transform_keys(&:to_sym)
124-
self.class.openapi_types.each_pair do |key, type|
125-
if attributes[self.class.attribute_map[key]].nil? && self.class.openapi_nullable.include?(key)
126-
self.send("#{key}=", nil)
119+
transformed_hash = {}
120+
openapi_types.each_pair do |key, type|
121+
if attributes.key?(attribute_map[key]) && attributes[attribute_map[key]].nil?
122+
transformed_hash["#{key}"] = nil
127123
elsif type =~ /\AArray<(.*)>/i
128124
# check to ensure the input is an array given that the attribute
129125
# is documented as an array but the input is not
130-
if attributes[self.class.attribute_map[key]].is_a?(Array)
131-
self.send("#{key}=", attributes[self.class.attribute_map[key]].map { |v| _deserialize($1, v) })
126+
if attributes[attribute_map[key]].is_a?(Array)
127+
transformed_hash["#{key}"] = attributes[attribute_map[key]].map { |v| _deserialize($1, v) }
132128
end
133-
elsif !attributes[self.class.attribute_map[key]].nil?
134-
self.send("#{key}=", _deserialize(type, attributes[self.class.attribute_map[key]]))
129+
elsif !attributes[attribute_map[key]].nil?
130+
transformed_hash["#{key}"] = _deserialize(type, attributes[attribute_map[key]])
135131
end
136132
end
137-
138-
self
133+
new(transformed_hash)
139134
end
140135

141136
# Deserializes the data based on type
142137
# @param string type Data type
143138
# @param string value Value to be deserialized
144139
# @return [Object] Deserialized data
145-
def _deserialize(type, value)
140+
def self._deserialize(type, value)
146141
case type.to_sym
147142
when :Time
148143
Time.parse(value)

samples/client/petstore/ruby-autoload/lib/petstore/models/all_of_with_single_ref.rb

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,15 @@ def initialize(attributes = {})
9595
# Show invalid properties with the reasons. Usually used together with valid?
9696
# @return Array for valid properties with the reasons
9797
def list_invalid_properties
98+
warn '[DEPRECATED] the `list_invalid_properties` method is obsolete'
9899
invalid_properties = Array.new
99100
invalid_properties
100101
end
101102

102103
# Check to see if the all the properties in the model are valid
103104
# @return true if the model is valid
104105
def valid?
106+
warn '[DEPRECATED] the `valid?` method is obsolete'
105107
true
106108
end
107109

@@ -130,37 +132,30 @@ def hash
130132
# @param [Hash] attributes Model attributes in the form of hash
131133
# @return [Object] Returns the model itself
132134
def self.build_from_hash(attributes)
133-
new.build_from_hash(attributes)
134-
end
135-
136-
# Builds the object from hash
137-
# @param [Hash] attributes Model attributes in the form of hash
138-
# @return [Object] Returns the model itself
139-
def build_from_hash(attributes)
140135
return nil unless attributes.is_a?(Hash)
141136
attributes = attributes.transform_keys(&:to_sym)
142-
self.class.openapi_types.each_pair do |key, type|
143-
if attributes[self.class.attribute_map[key]].nil? && self.class.openapi_nullable.include?(key)
144-
self.send("#{key}=", nil)
137+
transformed_hash = {}
138+
openapi_types.each_pair do |key, type|
139+
if attributes.key?(attribute_map[key]) && attributes[attribute_map[key]].nil?
140+
transformed_hash["#{key}"] = nil
145141
elsif type =~ /\AArray<(.*)>/i
146142
# check to ensure the input is an array given that the attribute
147143
# is documented as an array but the input is not
148-
if attributes[self.class.attribute_map[key]].is_a?(Array)
149-
self.send("#{key}=", attributes[self.class.attribute_map[key]].map { |v| _deserialize($1, v) })
144+
if attributes[attribute_map[key]].is_a?(Array)
145+
transformed_hash["#{key}"] = attributes[attribute_map[key]].map { |v| _deserialize($1, v) }
150146
end
151-
elsif !attributes[self.class.attribute_map[key]].nil?
152-
self.send("#{key}=", _deserialize(type, attributes[self.class.attribute_map[key]]))
147+
elsif !attributes[attribute_map[key]].nil?
148+
transformed_hash["#{key}"] = _deserialize(type, attributes[attribute_map[key]])
153149
end
154150
end
155-
156-
self
151+
new(transformed_hash)
157152
end
158153

159154
# Deserializes the data based on type
160155
# @param string type Data type
161156
# @param string value Value to be deserialized
162157
# @return [Object] Deserialized data
163-
def _deserialize(type, value)
158+
def self._deserialize(type, value)
164159
case type.to_sym
165160
when :Time
166161
Time.parse(value)

samples/client/petstore/ruby-autoload/lib/petstore/models/animal.rb

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ def initialize(attributes = {})
6868

6969
if attributes.key?(:'class_name')
7070
self.class_name = attributes[:'class_name']
71+
else
72+
self.class_name = nil
7173
end
7274

7375
if attributes.key?(:'color')
@@ -80,6 +82,7 @@ def initialize(attributes = {})
8082
# Show invalid properties with the reasons. Usually used together with valid?
8183
# @return Array for valid properties with the reasons
8284
def list_invalid_properties
85+
warn '[DEPRECATED] the `list_invalid_properties` method is obsolete'
8386
invalid_properties = Array.new
8487
if @class_name.nil?
8588
invalid_properties.push('invalid value for "class_name", class_name cannot be nil.')
@@ -91,6 +94,7 @@ def list_invalid_properties
9194
# Check to see if the all the properties in the model are valid
9295
# @return true if the model is valid
9396
def valid?
97+
warn '[DEPRECATED] the `valid?` method is obsolete'
9498
return false if @class_name.nil?
9599
true
96100
end
@@ -120,37 +124,30 @@ def hash
120124
# @param [Hash] attributes Model attributes in the form of hash
121125
# @return [Object] Returns the model itself
122126
def self.build_from_hash(attributes)
123-
new.build_from_hash(attributes)
124-
end
125-
126-
# Builds the object from hash
127-
# @param [Hash] attributes Model attributes in the form of hash
128-
# @return [Object] Returns the model itself
129-
def build_from_hash(attributes)
130127
return nil unless attributes.is_a?(Hash)
131128
attributes = attributes.transform_keys(&:to_sym)
132-
self.class.openapi_types.each_pair do |key, type|
133-
if attributes[self.class.attribute_map[key]].nil? && self.class.openapi_nullable.include?(key)
134-
self.send("#{key}=", nil)
129+
transformed_hash = {}
130+
openapi_types.each_pair do |key, type|
131+
if attributes.key?(attribute_map[key]) && attributes[attribute_map[key]].nil?
132+
transformed_hash["#{key}"] = nil
135133
elsif type =~ /\AArray<(.*)>/i
136134
# check to ensure the input is an array given that the attribute
137135
# is documented as an array but the input is not
138-
if attributes[self.class.attribute_map[key]].is_a?(Array)
139-
self.send("#{key}=", attributes[self.class.attribute_map[key]].map { |v| _deserialize($1, v) })
136+
if attributes[attribute_map[key]].is_a?(Array)
137+
transformed_hash["#{key}"] = attributes[attribute_map[key]].map { |v| _deserialize($1, v) }
140138
end
141-
elsif !attributes[self.class.attribute_map[key]].nil?
142-
self.send("#{key}=", _deserialize(type, attributes[self.class.attribute_map[key]]))
139+
elsif !attributes[attribute_map[key]].nil?
140+
transformed_hash["#{key}"] = _deserialize(type, attributes[attribute_map[key]])
143141
end
144142
end
145-
146-
self
143+
new(transformed_hash)
147144
end
148145

149146
# Deserializes the data based on type
150147
# @param string type Data type
151148
# @param string value Value to be deserialized
152149
# @return [Object] Deserialized data
153-
def _deserialize(type, value)
150+
def self._deserialize(type, value)
154151
case type.to_sym
155152
when :Time
156153
Time.parse(value)

0 commit comments

Comments
 (0)