Skip to content

Commit 3744a3f

Browse files
authored
add option to skip client side validation in ruby client (#5528)
1 parent 53b25b1 commit 3744a3f

File tree

10 files changed

+134
-65
lines changed

10 files changed

+134
-65
lines changed

modules/swagger-codegen/src/main/resources/ruby/api.mustache

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,13 @@ module {{moduleName}}
3838
{{#allParams}}
3939
{{#required}}
4040
# verify the required parameter '{{paramName}}' is set
41-
fail ArgumentError, "Missing the required parameter '{{paramName}}' when calling {{classname}}.{{operationId}}" if {{{paramName}}}.nil?
41+
if @api_client.config.client_side_validation && {{{paramName}}}.nil?
42+
fail ArgumentError, "Missing the required parameter '{{paramName}}' when calling {{classname}}.{{operationId}}"
43+
end
4244
{{#isEnum}}
4345
{{^isContainer}}
4446
# verify enum value
45-
unless [{{#allowableValues}}{{#values}}'{{{this}}}'{{^-last}}, {{/-last}}{{/values}}{{/allowableValues}}].include?({{{paramName}}})
47+
if @api_client.config.client_side_validation && ![{{#allowableValues}}{{#values}}'{{{this}}}'{{^-last}}, {{/-last}}{{/values}}{{/allowableValues}}].include?({{{paramName}}})
4648
fail ArgumentError, "invalid value for '{{{paramName}}}', must be one of {{#allowableValues}}{{#values}}{{{this}}}{{^-last}}, {{/-last}}{{/values}}{{/allowableValues}}"
4749
end
4850
{{/isContainer}}
@@ -51,56 +53,56 @@ module {{moduleName}}
5153
{{^required}}
5254
{{#isEnum}}
5355
{{#collectionFormat}}
54-
if opts[:'{{{paramName}}}'] && !opts[:'{{{paramName}}}'].all?{|item| [{{#allowableValues}}{{#values}}'{{{this}}}'{{^-last}}, {{/-last}}{{/values}}{{/allowableValues}}].include?(item)}
56+
if @api_client.config.client_side_validation && opts[:'{{{paramName}}}'] && !opts[:'{{{paramName}}}'].all?{|item| [{{#allowableValues}}{{#values}}'{{{this}}}'{{^-last}}, {{/-last}}{{/values}}{{/allowableValues}}].include?(item)}
5557
fail ArgumentError, 'invalid value for "{{{paramName}}}", must include one of {{#allowableValues}}{{#values}}{{{this}}}{{^-last}}, {{/-last}}{{/values}}{{/allowableValues}}'
5658
end
5759
{{/collectionFormat}}
5860
{{^collectionFormat}}
59-
if opts[:'{{{paramName}}}'] && ![{{#allowableValues}}{{#values}}'{{{this}}}'{{^-last}}, {{/-last}}{{/values}}{{/allowableValues}}].include?(opts[:'{{{paramName}}}'])
61+
if @api_client.config.client_side_validation && opts[:'{{{paramName}}}'] && ![{{#allowableValues}}{{#values}}'{{{this}}}'{{^-last}}, {{/-last}}{{/values}}{{/allowableValues}}].include?(opts[:'{{{paramName}}}'])
6062
fail ArgumentError, 'invalid value for "{{{paramName}}}", must be one of {{#allowableValues}}{{#values}}{{{this}}}{{^-last}}, {{/-last}}{{/values}}{{/allowableValues}}'
6163
end
6264
{{/collectionFormat}}
6365
{{/isEnum}}
6466
{{/required}}
6567
{{#hasValidation}}
6668
{{#maxLength}}
67-
if {{^required}}!opts[:'{{{paramName}}}'].nil? && {{/required}}{{#required}}{{{paramName}}}{{/required}}{{^required}}opts[:'{{{paramName}}}']{{/required}}.to_s.length > {{{maxLength}}}
69+
if @api_client.config.client_side_validation && {{^required}}!opts[:'{{{paramName}}}'].nil? && {{/required}}{{#required}}{{{paramName}}}{{/required}}{{^required}}opts[:'{{{paramName}}}']{{/required}}.to_s.length > {{{maxLength}}}
6870
fail ArgumentError, 'invalid value for "{{#required}}{{{paramName}}}{{/required}}{{^required}}opts[:"{{{paramName}}}"]{{/required}}" when calling {{classname}}.{{operationId}}, the character length must be smaller than or equal to {{{maxLength}}}.'
6971
end
7072

7173
{{/maxLength}}
7274
{{#minLength}}
73-
if {{^required}}!opts[:'{{{paramName}}}'].nil? && {{/required}}{{#required}}{{{paramName}}}{{/required}}{{^required}}opts[:'{{{paramName}}}']{{/required}}.to_s.length < {{{minLength}}}
75+
if @api_client.config.client_side_validation && {{^required}}!opts[:'{{{paramName}}}'].nil? && {{/required}}{{#required}}{{{paramName}}}{{/required}}{{^required}}opts[:'{{{paramName}}}']{{/required}}.to_s.length < {{{minLength}}}
7476
fail ArgumentError, 'invalid value for "{{#required}}{{{paramName}}}{{/required}}{{^required}}opts[:"{{{paramName}}}"]{{/required}}" when calling {{classname}}.{{operationId}}, the character length must be great than or equal to {{{minLength}}}.'
7577
end
7678

7779
{{/minLength}}
7880
{{#maximum}}
79-
if {{^required}}!opts[:'{{{paramName}}}'].nil? && {{/required}}{{#required}}{{{paramName}}}{{/required}}{{^required}}opts[:'{{{paramName}}}']{{/required}} >{{#exclusiveMaximum}}={{/exclusiveMaximum}} {{{maximum}}}
81+
if @api_client.config.client_side_validation && {{^required}}!opts[:'{{{paramName}}}'].nil? && {{/required}}{{#required}}{{{paramName}}}{{/required}}{{^required}}opts[:'{{{paramName}}}']{{/required}} >{{#exclusiveMaximum}}={{/exclusiveMaximum}} {{{maximum}}}
8082
fail ArgumentError, 'invalid value for "{{#required}}{{{paramName}}}{{/required}}{{^required}}opts[:"{{{paramName}}}"]{{/required}}" when calling {{classname}}.{{operationId}}, must be smaller than {{^exclusiveMaximum}}or equal to {{/exclusiveMaximum}}{{{maximum}}}.'
8183
end
8284

8385
{{/maximum}}
8486
{{#minimum}}
85-
if {{^required}}!opts[:'{{{paramName}}}'].nil? && {{/required}}{{#required}}{{{paramName}}}{{/required}}{{^required}}opts[:'{{{paramName}}}']{{/required}} <{{#exclusiveMinimum}}={{/exclusiveMinimum}} {{{minimum}}}
87+
if @api_client.config.client_side_validation && {{^required}}!opts[:'{{{paramName}}}'].nil? && {{/required}}{{#required}}{{{paramName}}}{{/required}}{{^required}}opts[:'{{{paramName}}}']{{/required}} <{{#exclusiveMinimum}}={{/exclusiveMinimum}} {{{minimum}}}
8688
fail ArgumentError, 'invalid value for "{{#required}}{{{paramName}}}{{/required}}{{^required}}opts[:"{{{paramName}}}"]{{/required}}" when calling {{classname}}.{{operationId}}, must be greater than {{^exclusiveMinimum}}or equal to {{/exclusiveMinimum}}{{{minimum}}}.'
8789
end
8890

8991
{{/minimum}}
9092
{{#pattern}}
91-
if {{^required}}!opts[:'{{{paramName}}}'].nil? && {{/required}}{{#required}}{{{paramName}}}{{/required}}{{^required}}opts[:'{{{paramName}}}']{{/required}} !~ Regexp.new({{{pattern}}})
93+
if @api_client.config.client_side_validation && {{^required}}!opts[:'{{{paramName}}}'].nil? && {{/required}}{{#required}}{{{paramName}}}{{/required}}{{^required}}opts[:'{{{paramName}}}']{{/required}} !~ Regexp.new({{{pattern}}})
9294
fail ArgumentError, "invalid value for '{{#required}}{{{paramName}}}{{/required}}{{^required}}opts[:\"{{{paramName}}}\"]{{/required}}' when calling {{classname}}.{{operationId}}, must conform to the pattern {{{pattern}}}."
9395
end
9496

9597
{{/pattern}}
9698
{{#maxItems}}
97-
if {{^required}}!opts[:'{{{paramName}}}'].nil? && {{/required}}{{#required}}{{{paramName}}}{{/required}}{{^required}}opts[:'{{{paramName}}}']{{/required}}.length > {{{maxItems}}}
99+
if @api_client.config.client_side_validation && {{^required}}!opts[:'{{{paramName}}}'].nil? && {{/required}}{{#required}}{{{paramName}}}{{/required}}{{^required}}opts[:'{{{paramName}}}']{{/required}}.length > {{{maxItems}}}
98100
fail ArgumentError, 'invalid value for "{{#required}}{{{paramName}}}{{/required}}{{^required}}opts[:"{{{paramName}}}"]{{/required}}" when calling {{classname}}.{{operationId}}, number of items must be less than or equal to {{{maxItems}}}.'
99101
end
100102

101103
{{/maxItems}}
102104
{{#minItems}}
103-
if {{^required}}!opts[:'{{{paramName}}}'].nil? && {{/required}}{{#required}}{{{paramName}}}{{/required}}{{^required}}opts[:'{{{paramName}}}']{{/required}}.length < {{{minItems}}}
105+
if @api_client.config.client_side_validation && {{^required}}!opts[:'{{{paramName}}}'].nil? && {{/required}}{{#required}}{{{paramName}}}{{/required}}{{^required}}opts[:'{{{paramName}}}']{{/required}}.length < {{{minItems}}}
104106
fail ArgumentError, 'invalid value for "{{#required}}{{{paramName}}}{{/required}}{{^required}}opts[:"{{{paramName}}}"]{{/required}}" when calling {{classname}}.{{operationId}}, number of items must be greater than or equal to {{{minItems}}}.'
105107
end
106108

modules/swagger-codegen/src/main/resources/ruby/configuration.mustache

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ module {{moduleName}}
6868
# Default to 0 (never times out).
6969
attr_accessor :timeout
7070

71+
# Set this to false to skip client side validation in the operation.
72+
# Default to true.
73+
# @return [true, false]
74+
attr_accessor :client_side_validation
75+
7176
### TLS/SSL setting
7277
# Set this to false to skip verifying SSL certificate when calling API from https server.
7378
# Default to true.
@@ -121,6 +126,7 @@ module {{moduleName}}
121126
@api_key = {}
122127
@api_key_prefix = {}
123128
@timeout = 0
129+
@client_side_validation = true
124130
@verify_ssl = true
125131
@verify_ssl_host = true
126132
@params_encoding = nil

modules/swagger-codegen/src/main/resources/ruby/gemspec.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ Gem::Specification.new do |s|
3434
s.add_development_dependency 'autotest', '~> 4.4', '>= 4.4.6'
3535
s.add_development_dependency 'autotest-rails-pure', '~> 4.1', '>= 4.1.2'
3636
s.add_development_dependency 'autotest-growl', '~> 0.2', '>= 0.2.16'
37-
s.add_development_dependency 'autotest-fsevent', '~> 0.2', '>= 0.2.11'
37+
s.add_development_dependency 'autotest-fsevent', '~> 0.2', '>= 0.2.12'
3838

3939
s.files = `find *`.split("\n").uniq.sort.select{|f| !f.empty? }
4040
s.test_files = `find spec/*`.split("\n")

samples/client/petstore/ruby/Gemfile.lock

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ PATH
66
typhoeus (~> 1.0, >= 1.0.1)
77

88
GEM
9-
remote: http://rubygems.org/
9+
remote: https://rubygems.org/
1010
specs:
1111
ZenTest (4.11.0)
1212
addressable (2.3.8)
1313
autotest (4.4.6)
1414
ZenTest (>= 4.4.1)
15-
autotest-fsevent (0.2.11)
15+
autotest-fsevent (0.2.12)
1616
sys-uname
1717
autotest-growl (0.2.16)
1818
autotest-rails-pure (4.1.2)
@@ -24,6 +24,7 @@ GEM
2424
ffi (1.9.8)
2525
hashdiff (0.3.0)
2626
json (1.8.3)
27+
rake (12.0.0)
2728
rspec (3.4.0)
2829
rspec-core (~> 3.4.0)
2930
rspec-expectations (~> 3.4.0)
@@ -38,7 +39,7 @@ GEM
3839
rspec-support (~> 3.4.0)
3940
rspec-support (3.4.1)
4041
safe_yaml (1.0.4)
41-
sys-uname (0.9.2)
42+
sys-uname (1.0.3)
4243
ffi (>= 1.0.0)
4344
typhoeus (1.0.1)
4445
ethon (>= 0.8.0)
@@ -53,10 +54,14 @@ PLATFORMS
5354

5455
DEPENDENCIES
5556
autotest (~> 4.4, >= 4.4.6)
56-
autotest-fsevent (~> 0.2, >= 0.2.11)
57+
autotest-fsevent (~> 0.2, >= 0.2.12)
5758
autotest-growl (~> 0.2, >= 0.2.16)
5859
autotest-rails-pure (~> 4.1, >= 4.1.2)
5960
petstore!
61+
rake (~> 12.0.0)
6062
rspec (~> 3.4, >= 3.4.0)
6163
vcr (~> 3.0, >= 3.0.1)
6264
webmock (~> 1.24, >= 1.24.3)
65+
66+
BUNDLED WITH
67+
1.12.3

samples/client/petstore/ruby/lib/petstore/api/fake_api.rb

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ def test_client_model_with_http_info(body, opts = {})
4040
@api_client.config.logger.debug "Calling API: FakeApi.test_client_model ..."
4141
end
4242
# verify the required parameter 'body' is set
43-
fail ArgumentError, "Missing the required parameter 'body' when calling FakeApi.test_client_model" if body.nil?
43+
if @api_client.config.client_side_validation && body.nil?
44+
fail ArgumentError, "Missing the required parameter 'body' when calling FakeApi.test_client_model"
45+
end
4446
# resource path
4547
local_var_path = "/fake"
4648

@@ -119,62 +121,70 @@ def test_endpoint_parameters_with_http_info(number, double, pattern_without_deli
119121
@api_client.config.logger.debug "Calling API: FakeApi.test_endpoint_parameters ..."
120122
end
121123
# verify the required parameter 'number' is set
122-
fail ArgumentError, "Missing the required parameter 'number' when calling FakeApi.test_endpoint_parameters" if number.nil?
123-
if number > 543.2
124+
if @api_client.config.client_side_validation && number.nil?
125+
fail ArgumentError, "Missing the required parameter 'number' when calling FakeApi.test_endpoint_parameters"
126+
end
127+
if @api_client.config.client_side_validation && number > 543.2
124128
fail ArgumentError, 'invalid value for "number" when calling FakeApi.test_endpoint_parameters, must be smaller than or equal to 543.2.'
125129
end
126130

127-
if number < 32.1
131+
if @api_client.config.client_side_validation && number < 32.1
128132
fail ArgumentError, 'invalid value for "number" when calling FakeApi.test_endpoint_parameters, must be greater than or equal to 32.1.'
129133
end
130134

131135
# verify the required parameter 'double' is set
132-
fail ArgumentError, "Missing the required parameter 'double' when calling FakeApi.test_endpoint_parameters" if double.nil?
133-
if double > 123.4
136+
if @api_client.config.client_side_validation && double.nil?
137+
fail ArgumentError, "Missing the required parameter 'double' when calling FakeApi.test_endpoint_parameters"
138+
end
139+
if @api_client.config.client_side_validation && double > 123.4
134140
fail ArgumentError, 'invalid value for "double" when calling FakeApi.test_endpoint_parameters, must be smaller than or equal to 123.4.'
135141
end
136142

137-
if double < 67.8
143+
if @api_client.config.client_side_validation && double < 67.8
138144
fail ArgumentError, 'invalid value for "double" when calling FakeApi.test_endpoint_parameters, must be greater than or equal to 67.8.'
139145
end
140146

141147
# verify the required parameter 'pattern_without_delimiter' is set
142-
fail ArgumentError, "Missing the required parameter 'pattern_without_delimiter' when calling FakeApi.test_endpoint_parameters" if pattern_without_delimiter.nil?
143-
if pattern_without_delimiter !~ Regexp.new(/^[A-Z].*/)
148+
if @api_client.config.client_side_validation && pattern_without_delimiter.nil?
149+
fail ArgumentError, "Missing the required parameter 'pattern_without_delimiter' when calling FakeApi.test_endpoint_parameters"
150+
end
151+
if @api_client.config.client_side_validation && pattern_without_delimiter !~ Regexp.new(/^[A-Z].*/)
144152
fail ArgumentError, "invalid value for 'pattern_without_delimiter' when calling FakeApi.test_endpoint_parameters, must conform to the pattern /^[A-Z].*/."
145153
end
146154

147155
# verify the required parameter 'byte' is set
148-
fail ArgumentError, "Missing the required parameter 'byte' when calling FakeApi.test_endpoint_parameters" if byte.nil?
149-
if !opts[:'integer'].nil? && opts[:'integer'] > 100
156+
if @api_client.config.client_side_validation && byte.nil?
157+
fail ArgumentError, "Missing the required parameter 'byte' when calling FakeApi.test_endpoint_parameters"
158+
end
159+
if @api_client.config.client_side_validation && !opts[:'integer'].nil? && opts[:'integer'] > 100
150160
fail ArgumentError, 'invalid value for "opts[:"integer"]" when calling FakeApi.test_endpoint_parameters, must be smaller than or equal to 100.'
151161
end
152162

153-
if !opts[:'integer'].nil? && opts[:'integer'] < 10
163+
if @api_client.config.client_side_validation && !opts[:'integer'].nil? && opts[:'integer'] < 10
154164
fail ArgumentError, 'invalid value for "opts[:"integer"]" when calling FakeApi.test_endpoint_parameters, must be greater than or equal to 10.'
155165
end
156166

157-
if !opts[:'int32'].nil? && opts[:'int32'] > 200
167+
if @api_client.config.client_side_validation && !opts[:'int32'].nil? && opts[:'int32'] > 200
158168
fail ArgumentError, 'invalid value for "opts[:"int32"]" when calling FakeApi.test_endpoint_parameters, must be smaller than or equal to 200.'
159169
end
160170

161-
if !opts[:'int32'].nil? && opts[:'int32'] < 20
171+
if @api_client.config.client_side_validation && !opts[:'int32'].nil? && opts[:'int32'] < 20
162172
fail ArgumentError, 'invalid value for "opts[:"int32"]" when calling FakeApi.test_endpoint_parameters, must be greater than or equal to 20.'
163173
end
164174

165-
if !opts[:'float'].nil? && opts[:'float'] > 987.6
175+
if @api_client.config.client_side_validation && !opts[:'float'].nil? && opts[:'float'] > 987.6
166176
fail ArgumentError, 'invalid value for "opts[:"float"]" when calling FakeApi.test_endpoint_parameters, must be smaller than or equal to 987.6.'
167177
end
168178

169-
if !opts[:'string'].nil? && opts[:'string'] !~ Regexp.new(/[a-z]/i)
179+
if @api_client.config.client_side_validation && !opts[:'string'].nil? && opts[:'string'] !~ Regexp.new(/[a-z]/i)
170180
fail ArgumentError, "invalid value for 'opts[:\"string\"]' when calling FakeApi.test_endpoint_parameters, must conform to the pattern /[a-z]/i."
171181
end
172182

173-
if !opts[:'password'].nil? && opts[:'password'].to_s.length > 64
183+
if @api_client.config.client_side_validation && !opts[:'password'].nil? && opts[:'password'].to_s.length > 64
174184
fail ArgumentError, 'invalid value for "opts[:"password"]" when calling FakeApi.test_endpoint_parameters, the character length must be smaller than or equal to 64.'
175185
end
176186

177-
if !opts[:'password'].nil? && opts[:'password'].to_s.length < 10
187+
if @api_client.config.client_side_validation && !opts[:'password'].nil? && opts[:'password'].to_s.length < 10
178188
fail ArgumentError, 'invalid value for "opts[:"password"]" when calling FakeApi.test_endpoint_parameters, the character length must be great than or equal to 10.'
179189
end
180190

@@ -256,28 +266,28 @@ def test_enum_parameters_with_http_info(opts = {})
256266
if @api_client.config.debugging
257267
@api_client.config.logger.debug "Calling API: FakeApi.test_enum_parameters ..."
258268
end
259-
if opts[:'enum_form_string_array'] && !opts[:'enum_form_string_array'].all?{|item| ['>', '$'].include?(item)}
269+
if @api_client.config.client_side_validation && opts[:'enum_form_string_array'] && !opts[:'enum_form_string_array'].all?{|item| ['>', '$'].include?(item)}
260270
fail ArgumentError, 'invalid value for "enum_form_string_array", must include one of >, $'
261271
end
262-
if opts[:'enum_form_string'] && !['_abc', '-efg', '(xyz)'].include?(opts[:'enum_form_string'])
272+
if @api_client.config.client_side_validation && opts[:'enum_form_string'] && !['_abc', '-efg', '(xyz)'].include?(opts[:'enum_form_string'])
263273
fail ArgumentError, 'invalid value for "enum_form_string", must be one of _abc, -efg, (xyz)'
264274
end
265-
if opts[:'enum_header_string_array'] && !opts[:'enum_header_string_array'].all?{|item| ['>', '$'].include?(item)}
275+
if @api_client.config.client_side_validation && opts[:'enum_header_string_array'] && !opts[:'enum_header_string_array'].all?{|item| ['>', '$'].include?(item)}
266276
fail ArgumentError, 'invalid value for "enum_header_string_array", must include one of >, $'
267277
end
268-
if opts[:'enum_header_string'] && !['_abc', '-efg', '(xyz)'].include?(opts[:'enum_header_string'])
278+
if @api_client.config.client_side_validation && opts[:'enum_header_string'] && !['_abc', '-efg', '(xyz)'].include?(opts[:'enum_header_string'])
269279
fail ArgumentError, 'invalid value for "enum_header_string", must be one of _abc, -efg, (xyz)'
270280
end
271-
if opts[:'enum_query_string_array'] && !opts[:'enum_query_string_array'].all?{|item| ['>', '$'].include?(item)}
281+
if @api_client.config.client_side_validation && opts[:'enum_query_string_array'] && !opts[:'enum_query_string_array'].all?{|item| ['>', '$'].include?(item)}
272282
fail ArgumentError, 'invalid value for "enum_query_string_array", must include one of >, $'
273283
end
274-
if opts[:'enum_query_string'] && !['_abc', '-efg', '(xyz)'].include?(opts[:'enum_query_string'])
284+
if @api_client.config.client_side_validation && opts[:'enum_query_string'] && !['_abc', '-efg', '(xyz)'].include?(opts[:'enum_query_string'])
275285
fail ArgumentError, 'invalid value for "enum_query_string", must be one of _abc, -efg, (xyz)'
276286
end
277-
if opts[:'enum_query_integer'] && !['1', '-2'].include?(opts[:'enum_query_integer'])
287+
if @api_client.config.client_side_validation && opts[:'enum_query_integer'] && !['1', '-2'].include?(opts[:'enum_query_integer'])
278288
fail ArgumentError, 'invalid value for "enum_query_integer", must be one of 1, -2'
279289
end
280-
if opts[:'enum_query_double'] && !['1.1', '-1.2'].include?(opts[:'enum_query_double'])
290+
if @api_client.config.client_side_validation && opts[:'enum_query_double'] && !['1.1', '-1.2'].include?(opts[:'enum_query_double'])
281291
fail ArgumentError, 'invalid value for "enum_query_double", must be one of 1.1, -1.2'
282292
end
283293
# resource path

0 commit comments

Comments
 (0)