Skip to content

Commit 2846ac4

Browse files
committed
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.
1 parent 8167aa1 commit 2846ac4

File tree

35 files changed

+399
-3
lines changed

35 files changed

+399
-3
lines changed

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,22 @@
350350
@{{{name}}} = {{{name}}}
351351
end
352352

353+
{{/hasValidation}}
354+
{{^hasValidation}}
355+
{{^isNullable}}
356+
{{#required}}
357+
# Custom attribute writer method with validation
358+
# @param [Object] {{{name}}} Value to be assigned
359+
def {{{name}}}=({{{name}}})
360+
if {{{name}}}.nil?
361+
fail ArgumentError, '{{{name}}} cannot be nil'
362+
end
363+
364+
@{{{name}}} = {{{name}}}
365+
end
366+
367+
{{/required}}
368+
{{/isNullable}}
353369
{{/hasValidation}}
354370
{{/isEnum}}
355371
{{/vars}}

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,26 @@ def valid?
160160
true
161161
end
162162

163+
# Custom attribute writer method with validation
164+
# @param [Object] name Value to be assigned
165+
def name=(name)
166+
if name.nil?
167+
fail ArgumentError, 'name cannot be nil'
168+
end
169+
170+
@name = name
171+
end
172+
173+
# Custom attribute writer method with validation
174+
# @param [Object] photo_urls Value to be assigned
175+
def photo_urls=(photo_urls)
176+
if photo_urls.nil?
177+
fail ArgumentError, 'photo_urls cannot be nil'
178+
end
179+
180+
@photo_urls = photo_urls
181+
end
182+
163183
# Custom attribute writer method checking allowed values (enum).
164184
# @param [Object] status Object to be assigned
165185
def status=(status)

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,26 @@ def valid?
160160
true
161161
end
162162

163+
# Custom attribute writer method with validation
164+
# @param [Object] name Value to be assigned
165+
def name=(name)
166+
if name.nil?
167+
fail ArgumentError, 'name cannot be nil'
168+
end
169+
170+
@name = name
171+
end
172+
173+
# Custom attribute writer method with validation
174+
# @param [Object] photo_urls Value to be assigned
175+
def photo_urls=(photo_urls)
176+
if photo_urls.nil?
177+
fail ArgumentError, 'photo_urls cannot be nil'
178+
end
179+
180+
@photo_urls = photo_urls
181+
end
182+
163183
# Custom attribute writer method checking allowed values (enum).
164184
# @param [Object] status Object to be assigned
165185
def status=(status)

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,26 @@ def valid?
160160
true
161161
end
162162

163+
# Custom attribute writer method with validation
164+
# @param [Object] name Value to be assigned
165+
def name=(name)
166+
if name.nil?
167+
fail ArgumentError, 'name cannot be nil'
168+
end
169+
170+
@name = name
171+
end
172+
173+
# Custom attribute writer method with validation
174+
# @param [Object] photo_urls Value to be assigned
175+
def photo_urls=(photo_urls)
176+
if photo_urls.nil?
177+
fail ArgumentError, 'photo_urls cannot be nil'
178+
end
179+
180+
@photo_urls = photo_urls
181+
end
182+
163183
# Custom attribute writer method checking allowed values (enum).
164184
# @param [Object] status Object to be assigned
165185
def status=(status)

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,16 @@ def valid?
9999
true
100100
end
101101

102+
# Custom attribute writer method with validation
103+
# @param [Object] class_name Value to be assigned
104+
def class_name=(class_name)
105+
if class_name.nil?
106+
fail ArgumentError, 'class_name cannot be nil'
107+
end
108+
109+
@class_name = class_name
110+
end
111+
102112
# Checks equality by comparing each attribute.
103113
# @param [Object] Object to be compared
104114
def ==(o)

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,16 @@ def valid?
9292
true
9393
end
9494

95+
# Custom attribute writer method with validation
96+
# @param [Object] name Value to be assigned
97+
def name=(name)
98+
if name.nil?
99+
fail ArgumentError, 'name cannot be nil'
100+
end
101+
102+
@name = name
103+
end
104+
95105
# Checks equality by comparing each attribute.
96106
# @param [Object] Object to be compared
97107
def ==(o)

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,26 @@ def string=(string)
410410
@string = string
411411
end
412412

413+
# Custom attribute writer method with validation
414+
# @param [Object] byte Value to be assigned
415+
def byte=(byte)
416+
if byte.nil?
417+
fail ArgumentError, 'byte cannot be nil'
418+
end
419+
420+
@byte = byte
421+
end
422+
423+
# Custom attribute writer method with validation
424+
# @param [Object] date Value to be assigned
425+
def date=(date)
426+
if date.nil?
427+
fail ArgumentError, 'date cannot be nil'
428+
end
429+
430+
@date = date
431+
end
432+
413433
# Custom attribute writer method with validation
414434
# @param [Object] password Value to be assigned
415435
def password=(password)

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,16 @@ def valid?
109109
true
110110
end
111111

112+
# Custom attribute writer method with validation
113+
# @param [Object] name Value to be assigned
114+
def name=(name)
115+
if name.nil?
116+
fail ArgumentError, 'name cannot be nil'
117+
end
118+
119+
@name = name
120+
end
121+
112122
# Checks equality by comparing each attribute.
113123
# @param [Object] Object to be compared
114124
def ==(o)

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,16 @@ def valid?
106106
true
107107
end
108108

109+
# Custom attribute writer method with validation
110+
# @param [Object] value Value to be assigned
111+
def value=(value)
112+
if value.nil?
113+
fail ArgumentError, 'value cannot be nil'
114+
end
115+
116+
@value = value
117+
end
118+
109119
# Checks equality by comparing each attribute.
110120
# @param [Object] Object to be compared
111121
def ==(o)

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,16 @@ def valid?
160160
true
161161
end
162162

163+
# Custom attribute writer method with validation
164+
# @param [Object] name Value to be assigned
165+
def name=(name)
166+
if name.nil?
167+
fail ArgumentError, 'name cannot be nil'
168+
end
169+
170+
@name = name
171+
end
172+
163173
# Custom attribute writer method with validation
164174
# @param [Object] photo_urls Value to be assigned
165175
def photo_urls=(photo_urls)

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,16 @@ def valid?
9999
true
100100
end
101101

102+
# Custom attribute writer method with validation
103+
# @param [Object] class_name Value to be assigned
104+
def class_name=(class_name)
105+
if class_name.nil?
106+
fail ArgumentError, 'class_name cannot be nil'
107+
end
108+
109+
@class_name = class_name
110+
end
111+
102112
# Checks equality by comparing each attribute.
103113
# @param [Object] Object to be compared
104114
def ==(o)

samples/client/petstore/ruby-faraday/lib/petstore/models/category.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,16 @@ def valid?
9292
true
9393
end
9494

95+
# Custom attribute writer method with validation
96+
# @param [Object] name Value to be assigned
97+
def name=(name)
98+
if name.nil?
99+
fail ArgumentError, 'name cannot be nil'
100+
end
101+
102+
@name = name
103+
end
104+
95105
# Checks equality by comparing each attribute.
96106
# @param [Object] Object to be compared
97107
def ==(o)

samples/client/petstore/ruby-faraday/lib/petstore/models/format_test.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,26 @@ def string=(string)
410410
@string = string
411411
end
412412

413+
# Custom attribute writer method with validation
414+
# @param [Object] byte Value to be assigned
415+
def byte=(byte)
416+
if byte.nil?
417+
fail ArgumentError, 'byte cannot be nil'
418+
end
419+
420+
@byte = byte
421+
end
422+
423+
# Custom attribute writer method with validation
424+
# @param [Object] date Value to be assigned
425+
def date=(date)
426+
if date.nil?
427+
fail ArgumentError, 'date cannot be nil'
428+
end
429+
430+
@date = date
431+
end
432+
413433
# Custom attribute writer method with validation
414434
# @param [Object] password Value to be assigned
415435
def password=(password)

samples/client/petstore/ruby-faraday/lib/petstore/models/name.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,16 @@ def valid?
109109
true
110110
end
111111

112+
# Custom attribute writer method with validation
113+
# @param [Object] name Value to be assigned
114+
def name=(name)
115+
if name.nil?
116+
fail ArgumentError, 'name cannot be nil'
117+
end
118+
119+
@name = name
120+
end
121+
112122
# Checks equality by comparing each attribute.
113123
# @param [Object] Object to be compared
114124
def ==(o)

samples/client/petstore/ruby-faraday/lib/petstore/models/outer_object_with_enum_property.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,16 @@ def valid?
106106
true
107107
end
108108

109+
# Custom attribute writer method with validation
110+
# @param [Object] value Value to be assigned
111+
def value=(value)
112+
if value.nil?
113+
fail ArgumentError, 'value cannot be nil'
114+
end
115+
116+
@value = value
117+
end
118+
109119
# Checks equality by comparing each attribute.
110120
# @param [Object] Object to be compared
111121
def ==(o)

samples/client/petstore/ruby-faraday/lib/petstore/models/pet.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,16 @@ def valid?
160160
true
161161
end
162162

163+
# Custom attribute writer method with validation
164+
# @param [Object] name Value to be assigned
165+
def name=(name)
166+
if name.nil?
167+
fail ArgumentError, 'name cannot be nil'
168+
end
169+
170+
@name = name
171+
end
172+
163173
# Custom attribute writer method with validation
164174
# @param [Object] photo_urls Value to be assigned
165175
def photo_urls=(photo_urls)

samples/client/petstore/ruby-faraday/spec/custom/base_object_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ def initialize(attributes = {})
9494

9595
describe 'BaseObject' do
9696
describe 'boolean values' do
97-
let(:obj) { Petstore::Cat.new(declawed: false) }
97+
let(:obj) { Petstore::Cat.new(class_name: "CAT", declawed: false) }
9898

9999
it 'should have values set' do
100100
expect(obj.declawed).not_to be_nil

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,16 @@ def valid?
9999
true
100100
end
101101

102+
# Custom attribute writer method with validation
103+
# @param [Object] class_name Value to be assigned
104+
def class_name=(class_name)
105+
if class_name.nil?
106+
fail ArgumentError, 'class_name cannot be nil'
107+
end
108+
109+
@class_name = class_name
110+
end
111+
102112
# Checks equality by comparing each attribute.
103113
# @param [Object] Object to be compared
104114
def ==(o)

samples/client/petstore/ruby-httpx/lib/petstore/models/category.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,16 @@ def valid?
9292
true
9393
end
9494

95+
# Custom attribute writer method with validation
96+
# @param [Object] name Value to be assigned
97+
def name=(name)
98+
if name.nil?
99+
fail ArgumentError, 'name cannot be nil'
100+
end
101+
102+
@name = name
103+
end
104+
95105
# Checks equality by comparing each attribute.
96106
# @param [Object] Object to be compared
97107
def ==(o)

samples/client/petstore/ruby-httpx/lib/petstore/models/format_test.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,26 @@ def string=(string)
410410
@string = string
411411
end
412412

413+
# Custom attribute writer method with validation
414+
# @param [Object] byte Value to be assigned
415+
def byte=(byte)
416+
if byte.nil?
417+
fail ArgumentError, 'byte cannot be nil'
418+
end
419+
420+
@byte = byte
421+
end
422+
423+
# Custom attribute writer method with validation
424+
# @param [Object] date Value to be assigned
425+
def date=(date)
426+
if date.nil?
427+
fail ArgumentError, 'date cannot be nil'
428+
end
429+
430+
@date = date
431+
end
432+
413433
# Custom attribute writer method with validation
414434
# @param [Object] password Value to be assigned
415435
def password=(password)

0 commit comments

Comments
 (0)