From 8c357dd1d61656c048011571a274b2dcc62decde Mon Sep 17 00:00:00 2001 From: wing328 Date: Mon, 1 Aug 2016 16:50:11 +0800 Subject: [PATCH] fix validation for optional parameters/properties for ruby --- .../codegen/languages/RubyClientCodegen.java | 1 - .../src/main/resources/ruby/api.mustache | 10 +- .../ruby/partial_model_generic.mustache | 38 +++--- samples/client/petstore/ruby/README.md | 2 +- .../ruby/lib/petstore/api/fake_api.rb | 14 +-- .../ruby/lib/petstore/models/format_test.rb | 116 +++++++++++++----- 6 files changed, 118 insertions(+), 63 deletions(-) diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/RubyClientCodegen.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/RubyClientCodegen.java index 66f7b2f09b7b..a2fc199d586f 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/RubyClientCodegen.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/RubyClientCodegen.java @@ -230,7 +230,6 @@ public class RubyClientCodegen extends DefaultCodegen implements CodegenConfig { supportingFiles.add(new SupportingFile("README.mustache", "", "README.md")); supportingFiles.add(new SupportingFile("git_push.sh.mustache", "", "git_push.sh")); supportingFiles.add(new SupportingFile("gitignore.mustache", "", ".gitignore")); - supportingFiles.add(new SupportingFile("LICENSE", "", "LICENSE")); // test files should not be overwritten writeOptional(outputFolder, new SupportingFile("rspec.mustache", "", ".rspec")); diff --git a/modules/swagger-codegen/src/main/resources/ruby/api.mustache b/modules/swagger-codegen/src/main/resources/ruby/api.mustache index 53e455649cdd..ca3e2c153cb7 100644 --- a/modules/swagger-codegen/src/main/resources/ruby/api.mustache +++ b/modules/swagger-codegen/src/main/resources/ruby/api.mustache @@ -57,31 +57,31 @@ module {{moduleName}} {{/required}} {{#hasValidation}} {{#maxLength}} - if {{#required}}{{{paramName}}}{{/required}}{{^required}}opts[:'{{{paramName}}}']{{/required}}.to_s.length > {{{maxLength}}} + if {{^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}}{{{paramName}}}{{/required}}{{^required}}opts[:'{{{paramName}}}']{{/required}}.to_s.length < {{{minLength}}} + if {{^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}}{{{paramName}}}{{/required}}{{^required}}opts[:'{{{paramName}}}']{{/required}} > {{{maximum}}} + if {{^required}}!opts[:'{{{paramName}}}'].nil? && {{/required}}{{#required}}{{{paramName}}}{{/required}}{{^required}}opts[:'{{{paramName}}}']{{/required}} > {{{maximum}}} fail ArgumentError, 'invalid value for "{{#required}}{{{paramName}}}{{/required}}{{^required}}opts[:"{{{paramName}}}"]{{/required}}" when calling {{classname}}.{{operationId}}, must be smaller than or equal to {{{maximum}}}.' end {{/maximum}} {{#minimum}} - if {{#required}}{{{paramName}}}{{/required}}{{^required}}opts[:'{{{paramName}}}']{{/required}} < {{{minimum}}} + if {{^required}}!opts[:'{{{paramName}}}'].nil? && {{/required}}{{#required}}{{{paramName}}}{{/required}}{{^required}}opts[:'{{{paramName}}}']{{/required}} < {{{minimum}}} fail ArgumentError, 'invalid value for "{{#required}}{{{paramName}}}{{/required}}{{^required}}opts[:"{{{paramName}}}"]{{/required}}" when calling {{classname}}.{{operationId}}, must be greater than or equal to {{{minimum}}}.' end {{/minimum}} {{#pattern}} - if {{#required}}{{{paramName}}}{{/required}}{{^required}}opts[:'{{{paramName}}}']{{/required}} !~ Regexp.new({{{pattern}}}) + if {{^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 diff --git a/modules/swagger-codegen/src/main/resources/ruby/partial_model_generic.mustache b/modules/swagger-codegen/src/main/resources/ruby/partial_model_generic.mustache index e0eb84f8886d..6416ada91f18 100644 --- a/modules/swagger-codegen/src/main/resources/ruby/partial_model_generic.mustache +++ b/modules/swagger-codegen/src/main/resources/ruby/partial_model_generic.mustache @@ -77,42 +77,46 @@ # @return Array for valid properies with the reasons def list_invalid_properties invalid_properties = Array.new + {{#vars}} {{#hasValidation}} + {{#required}} if @{{{name}}}.nil? - fail ArgumentError, "{{{name}}} cannot be nil" + invalid_properties.push("invalid value for '{{{name}}}', {{{name}}} cannot be nil.") end + {{/required}} {{#maxLength}} - if @{{{name}}}.to_s.length > {{{maxLength}}} + if {{^required}}!@{{{name}}}.nil? && {{/required}}@{{{name}}}.to_s.length > {{{maxLength}}} invalid_properties.push("invalid value for '{{{name}}}', the character length must be smaller than or equal to {{{maxLength}}}.") end {{/maxLength}} {{#minLength}} - if @{{{name}}}.to_s.length < {{{minLength}}} + if {{^required}}!@{{{name}}}.nil? && {{/required}}@{{{name}}}.to_s.length < {{{minLength}}} invalid_properties.push("invalid value for '{{{name}}}', the character length must be great than or equal to {{{minLength}}}.") end {{/minLength}} {{#maximum}} - if @{{{name}}} > {{{maximum}}} + if {{^required}}!@{{{name}}}.nil? && {{/required}}@{{{name}}} > {{{maximum}}} invalid_properties.push("invalid value for '{{{name}}}', must be smaller than or equal to {{{maximum}}}.") end {{/maximum}} {{#minimum}} - if @{{{name}}} < {{{minimum}}} + if {{^required}}!@{{{name}}}.nil? && {{/required}}@{{{name}}} < {{{minimum}}} invalid_properties.push("invalid value for '{{{name}}}', must be greater than or equal to {{{minimum}}}.") end {{/minimum}} {{#pattern}} - if @{{{name}}} !~ Regexp.new({{{pattern}}}) + if {{^required}}!@{{{name}}}.nil? && {{/required}}@{{{name}}} !~ Regexp.new({{{pattern}}}) invalid_properties.push("invalid value for '{{{name}}}', must conform to the pattern {{{pattern}}}.") end {{/pattern}} {{/hasValidation}} + {{/vars}} return invalid_properties end @@ -131,19 +135,19 @@ {{/isEnum}} {{#hasValidation}} {{#maxLength}} - return false if @{{{name}}}.to_s.length > {{{maxLength}}} + return false if {{^required}}!@{{{name}}}.nil? && {{/required}}@{{{name}}}.to_s.length > {{{maxLength}}} {{/maxLength}} {{#minLength}} - return false if @{{{name}}}.to_s.length < {{{minLength}}} + return false if {{^required}}!@{{{name}}}.nil? && {{/required}}@{{{name}}}.to_s.length < {{{minLength}}} {{/minLength}} {{#maximum}} - return false if @{{{name}}} > {{{maximum}}} + return false if {{^required}}!@{{{name}}}.nil? && {{/required}}@{{{name}}} > {{{maximum}}} {{/maximum}} {{#minimum}} - return false if @{{{name}}} < {{{minimum}}} + return false if {{^required}}!@{{{name}}}.nil? && {{/required}}@{{{name}}} < {{{minimum}}} {{/minimum}} {{#pattern}} - return false if @{{{name}}} !~ Regexp.new({{{pattern}}}) + return false if {{^required}}!@{{{name}}}.nil? && {{/required}}@{{{name}}} !~ Regexp.new({{{pattern}}}) {{/pattern}} {{/hasValidation}} {{/vars}} @@ -170,36 +174,38 @@ # Custom attribute writer method with validation # @param [Object] {{{name}}} Value to be assigned def {{{name}}}=({{{name}}}) + {{#required}} if {{{name}}}.nil? fail ArgumentError, "{{{name}}} cannot be nil" end + {{/required}} {{#maxLength}} - if {{{name}}}.to_s.length > {{{maxLength}}} + if {{^required}}!{{{name}}}.nil? && {{/required}}{{{name}}}.to_s.length > {{{maxLength}}} fail ArgumentError, "invalid value for '{{{name}}}', the character length must be smaller than or equal to {{{maxLength}}}." end {{/maxLength}} {{#minLength}} - if {{{name}}}.to_s.length < {{{minLength}}} + if {{^required}}!{{{name}}}.nil? && {{/required}}{{{name}}}.to_s.length < {{{minLength}}} fail ArgumentError, "invalid value for '{{{name}}}', the character length must be great than or equal to {{{minLength}}}." end {{/minLength}} {{#maximum}} - if {{{name}}} > {{{maximum}}} + if {{^required}}!{{{name}}}.nil? && {{/required}}{{{name}}} > {{{maximum}}} fail ArgumentError, "invalid value for '{{{name}}}', must be smaller than or equal to {{{maximum}}}." end {{/maximum}} {{#minimum}} - if {{{name}}} < {{{minimum}}} + if {{^required}}!{{{name}}}.nil? && {{/required}}{{{name}}} < {{{minimum}}} fail ArgumentError, "invalid value for '{{{name}}}', must be greater than or equal to {{{minimum}}}." end {{/minimum}} {{#pattern}} - if @{{{name}}} !~ Regexp.new({{{pattern}}}) + if {{^required}}!{{{name}}}.nil? && {{/required}}{{{name}}} !~ Regexp.new({{{pattern}}}) fail ArgumentError, "invalid value for '{{{name}}}', must conform to the pattern {{{pattern}}}." end diff --git a/samples/client/petstore/ruby/README.md b/samples/client/petstore/ruby/README.md index 2c850599fd5d..cb116bec38bd 100644 --- a/samples/client/petstore/ruby/README.md +++ b/samples/client/petstore/ruby/README.md @@ -8,7 +8,7 @@ This SDK is automatically generated by the [Swagger Codegen](https://github.com/ - API version: 1.0.0 - Package version: 1.0.0 -- Build date: 2016-07-26T14:38:12.507+08:00 +- Build date: 2016-08-01T16:37:23.828+08:00 - Build package: class io.swagger.codegen.languages.RubyClientCodegen ## Installation diff --git a/samples/client/petstore/ruby/lib/petstore/api/fake_api.rb b/samples/client/petstore/ruby/lib/petstore/api/fake_api.rb index 3cf57c3be882..95745e8f8b32 100644 --- a/samples/client/petstore/ruby/lib/petstore/api/fake_api.rb +++ b/samples/client/petstore/ruby/lib/petstore/api/fake_api.rb @@ -157,31 +157,31 @@ module Petstore # 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'] > 100.0 + if !opts[:'integer'].nil? && opts[:'integer'] > 100.0 fail ArgumentError, 'invalid value for "opts[:"integer"]" when calling FakeApi.test_endpoint_parameters, must be smaller than or equal to 100.0.' end - if opts[:'integer'] < 10.0 + if !opts[:'integer'].nil? && opts[:'integer'] < 10.0 fail ArgumentError, 'invalid value for "opts[:"integer"]" when calling FakeApi.test_endpoint_parameters, must be greater than or equal to 10.0.' end - if opts[:'int32'] > 200.0 + if !opts[:'int32'].nil? && opts[:'int32'] > 200.0 fail ArgumentError, 'invalid value for "opts[:"int32"]" when calling FakeApi.test_endpoint_parameters, must be smaller than or equal to 200.0.' end - if opts[:'int32'] < 20.0 + if !opts[:'int32'].nil? && opts[:'int32'] < 20.0 fail ArgumentError, 'invalid value for "opts[:"int32"]" when calling FakeApi.test_endpoint_parameters, must be greater than or equal to 20.0.' end - if opts[:'float'] > 987.6 + if !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[:'password'].to_s.length > 64 + if !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'].to_s.length < 10 + if !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 diff --git a/samples/client/petstore/ruby/lib/petstore/models/format_test.rb b/samples/client/petstore/ruby/lib/petstore/models/format_test.rb index 876da7907472..2159b1ea6562 100644 --- a/samples/client/petstore/ruby/lib/petstore/models/format_test.rb +++ b/samples/client/petstore/ruby/lib/petstore/models/format_test.rb @@ -157,24 +157,89 @@ module Petstore # @return Array for valid properies with the reasons def list_invalid_properties invalid_properties = Array.new + + if !@integer.nil? && @integer > 100.0 + invalid_properties.push("invalid value for 'integer', must be smaller than or equal to 100.0.") + end + + if !@integer.nil? && @integer < 10.0 + invalid_properties.push("invalid value for 'integer', must be greater than or equal to 10.0.") + end + + + if !@int32.nil? && @int32 > 200.0 + invalid_properties.push("invalid value for 'int32', must be smaller than or equal to 200.0.") + end + + if !@int32.nil? && @int32 < 20.0 + invalid_properties.push("invalid value for 'int32', must be greater than or equal to 20.0.") + end + + if @number.nil? + invalid_properties.push("invalid value for 'number', number cannot be nil.") + end + + if @number > 543.2 + invalid_properties.push("invalid value for 'number', must be smaller than or equal to 543.2.") + end + + if @number < 32.1 + invalid_properties.push("invalid value for 'number', must be greater than or equal to 32.1.") + end + + + if !@float.nil? && @float > 987.6 + invalid_properties.push("invalid value for 'float', must be smaller than or equal to 987.6.") + end + + if !@float.nil? && @float < 54.3 + invalid_properties.push("invalid value for 'float', must be greater than or equal to 54.3.") + end + + + if !@double.nil? && @double > 123.4 + invalid_properties.push("invalid value for 'double', must be smaller than or equal to 123.4.") + end + + if !@double.nil? && @double < 67.8 + invalid_properties.push("invalid value for 'double', must be greater than or equal to 67.8.") + end + + + if !@string.nil? && @string !~ Regexp.new(/[a-z]/i) + invalid_properties.push("invalid value for 'string', must conform to the pattern /[a-z]/i.") + end + + if @password.nil? + invalid_properties.push("invalid value for 'password', password cannot be nil.") + end + + if @password.to_s.length > 64 + invalid_properties.push("invalid value for 'password', the character length must be smaller than or equal to 64.") + end + + if @password.to_s.length < 10 + invalid_properties.push("invalid value for 'password', the character length must be great than or equal to 10.") + end + return invalid_properties end # Check to see if the all the properties in the model are valid # @return true if the model is valid def valid? - return false if @integer > 100.0 - return false if @integer < 10.0 - return false if @int32 > 200.0 - return false if @int32 < 20.0 + return false if !@integer.nil? && @integer > 100.0 + return false if !@integer.nil? && @integer < 10.0 + return false if !@int32.nil? && @int32 > 200.0 + return false if !@int32.nil? && @int32 < 20.0 return false if @number.nil? return false if @number > 543.2 return false if @number < 32.1 - return false if @float > 987.6 - return false if @float < 54.3 - return false if @double > 123.4 - return false if @double < 67.8 - return false if @string !~ Regexp.new(/[a-z]/i) + return false if !@float.nil? && @float > 987.6 + return false if !@float.nil? && @float < 54.3 + return false if !@double.nil? && @double > 123.4 + return false if !@double.nil? && @double < 67.8 + return false if !@string.nil? && @string !~ Regexp.new(/[a-z]/i) return false if @byte.nil? return false if @date.nil? return false if @password.nil? @@ -186,15 +251,12 @@ module Petstore # Custom attribute writer method with validation # @param [Object] integer Value to be assigned def integer=(integer) - if integer.nil? - fail ArgumentError, "integer cannot be nil" - end - if integer > 100.0 + if !integer.nil? && integer > 100.0 fail ArgumentError, "invalid value for 'integer', must be smaller than or equal to 100.0." end - if integer < 10.0 + if !integer.nil? && integer < 10.0 fail ArgumentError, "invalid value for 'integer', must be greater than or equal to 10.0." end @@ -204,15 +266,12 @@ module Petstore # Custom attribute writer method with validation # @param [Object] int32 Value to be assigned def int32=(int32) - if int32.nil? - fail ArgumentError, "int32 cannot be nil" - end - if int32 > 200.0 + if !int32.nil? && int32 > 200.0 fail ArgumentError, "invalid value for 'int32', must be smaller than or equal to 200.0." end - if int32 < 20.0 + if !int32.nil? && int32 < 20.0 fail ArgumentError, "invalid value for 'int32', must be greater than or equal to 20.0." end @@ -240,15 +299,12 @@ module Petstore # Custom attribute writer method with validation # @param [Object] float Value to be assigned def float=(float) - if float.nil? - fail ArgumentError, "float cannot be nil" - end - if float > 987.6 + if !float.nil? && float > 987.6 fail ArgumentError, "invalid value for 'float', must be smaller than or equal to 987.6." end - if float < 54.3 + if !float.nil? && float < 54.3 fail ArgumentError, "invalid value for 'float', must be greater than or equal to 54.3." end @@ -258,15 +314,12 @@ module Petstore # Custom attribute writer method with validation # @param [Object] double Value to be assigned def double=(double) - if double.nil? - fail ArgumentError, "double cannot be nil" - end - if double > 123.4 + if !double.nil? && double > 123.4 fail ArgumentError, "invalid value for 'double', must be smaller than or equal to 123.4." end - if double < 67.8 + if !double.nil? && double < 67.8 fail ArgumentError, "invalid value for 'double', must be greater than or equal to 67.8." end @@ -276,11 +329,8 @@ module Petstore # Custom attribute writer method with validation # @param [Object] string Value to be assigned def string=(string) - if string.nil? - fail ArgumentError, "string cannot be nil" - end - if @string !~ Regexp.new(/[a-z]/i) + if !string.nil? && string !~ Regexp.new(/[a-z]/i) fail ArgumentError, "invalid value for 'string', must conform to the pattern /[a-z]/i." end