Skip to content
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

Fix regexp in ruby-client #1521

Merged
merged 2 commits into from
Nov 28, 2018
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
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");
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wing328 @ggershoni
This PR does not have backward compatibility for these 3 patterns in definitions.
But, it seems that those regexp patterns are not correct regular expressions.

I think we do not need to support. WDYT?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was trying to ensure that if forward slashes where not surrounding the inputted regex then they would be added to force the inputted text to be a treated as a regex literal... I wanted to prevent someone from putting in malicious code via pattern. The Mustache template doesn't surround the regex with quotes anymore.

I was thinking we could put forward slashes in the Mustache template but then we might have an ending forward slash when we didn't want one i.e. /^pattern$/i/.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@meganemura I read the code in addRegularExpressionDelimiter and I like it. Thanks heaps for the refactor!

@wing328 I am happy to have those 3 tests removed. @meganemura is right, they are not correct regular expressions.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ggershoni Thanks for reviewing 👍

// 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