Skip to content

Commit c349270

Browse files
[Ruby] Fix ruby client generator to generate setter methods with not null validation (#20672)
* fix: Fix ruby client generator to generate setter methods with not null validation Not null validation was missing for required and non-nullable properties when no other validation was present. * fix: Fix an issue where the initialize method did not accept attributes defined in its parent class
1 parent 61ae569 commit c349270

File tree

217 files changed

+2328
-648
lines changed

Some content is hidden

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

217 files changed

+2328
-648
lines changed

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

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,21 @@
4242
}
4343
end
4444

45-
# Returns all the JSON keys this model knows about{{#parent}}, including the ones defined in its parent(s){{/parent}}
46-
def self.acceptable_attributes
45+
# Returns attribute mapping this model knows about{{#parent}}, including the ones defined in its parent(s){{/parent}}
46+
def self.acceptable_attribute_map
4747
{{^parent}}
48-
attribute_map.values
48+
attribute_map
4949
{{/parent}}
5050
{{#parent}}
51-
attribute_map.values.concat(superclass.acceptable_attributes)
51+
superclass.acceptable_attribute_map.merge(attribute_map)
5252
{{/parent}}
5353
end
5454

55+
# Returns all the JSON keys this model knows about{{#parent}}, including the ones defined in its parent(s){{/parent}}
56+
def self.acceptable_attributes
57+
acceptable_attribute_map.values
58+
end
59+
5560
# Attribute type mapping.
5661
def self.openapi_types
5762
{
@@ -115,9 +120,10 @@
115120
end
116121

117122
# check to see if the attribute exists and convert string to symbol for hash key
123+
acceptable_attribute_map = self.class.acceptable_attribute_map
118124
attributes = attributes.each_with_object({}) { |(k, v), h|
119-
if (!self.class.attribute_map.key?(k.to_sym))
120-
fail ArgumentError, "`#{k}` is not a valid attribute in `{{{moduleName}}}::{{{classname}}}`. Please check the name to make sure it's valid. List of attributes: " + self.class.attribute_map.keys.inspect
125+
if (!acceptable_attribute_map.key?(k.to_sym))
126+
fail ArgumentError, "`#{k}` is not a valid attribute in `{{{moduleName}}}::{{{classname}}}`. Please check the name to make sure it's valid. List of attributes: " + acceptable_attribute_map.keys.inspect
121127
end
122128
h[k.to_sym] = v
123129
}
@@ -350,6 +356,22 @@
350356
@{{{name}}} = {{{name}}}
351357
end
352358

359+
{{/hasValidation}}
360+
{{^hasValidation}}
361+
{{^isNullable}}
362+
{{#required}}
363+
# Custom attribute writer method with validation
364+
# @param [Object] {{{name}}} Value to be assigned
365+
def {{{name}}}=({{{name}}})
366+
if {{{name}}}.nil?
367+
fail ArgumentError, '{{{name}}} cannot be nil'
368+
end
369+
370+
@{{{name}}} = {{{name}}}
371+
end
372+
373+
{{/required}}
374+
{{/isNullable}}
353375
{{/hasValidation}}
354376
{{/isEnum}}
355377
{{/vars}}

samples/client/echo_api/ruby-faraday/lib/openapi_client/models/bird.rb

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,14 @@ def self.attribute_map
2727
}
2828
end
2929

30+
# Returns attribute mapping this model knows about
31+
def self.acceptable_attribute_map
32+
attribute_map
33+
end
34+
3035
# Returns all the JSON keys this model knows about
3136
def self.acceptable_attributes
32-
attribute_map.values
37+
acceptable_attribute_map.values
3338
end
3439

3540
# Attribute type mapping.
@@ -54,9 +59,10 @@ def initialize(attributes = {})
5459
end
5560

5661
# check to see if the attribute exists and convert string to symbol for hash key
62+
acceptable_attribute_map = self.class.acceptable_attribute_map
5763
attributes = attributes.each_with_object({}) { |(k, v), h|
58-
if (!self.class.attribute_map.key?(k.to_sym))
59-
fail ArgumentError, "`#{k}` is not a valid attribute in `OpenapiClient::Bird`. Please check the name to make sure it's valid. List of attributes: " + self.class.attribute_map.keys.inspect
64+
if (!acceptable_attribute_map.key?(k.to_sym))
65+
fail ArgumentError, "`#{k}` is not a valid attribute in `OpenapiClient::Bird`. Please check the name to make sure it's valid. List of attributes: " + acceptable_attribute_map.keys.inspect
6066
end
6167
h[k.to_sym] = v
6268
}

samples/client/echo_api/ruby-faraday/lib/openapi_client/models/category.rb

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,14 @@ def self.attribute_map
2727
}
2828
end
2929

30+
# Returns attribute mapping this model knows about
31+
def self.acceptable_attribute_map
32+
attribute_map
33+
end
34+
3035
# Returns all the JSON keys this model knows about
3136
def self.acceptable_attributes
32-
attribute_map.values
37+
acceptable_attribute_map.values
3338
end
3439

3540
# Attribute type mapping.
@@ -54,9 +59,10 @@ def initialize(attributes = {})
5459
end
5560

5661
# check to see if the attribute exists and convert string to symbol for hash key
62+
acceptable_attribute_map = self.class.acceptable_attribute_map
5763
attributes = attributes.each_with_object({}) { |(k, v), h|
58-
if (!self.class.attribute_map.key?(k.to_sym))
59-
fail ArgumentError, "`#{k}` is not a valid attribute in `OpenapiClient::Category`. Please check the name to make sure it's valid. List of attributes: " + self.class.attribute_map.keys.inspect
64+
if (!acceptable_attribute_map.key?(k.to_sym))
65+
fail ArgumentError, "`#{k}` is not a valid attribute in `OpenapiClient::Category`. Please check the name to make sure it's valid. List of attributes: " + acceptable_attribute_map.keys.inspect
6066
end
6167
h[k.to_sym] = v
6268
}

samples/client/echo_api/ruby-faraday/lib/openapi_client/models/data_query.rb

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,14 @@ def self.attribute_map
5555
}
5656
end
5757

58+
# Returns attribute mapping this model knows about, including the ones defined in its parent(s)
59+
def self.acceptable_attribute_map
60+
superclass.acceptable_attribute_map.merge(attribute_map)
61+
end
62+
5863
# Returns all the JSON keys this model knows about, including the ones defined in its parent(s)
5964
def self.acceptable_attributes
60-
attribute_map.values.concat(superclass.acceptable_attributes)
65+
acceptable_attribute_map.values
6166
end
6267

6368
# Attribute type mapping.
@@ -90,9 +95,10 @@ def initialize(attributes = {})
9095
end
9196

9297
# check to see if the attribute exists and convert string to symbol for hash key
98+
acceptable_attribute_map = self.class.acceptable_attribute_map
9399
attributes = attributes.each_with_object({}) { |(k, v), h|
94-
if (!self.class.attribute_map.key?(k.to_sym))
95-
fail ArgumentError, "`#{k}` is not a valid attribute in `OpenapiClient::DataQuery`. Please check the name to make sure it's valid. List of attributes: " + self.class.attribute_map.keys.inspect
100+
if (!acceptable_attribute_map.key?(k.to_sym))
101+
fail ArgumentError, "`#{k}` is not a valid attribute in `OpenapiClient::DataQuery`. Please check the name to make sure it's valid. List of attributes: " + acceptable_attribute_map.keys.inspect
96102
end
97103
h[k.to_sym] = v
98104
}

samples/client/echo_api/ruby-faraday/lib/openapi_client/models/default_value.rb

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,14 @@ def self.attribute_map
6868
}
6969
end
7070

71+
# Returns attribute mapping this model knows about
72+
def self.acceptable_attribute_map
73+
attribute_map
74+
end
75+
7176
# Returns all the JSON keys this model knows about
7277
def self.acceptable_attributes
73-
attribute_map.values
78+
acceptable_attribute_map.values
7479
end
7580

7681
# Attribute type mapping.
@@ -104,9 +109,10 @@ def initialize(attributes = {})
104109
end
105110

106111
# check to see if the attribute exists and convert string to symbol for hash key
112+
acceptable_attribute_map = self.class.acceptable_attribute_map
107113
attributes = attributes.each_with_object({}) { |(k, v), h|
108-
if (!self.class.attribute_map.key?(k.to_sym))
109-
fail ArgumentError, "`#{k}` is not a valid attribute in `OpenapiClient::DefaultValue`. Please check the name to make sure it's valid. List of attributes: " + self.class.attribute_map.keys.inspect
114+
if (!acceptable_attribute_map.key?(k.to_sym))
115+
fail ArgumentError, "`#{k}` is not a valid attribute in `OpenapiClient::DefaultValue`. Please check the name to make sure it's valid. List of attributes: " + acceptable_attribute_map.keys.inspect
110116
end
111117
h[k.to_sym] = v
112118
}

samples/client/echo_api/ruby-faraday/lib/openapi_client/models/number_properties_only.rb

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,14 @@ def self.attribute_map
3030
}
3131
end
3232

33+
# Returns attribute mapping this model knows about
34+
def self.acceptable_attribute_map
35+
attribute_map
36+
end
37+
3338
# Returns all the JSON keys this model knows about
3439
def self.acceptable_attributes
35-
attribute_map.values
40+
acceptable_attribute_map.values
3641
end
3742

3843
# Attribute type mapping.
@@ -58,9 +63,10 @@ def initialize(attributes = {})
5863
end
5964

6065
# check to see if the attribute exists and convert string to symbol for hash key
66+
acceptable_attribute_map = self.class.acceptable_attribute_map
6167
attributes = attributes.each_with_object({}) { |(k, v), h|
62-
if (!self.class.attribute_map.key?(k.to_sym))
63-
fail ArgumentError, "`#{k}` is not a valid attribute in `OpenapiClient::NumberPropertiesOnly`. Please check the name to make sure it's valid. List of attributes: " + self.class.attribute_map.keys.inspect
68+
if (!acceptable_attribute_map.key?(k.to_sym))
69+
fail ArgumentError, "`#{k}` is not a valid attribute in `OpenapiClient::NumberPropertiesOnly`. Please check the name to make sure it's valid. List of attributes: " + acceptable_attribute_map.keys.inspect
6470
end
6571
h[k.to_sym] = v
6672
}

samples/client/echo_api/ruby-faraday/lib/openapi_client/models/pet.rb

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,14 @@ def self.attribute_map
6262
}
6363
end
6464

65+
# Returns attribute mapping this model knows about
66+
def self.acceptable_attribute_map
67+
attribute_map
68+
end
69+
6570
# Returns all the JSON keys this model knows about
6671
def self.acceptable_attributes
67-
attribute_map.values
72+
acceptable_attribute_map.values
6873
end
6974

7075
# Attribute type mapping.
@@ -93,9 +98,10 @@ def initialize(attributes = {})
9398
end
9499

95100
# check to see if the attribute exists and convert string to symbol for hash key
101+
acceptable_attribute_map = self.class.acceptable_attribute_map
96102
attributes = attributes.each_with_object({}) { |(k, v), h|
97-
if (!self.class.attribute_map.key?(k.to_sym))
98-
fail ArgumentError, "`#{k}` is not a valid attribute in `OpenapiClient::Pet`. Please check the name to make sure it's valid. List of attributes: " + self.class.attribute_map.keys.inspect
103+
if (!acceptable_attribute_map.key?(k.to_sym))
104+
fail ArgumentError, "`#{k}` is not a valid attribute in `OpenapiClient::Pet`. Please check the name to make sure it's valid. List of attributes: " + acceptable_attribute_map.keys.inspect
99105
end
100106
h[k.to_sym] = v
101107
}
@@ -160,6 +166,26 @@ def valid?
160166
true
161167
end
162168

169+
# Custom attribute writer method with validation
170+
# @param [Object] name Value to be assigned
171+
def name=(name)
172+
if name.nil?
173+
fail ArgumentError, 'name cannot be nil'
174+
end
175+
176+
@name = name
177+
end
178+
179+
# Custom attribute writer method with validation
180+
# @param [Object] photo_urls Value to be assigned
181+
def photo_urls=(photo_urls)
182+
if photo_urls.nil?
183+
fail ArgumentError, 'photo_urls cannot be nil'
184+
end
185+
186+
@photo_urls = photo_urls
187+
end
188+
163189
# Custom attribute writer method checking allowed values (enum).
164190
# @param [Object] status Object to be assigned
165191
def status=(status)

samples/client/echo_api/ruby-faraday/lib/openapi_client/models/query.rb

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,14 @@ def self.attribute_map
5050
}
5151
end
5252

53+
# Returns attribute mapping this model knows about
54+
def self.acceptable_attribute_map
55+
attribute_map
56+
end
57+
5358
# Returns all the JSON keys this model knows about
5459
def self.acceptable_attributes
55-
attribute_map.values
60+
acceptable_attribute_map.values
5661
end
5762

5863
# Attribute type mapping.
@@ -77,9 +82,10 @@ def initialize(attributes = {})
7782
end
7883

7984
# check to see if the attribute exists and convert string to symbol for hash key
85+
acceptable_attribute_map = self.class.acceptable_attribute_map
8086
attributes = attributes.each_with_object({}) { |(k, v), h|
81-
if (!self.class.attribute_map.key?(k.to_sym))
82-
fail ArgumentError, "`#{k}` is not a valid attribute in `OpenapiClient::Query`. Please check the name to make sure it's valid. List of attributes: " + self.class.attribute_map.keys.inspect
87+
if (!acceptable_attribute_map.key?(k.to_sym))
88+
fail ArgumentError, "`#{k}` is not a valid attribute in `OpenapiClient::Query`. Please check the name to make sure it's valid. List of attributes: " + acceptable_attribute_map.keys.inspect
8389
end
8490
h[k.to_sym] = v
8591
}

samples/client/echo_api/ruby-faraday/lib/openapi_client/models/tag.rb

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,14 @@ def self.attribute_map
2727
}
2828
end
2929

30+
# Returns attribute mapping this model knows about
31+
def self.acceptable_attribute_map
32+
attribute_map
33+
end
34+
3035
# Returns all the JSON keys this model knows about
3136
def self.acceptable_attributes
32-
attribute_map.values
37+
acceptable_attribute_map.values
3338
end
3439

3540
# Attribute type mapping.
@@ -54,9 +59,10 @@ def initialize(attributes = {})
5459
end
5560

5661
# check to see if the attribute exists and convert string to symbol for hash key
62+
acceptable_attribute_map = self.class.acceptable_attribute_map
5763
attributes = attributes.each_with_object({}) { |(k, v), h|
58-
if (!self.class.attribute_map.key?(k.to_sym))
59-
fail ArgumentError, "`#{k}` is not a valid attribute in `OpenapiClient::Tag`. Please check the name to make sure it's valid. List of attributes: " + self.class.attribute_map.keys.inspect
64+
if (!acceptable_attribute_map.key?(k.to_sym))
65+
fail ArgumentError, "`#{k}` is not a valid attribute in `OpenapiClient::Tag`. Please check the name to make sure it's valid. List of attributes: " + acceptable_attribute_map.keys.inspect
6066
end
6167
h[k.to_sym] = v
6268
}

samples/client/echo_api/ruby-faraday/lib/openapi_client/models/test_form_object_multipart_request_marker.rb

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,14 @@ def self.attribute_map
2424
}
2525
end
2626

27+
# Returns attribute mapping this model knows about
28+
def self.acceptable_attribute_map
29+
attribute_map
30+
end
31+
2732
# Returns all the JSON keys this model knows about
2833
def self.acceptable_attributes
29-
attribute_map.values
34+
acceptable_attribute_map.values
3035
end
3136

3237
# Attribute type mapping.
@@ -50,9 +55,10 @@ def initialize(attributes = {})
5055
end
5156

5257
# check to see if the attribute exists and convert string to symbol for hash key
58+
acceptable_attribute_map = self.class.acceptable_attribute_map
5359
attributes = attributes.each_with_object({}) { |(k, v), h|
54-
if (!self.class.attribute_map.key?(k.to_sym))
55-
fail ArgumentError, "`#{k}` is not a valid attribute in `OpenapiClient::TestFormObjectMultipartRequestMarker`. Please check the name to make sure it's valid. List of attributes: " + self.class.attribute_map.keys.inspect
60+
if (!acceptable_attribute_map.key?(k.to_sym))
61+
fail ArgumentError, "`#{k}` is not a valid attribute in `OpenapiClient::TestFormObjectMultipartRequestMarker`. Please check the name to make sure it's valid. List of attributes: " + acceptable_attribute_map.keys.inspect
5662
end
5763
h[k.to_sym] = v
5864
}

0 commit comments

Comments
 (0)