Skip to content

[Ruby] add option to skip client side validation in Ruby client #5528

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 2, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions modules/swagger-codegen/src/main/resources/ruby/api.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,13 @@ module {{moduleName}}
{{#allParams}}
{{#required}}
# verify the required parameter '{{paramName}}' is set
fail ArgumentError, "Missing the required parameter '{{paramName}}' when calling {{classname}}.{{operationId}}" if {{{paramName}}}.nil?
if @api_client.config.client_side_validation && {{{paramName}}}.nil?
fail ArgumentError, "Missing the required parameter '{{paramName}}' when calling {{classname}}.{{operationId}}"
end
{{#isEnum}}
{{^isContainer}}
# verify enum value
unless [{{#allowableValues}}{{#values}}'{{{this}}}'{{^-last}}, {{/-last}}{{/values}}{{/allowableValues}}].include?({{{paramName}}})
if @api_client.config.client_side_validation && ![{{#allowableValues}}{{#values}}'{{{this}}}'{{^-last}}, {{/-last}}{{/values}}{{/allowableValues}}].include?({{{paramName}}})
fail ArgumentError, "invalid value for '{{{paramName}}}', must be one of {{#allowableValues}}{{#values}}{{{this}}}{{^-last}}, {{/-last}}{{/values}}{{/allowableValues}}"
end
{{/isContainer}}
Expand All @@ -51,56 +53,56 @@ module {{moduleName}}
{{^required}}
{{#isEnum}}
{{#collectionFormat}}
if opts[:'{{{paramName}}}'] && !opts[:'{{{paramName}}}'].all?{|item| [{{#allowableValues}}{{#values}}'{{{this}}}'{{^-last}}, {{/-last}}{{/values}}{{/allowableValues}}].include?(item)}
if @api_client.config.client_side_validation && opts[:'{{{paramName}}}'] && !opts[:'{{{paramName}}}'].all?{|item| [{{#allowableValues}}{{#values}}'{{{this}}}'{{^-last}}, {{/-last}}{{/values}}{{/allowableValues}}].include?(item)}
fail ArgumentError, 'invalid value for "{{{paramName}}}", must include one of {{#allowableValues}}{{#values}}{{{this}}}{{^-last}}, {{/-last}}{{/values}}{{/allowableValues}}'
end
{{/collectionFormat}}
{{^collectionFormat}}
if opts[:'{{{paramName}}}'] && ![{{#allowableValues}}{{#values}}'{{{this}}}'{{^-last}}, {{/-last}}{{/values}}{{/allowableValues}}].include?(opts[:'{{{paramName}}}'])
if @api_client.config.client_side_validation && opts[:'{{{paramName}}}'] && ![{{#allowableValues}}{{#values}}'{{{this}}}'{{^-last}}, {{/-last}}{{/values}}{{/allowableValues}}].include?(opts[:'{{{paramName}}}'])
fail ArgumentError, 'invalid value for "{{{paramName}}}", must be one of {{#allowableValues}}{{#values}}{{{this}}}{{^-last}}, {{/-last}}{{/values}}{{/allowableValues}}'
end
{{/collectionFormat}}
{{/isEnum}}
{{/required}}
{{#hasValidation}}
{{#maxLength}}
if {{^required}}!opts[:'{{{paramName}}}'].nil? && {{/required}}{{#required}}{{{paramName}}}{{/required}}{{^required}}opts[:'{{{paramName}}}']{{/required}}.to_s.length > {{{maxLength}}}
if @api_client.config.client_side_validation && {{^required}}!opts[:'{{{paramName}}}'].nil? && {{/required}}{{#required}}{{{paramName}}}{{/required}}{{^required}}opts[:'{{{paramName}}}']{{/required}}.to_s.length > {{{maxLength}}}
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}}}.'
end

{{/maxLength}}
{{#minLength}}
if {{^required}}!opts[:'{{{paramName}}}'].nil? && {{/required}}{{#required}}{{{paramName}}}{{/required}}{{^required}}opts[:'{{{paramName}}}']{{/required}}.to_s.length < {{{minLength}}}
if @api_client.config.client_side_validation && {{^required}}!opts[:'{{{paramName}}}'].nil? && {{/required}}{{#required}}{{{paramName}}}{{/required}}{{^required}}opts[:'{{{paramName}}}']{{/required}}.to_s.length < {{{minLength}}}
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}}}.'
end

{{/minLength}}
{{#maximum}}
if {{^required}}!opts[:'{{{paramName}}}'].nil? && {{/required}}{{#required}}{{{paramName}}}{{/required}}{{^required}}opts[:'{{{paramName}}}']{{/required}} >{{#exclusiveMaximum}}={{/exclusiveMaximum}} {{{maximum}}}
if @api_client.config.client_side_validation && {{^required}}!opts[:'{{{paramName}}}'].nil? && {{/required}}{{#required}}{{{paramName}}}{{/required}}{{^required}}opts[:'{{{paramName}}}']{{/required}} >{{#exclusiveMaximum}}={{/exclusiveMaximum}} {{{maximum}}}
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}}}.'
end

{{/maximum}}
{{#minimum}}
if {{^required}}!opts[:'{{{paramName}}}'].nil? && {{/required}}{{#required}}{{{paramName}}}{{/required}}{{^required}}opts[:'{{{paramName}}}']{{/required}} <{{#exclusiveMinimum}}={{/exclusiveMinimum}} {{{minimum}}}
if @api_client.config.client_side_validation && {{^required}}!opts[:'{{{paramName}}}'].nil? && {{/required}}{{#required}}{{{paramName}}}{{/required}}{{^required}}opts[:'{{{paramName}}}']{{/required}} <{{#exclusiveMinimum}}={{/exclusiveMinimum}} {{{minimum}}}
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}}}.'
end

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

{{/pattern}}
{{#maxItems}}
if {{^required}}!opts[:'{{{paramName}}}'].nil? && {{/required}}{{#required}}{{{paramName}}}{{/required}}{{^required}}opts[:'{{{paramName}}}']{{/required}}.length > {{{maxItems}}}
if @api_client.config.client_side_validation && {{^required}}!opts[:'{{{paramName}}}'].nil? && {{/required}}{{#required}}{{{paramName}}}{{/required}}{{^required}}opts[:'{{{paramName}}}']{{/required}}.length > {{{maxItems}}}
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}}}.'
end

{{/maxItems}}
{{#minItems}}
if {{^required}}!opts[:'{{{paramName}}}'].nil? && {{/required}}{{#required}}{{{paramName}}}{{/required}}{{^required}}opts[:'{{{paramName}}}']{{/required}}.length < {{{minItems}}}
if @api_client.config.client_side_validation && {{^required}}!opts[:'{{{paramName}}}'].nil? && {{/required}}{{#required}}{{{paramName}}}{{/required}}{{^required}}opts[:'{{{paramName}}}']{{/required}}.length < {{{minItems}}}
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}}}.'
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ module {{moduleName}}
# Default to 0 (never times out).
attr_accessor :timeout

# Set this to false to skip client side validation in the operation.
# Default to true.
# @return [true, false]
attr_accessor :client_side_validation

### TLS/SSL setting
# Set this to false to skip verifying SSL certificate when calling API from https server.
# Default to true.
Expand Down Expand Up @@ -121,6 +126,7 @@ module {{moduleName}}
@api_key = {}
@api_key_prefix = {}
@timeout = 0
@client_side_validation = true
@verify_ssl = true
@verify_ssl_host = true
@params_encoding = nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Gem::Specification.new do |s|
s.add_development_dependency 'autotest', '~> 4.4', '>= 4.4.6'
s.add_development_dependency 'autotest-rails-pure', '~> 4.1', '>= 4.1.2'
s.add_development_dependency 'autotest-growl', '~> 0.2', '>= 0.2.16'
s.add_development_dependency 'autotest-fsevent', '~> 0.2', '>= 0.2.11'
s.add_development_dependency 'autotest-fsevent', '~> 0.2', '>= 0.2.12'

s.files = `find *`.split("\n").uniq.sort.select{|f| !f.empty? }
s.test_files = `find spec/*`.split("\n")
Expand Down
13 changes: 9 additions & 4 deletions samples/client/petstore/ruby/Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ PATH
typhoeus (~> 1.0, >= 1.0.1)

GEM
remote: http://rubygems.org/
remote: https://rubygems.org/
specs:
ZenTest (4.11.0)
addressable (2.3.8)
autotest (4.4.6)
ZenTest (>= 4.4.1)
autotest-fsevent (0.2.11)
autotest-fsevent (0.2.12)
sys-uname
autotest-growl (0.2.16)
autotest-rails-pure (4.1.2)
Expand All @@ -24,6 +24,7 @@ GEM
ffi (1.9.8)
hashdiff (0.3.0)
json (1.8.3)
rake (12.0.0)
rspec (3.4.0)
rspec-core (~> 3.4.0)
rspec-expectations (~> 3.4.0)
Expand All @@ -38,7 +39,7 @@ GEM
rspec-support (~> 3.4.0)
rspec-support (3.4.1)
safe_yaml (1.0.4)
sys-uname (0.9.2)
sys-uname (1.0.3)
ffi (>= 1.0.0)
typhoeus (1.0.1)
ethon (>= 0.8.0)
Expand All @@ -53,10 +54,14 @@ PLATFORMS

DEPENDENCIES
autotest (~> 4.4, >= 4.4.6)
autotest-fsevent (~> 0.2, >= 0.2.11)
autotest-fsevent (~> 0.2, >= 0.2.12)
autotest-growl (~> 0.2, >= 0.2.16)
autotest-rails-pure (~> 4.1, >= 4.1.2)
petstore!
rake (~> 12.0.0)
rspec (~> 3.4, >= 3.4.0)
vcr (~> 3.0, >= 3.0.1)
webmock (~> 1.24, >= 1.24.3)

BUNDLED WITH
1.12.3
62 changes: 36 additions & 26 deletions samples/client/petstore/ruby/lib/petstore/api/fake_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ def test_client_model_with_http_info(body, opts = {})
@api_client.config.logger.debug "Calling API: FakeApi.test_client_model ..."
end
# verify the required parameter 'body' is set
fail ArgumentError, "Missing the required parameter 'body' when calling FakeApi.test_client_model" if body.nil?
if @api_client.config.client_side_validation && body.nil?
fail ArgumentError, "Missing the required parameter 'body' when calling FakeApi.test_client_model"
end
# resource path
local_var_path = "/fake"

Expand Down Expand Up @@ -119,62 +121,70 @@ def test_endpoint_parameters_with_http_info(number, double, pattern_without_deli
@api_client.config.logger.debug "Calling API: FakeApi.test_endpoint_parameters ..."
end
# verify the required parameter 'number' is set
fail ArgumentError, "Missing the required parameter 'number' when calling FakeApi.test_endpoint_parameters" if number.nil?
if number > 543.2
if @api_client.config.client_side_validation && number.nil?
fail ArgumentError, "Missing the required parameter 'number' when calling FakeApi.test_endpoint_parameters"
end
if @api_client.config.client_side_validation && number > 543.2
fail ArgumentError, 'invalid value for "number" when calling FakeApi.test_endpoint_parameters, must be smaller than or equal to 543.2.'
end

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

# verify the required parameter 'double' is set
fail ArgumentError, "Missing the required parameter 'double' when calling FakeApi.test_endpoint_parameters" if double.nil?
if double > 123.4
if @api_client.config.client_side_validation && double.nil?
fail ArgumentError, "Missing the required parameter 'double' when calling FakeApi.test_endpoint_parameters"
end
if @api_client.config.client_side_validation && double > 123.4
fail ArgumentError, 'invalid value for "double" when calling FakeApi.test_endpoint_parameters, must be smaller than or equal to 123.4.'
end

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

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

# verify the required parameter 'byte' is set
fail ArgumentError, "Missing the required parameter 'byte' when calling FakeApi.test_endpoint_parameters" if byte.nil?
if !opts[:'integer'].nil? && opts[:'integer'] > 100
if @api_client.config.client_side_validation && byte.nil?
fail ArgumentError, "Missing the required parameter 'byte' when calling FakeApi.test_endpoint_parameters"
end
if @api_client.config.client_side_validation && !opts[:'integer'].nil? && opts[:'integer'] > 100
fail ArgumentError, 'invalid value for "opts[:"integer"]" when calling FakeApi.test_endpoint_parameters, must be smaller than or equal to 100.'
end

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

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

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

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

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

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

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

Expand Down Expand Up @@ -256,28 +266,28 @@ def test_enum_parameters_with_http_info(opts = {})
if @api_client.config.debugging
@api_client.config.logger.debug "Calling API: FakeApi.test_enum_parameters ..."
end
if opts[:'enum_form_string_array'] && !opts[:'enum_form_string_array'].all?{|item| ['>', '$'].include?(item)}
if @api_client.config.client_side_validation && opts[:'enum_form_string_array'] && !opts[:'enum_form_string_array'].all?{|item| ['>', '$'].include?(item)}
fail ArgumentError, 'invalid value for "enum_form_string_array", must include one of >, $'
end
if opts[:'enum_form_string'] && !['_abc', '-efg', '(xyz)'].include?(opts[:'enum_form_string'])
if @api_client.config.client_side_validation && opts[:'enum_form_string'] && !['_abc', '-efg', '(xyz)'].include?(opts[:'enum_form_string'])
fail ArgumentError, 'invalid value for "enum_form_string", must be one of _abc, -efg, (xyz)'
end
if opts[:'enum_header_string_array'] && !opts[:'enum_header_string_array'].all?{|item| ['>', '$'].include?(item)}
if @api_client.config.client_side_validation && opts[:'enum_header_string_array'] && !opts[:'enum_header_string_array'].all?{|item| ['>', '$'].include?(item)}
fail ArgumentError, 'invalid value for "enum_header_string_array", must include one of >, $'
end
if opts[:'enum_header_string'] && !['_abc', '-efg', '(xyz)'].include?(opts[:'enum_header_string'])
if @api_client.config.client_side_validation && opts[:'enum_header_string'] && !['_abc', '-efg', '(xyz)'].include?(opts[:'enum_header_string'])
fail ArgumentError, 'invalid value for "enum_header_string", must be one of _abc, -efg, (xyz)'
end
if opts[:'enum_query_string_array'] && !opts[:'enum_query_string_array'].all?{|item| ['>', '$'].include?(item)}
if @api_client.config.client_side_validation && opts[:'enum_query_string_array'] && !opts[:'enum_query_string_array'].all?{|item| ['>', '$'].include?(item)}
fail ArgumentError, 'invalid value for "enum_query_string_array", must include one of >, $'
end
if opts[:'enum_query_string'] && !['_abc', '-efg', '(xyz)'].include?(opts[:'enum_query_string'])
if @api_client.config.client_side_validation && opts[:'enum_query_string'] && !['_abc', '-efg', '(xyz)'].include?(opts[:'enum_query_string'])
fail ArgumentError, 'invalid value for "enum_query_string", must be one of _abc, -efg, (xyz)'
end
if opts[:'enum_query_integer'] && !['1', '-2'].include?(opts[:'enum_query_integer'])
if @api_client.config.client_side_validation && opts[:'enum_query_integer'] && !['1', '-2'].include?(opts[:'enum_query_integer'])
fail ArgumentError, 'invalid value for "enum_query_integer", must be one of 1, -2'
end
if opts[:'enum_query_double'] && !['1.1', '-1.2'].include?(opts[:'enum_query_double'])
if @api_client.config.client_side_validation && opts[:'enum_query_double'] && !['1.1', '-1.2'].include?(opts[:'enum_query_double'])
fail ArgumentError, 'invalid value for "enum_query_double", must be one of 1.1, -1.2'
end
# resource path
Expand Down
Loading