Skip to content

Commit

Permalink
Fix regexp in ruby-client (OpenAPITools#1521)
Browse files Browse the repository at this point in the history
* Fix regexp in ruby-client

* Remove tests for unknown regexp patterns
  • Loading branch information
meganemura authored and wing328 committed Nov 28, 2018
1 parent db91e0a commit bef1f7f
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}
15 changes: 0 additions & 15 deletions modules/openapi-generator/src/test/resources/3_0/test_regex.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
10 changes: 5 additions & 5 deletions samples/client/petstore/ruby/lib/petstore/models/format_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit bef1f7f

Please sign in to comment.