diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractRubyCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractRubyCodegen.java index 754d825016b8..7b32b989bd74 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractRubyCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractRubyCodegen.java @@ -147,20 +147,7 @@ public String toVarName(String name) { } public String toRegularExpression(String pattern) { - if (StringUtils.isEmpty(pattern)) { - return pattern; - } - - // We don't escape \ in string since Ruby doesn't like \ escaped in regex literal - String regexString = pattern; - if (!regexString.startsWith("/")) { - regexString = "/" + regexString; - } - if (StringUtils.countMatches(regexString, '/') == 1) { - // we only have forward slash inserted at start... adding one to end - regexString = regexString + "/"; - } - return regexString; + return addRegularExpressionDelimiter(pattern); } @Override diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/ruby/RubyClientCodegenTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/ruby/RubyClientCodegenTest.java index 9bb5e0f0464b..5d025c098f0c 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/ruby/RubyClientCodegenTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/ruby/RubyClientCodegenTest.java @@ -319,13 +319,7 @@ public void exampleRegexParameterValidationOAS3Test() { Assert.assertEquals(op.allParams.get(0).pattern, "/^pattern$/"); // pattern_two_slashes '/^pattern$/i' Assert.assertEquals(op.allParams.get(1).pattern, "/^pattern$/i"); - // pattern_one_slash_start '/^pattern$' - Assert.assertEquals(op.allParams.get(2).pattern, "/^pattern$/"); - // pattern_one_slash_end '^pattern$/' - Assert.assertEquals(op.allParams.get(3).pattern, "/^pattern$/"); - // pattern_one_slash_near_end '^pattern$/im' - Assert.assertEquals(op.allParams.get(4).pattern, "/^pattern$/im"); // pattern_dont_escape_backslash '/^pattern\d{3}$/i' NOTE: the double \ is to escape \ in string but is read as single \ - Assert.assertEquals(op.allParams.get(5).pattern, "/^pattern\\d{3}$/i"); + Assert.assertEquals(op.allParams.get(2).pattern, "/^pattern\\d{3}$/i"); } } diff --git a/modules/openapi-generator/src/test/resources/3_0/test_regex.yaml b/modules/openapi-generator/src/test/resources/3_0/test_regex.yaml index af1ae8d3101c..f0e8decdbb4d 100644 --- a/modules/openapi-generator/src/test/resources/3_0/test_regex.yaml +++ b/modules/openapi-generator/src/test/resources/3_0/test_regex.yaml @@ -25,21 +25,6 @@ paths: schema: type: string pattern: '/^pattern$/i' - - name: pattern_one_slash_start - in: header - schema: - type: string - pattern: '/^pattern$' - - name: pattern_one_slash_end - in: header - schema: - type: string - pattern: '^pattern$/' - - name: pattern_one_slash_near_end - in: header - schema: - type: string - pattern: '^pattern$/im' - name: pattern_dont_escape_backslash in: header schema: 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 2ac0fe6b4b14..f1beb6d2bb7e 100644 --- a/samples/client/petstore/ruby/lib/petstore/models/format_test.rb +++ b/samples/client/petstore/ruby/lib/petstore/models/format_test.rb @@ -195,8 +195,8 @@ def list_invalid_properties invalid_properties.push('invalid value for "byte", byte cannot be nil.') end - if @byte !~ Regexp.new(/^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$) - invalid_properties.push('invalid value for "byte", must conform to the pattern /^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$.') + if @byte !~ Regexp.new(/^(?:[A-Za-z0-9+\/]{4})*(?:[A-Za-z0-9+\/]{2}==|[A-Za-z0-9+\/]{3}=)?$/) + invalid_properties.push('invalid value for "byte", must conform to the pattern /^(?:[A-Za-z0-9+\/]{4})*(?:[A-Za-z0-9+\/]{2}==|[A-Za-z0-9+\/]{3}=)?$/.') end if @date.nil? @@ -234,7 +234,7 @@ def valid? 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 @byte !~ Regexp.new(/^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$) + return false if @byte !~ Regexp.new(/^(?:[A-Za-z0-9+\/]{4})*(?:[A-Za-z0-9+\/]{2}==|[A-Za-z0-9+\/]{3}=)?$/) return false if @date.nil? return false if @password.nil? return false if @password.to_s.length > 64 @@ -333,8 +333,8 @@ def byte=(byte) fail ArgumentError, 'byte cannot be nil' end - if byte !~ Regexp.new(/^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$) - fail ArgumentError, 'invalid value for "byte", must conform to the pattern /^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$.' + if byte !~ Regexp.new(/^(?:[A-Za-z0-9+\/]{4})*(?:[A-Za-z0-9+\/]{2}==|[A-Za-z0-9+\/]{3}=)?$/) + fail ArgumentError, 'invalid value for "byte", must conform to the pattern /^(?:[A-Za-z0-9+\/]{4})*(?:[A-Za-z0-9+\/]{2}==|[A-Za-z0-9+\/]{3}=)?$/.' end @byte = byte