Skip to content

Commit

Permalink
Added tests and fix for issue #1392. Fix regex generated in Ruby clie…
Browse files Browse the repository at this point in the history
…nt. (#1393)

* Added tests and fix for issue #1392. Param validation with regex not recognizing \d correctly in Ruby client.

* Added generated files to pass ./bin/utils/ensure-up-to-date which is run by circleci
  • Loading branch information
ggershoni authored and wing328 committed Nov 7, 2018
1 parent f21640f commit 0e2e1bf
Show file tree
Hide file tree
Showing 13 changed files with 340 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,23 @@ public String toVarName(String name) {
return 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;
}

@Override
public String toParamName(String name) {
// should be the same as variable name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ GEM
public_suffix (>= 2.0.2, < 4.0)
autotest (4.4.6)
ZenTest (>= 4.4.1)
autotest-fsevent (0.2.13)
autotest-fsevent (0.2.14)
sys-uname
autotest-growl (0.2.16)
autotest-rails-pure (4.1.2)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -300,4 +300,32 @@ public void exampleStringFromXExampleParameterOAS3Test() {
CodegenParameter pp = op.pathParams.get(0);
Assert.assertEquals(pp.example, "'orderid123'");
}

/**
* We want to make sure that all Regex patterns:
* - Start with / so Ruby know this is a regex pattern
* - Have a second / that may be added to end if only 1 exists at start
* - If there are 2 / in pattern then don't add any more
*/
@Test(description = "test regex patterns")
public void exampleRegexParameterValidationOAS3Test() {
final OpenAPI openAPI = new OpenAPIParser()
.readLocation("src/test/resources/3_0/test_regex.yaml", null, new ParseOptions()).getOpenAPI();
final RubyClientCodegen codegen = new RubyClientCodegen();
final String path = "/ping";
final Operation p = openAPI.getPaths().get(path).getGet();
final CodegenOperation op = codegen.fromOperation(path, "get", p, openAPI.getComponents().getSchemas());
// pattern_no_forward_slashes '^pattern$'
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");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1338,6 +1338,14 @@ components:
format: password
maxLength: 64
minLength: 10
pattern_with_digits:
description: A string that is a 10 digit number. Can have leading zeros.
type: string
pattern: '^\d{10}$'
pattern_with_digits_and_delimiter:
description: A string starting with 'image_' (case insensitive) and one to three digits following i.e. Image_01.
type: string
pattern: '/^image_\d{1,3}$/i'
EnumClass:
type: string
default: '-efg'
Expand Down
51 changes: 51 additions & 0 deletions modules/openapi-generator/src/test/resources/3_0/test_regex.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
openapi: 3.0.1
info:
title: Test Regex generation for parameter validation
version: 1.0.0
components:
headers:

responses:
OK_200:
description: OK

paths:
/ping:
get:
summary: Get Payment Information
description: Returns the content of a payment object
parameters:
- name: pattern_no_forward_slashes
in: header
schema:
type: string
pattern: '^pattern$'
- name: pattern_two_slashes
in: header
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:
type: string
pattern: '/^pattern\d{3}$/i'

responses:
'200':
$ref: "#/components/responses/OK_200"
2 changes: 1 addition & 1 deletion samples/client/petstore/ruby/Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ GEM
public_suffix (>= 2.0.2, < 4.0)
autotest (4.4.6)
ZenTest (>= 4.4.1)
autotest-fsevent (0.2.13)
autotest-fsevent (0.2.14)
sys-uname
autotest-growl (0.2.16)
autotest-rails-pure (4.1.2)
Expand Down
2 changes: 2 additions & 0 deletions samples/client/petstore/ruby/docs/FormatTest.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,7 @@ Name | Type | Description | Notes
**date_time** | **DateTime** | | [optional]
**uuid** | **String** | | [optional]
**password** | **String** | |
**pattern_with_digits** | **String** | A string that is a 10 digit number. Can have leading zeros. | [optional]
**pattern_with_digits_and_delimiter** | **String** | A string starting with &#39;image_&#39; (case insensitive) and one to three digits following i.e. Image_01. | [optional]


58 changes: 54 additions & 4 deletions samples/client/petstore/ruby/lib/petstore/models/format_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ class FormatTest

attr_accessor :password

# A string that is a 10 digit number. Can have leading zeros.
attr_accessor :pattern_with_digits

# A string starting with 'image_' (case insensitive) and one to three digits following i.e. Image_01.
attr_accessor :pattern_with_digits_and_delimiter

# Attribute mapping from ruby-style variable name to JSON key.
def self.attribute_map
{
Expand All @@ -55,7 +61,9 @@ def self.attribute_map
:'date' => :'date',
:'date_time' => :'dateTime',
:'uuid' => :'uuid',
:'password' => :'password'
:'password' => :'password',
:'pattern_with_digits' => :'pattern_with_digits',
:'pattern_with_digits_and_delimiter' => :'pattern_with_digits_and_delimiter'
}
end

Expand All @@ -74,7 +82,9 @@ def self.openapi_types
:'date' => :'Date',
:'date_time' => :'DateTime',
:'uuid' => :'String',
:'password' => :'String'
:'password' => :'String',
:'pattern_with_digits' => :'String',
:'pattern_with_digits_and_delimiter' => :'String'
}
end

Expand Down Expand Up @@ -137,6 +147,14 @@ def initialize(attributes = {})
if attributes.has_key?(:'password')
self.password = attributes[:'password']
end

if attributes.has_key?(:'pattern_with_digits')
self.pattern_with_digits = attributes[:'pattern_with_digits']
end

if attributes.has_key?(:'pattern_with_digits_and_delimiter')
self.pattern_with_digits_and_delimiter = attributes[:'pattern_with_digits_and_delimiter']
end
end

# Show invalid properties with the reasons. Usually used together with valid?
Expand Down Expand Up @@ -211,6 +229,14 @@ def list_invalid_properties
invalid_properties.push('invalid value for "password", the character length must be great than or equal to 10.')
end

if !@pattern_with_digits.nil? && @pattern_with_digits !~ Regexp.new(/^\d{10}$/)
invalid_properties.push('invalid value for "pattern_with_digits", must conform to the pattern /^\d{10}$/.')
end

if !@pattern_with_digits_and_delimiter.nil? && @pattern_with_digits_and_delimiter !~ Regexp.new(/^image_\d{1,3}$/i)
invalid_properties.push('invalid value for "pattern_with_digits_and_delimiter", must conform to the pattern /^image_\d{1,3}$/i.')
end

invalid_properties
end

Expand All @@ -234,6 +260,8 @@ def valid?
return false if @password.nil?
return false if @password.to_s.length > 64
return false if @password.to_s.length < 10
return false if !@pattern_with_digits.nil? && @pattern_with_digits !~ Regexp.new(/^\d{10}$/)
return false if !@pattern_with_digits_and_delimiter.nil? && @pattern_with_digits_and_delimiter !~ Regexp.new(/^image_\d{1,3}$/i)
true
end

Expand Down Expand Up @@ -339,6 +367,26 @@ def password=(password)
@password = password
end

# Custom attribute writer method with validation
# @param [Object] pattern_with_digits Value to be assigned
def pattern_with_digits=(pattern_with_digits)
if !pattern_with_digits.nil? && pattern_with_digits !~ Regexp.new(/^\d{10}$/)
fail ArgumentError, 'invalid value for "pattern_with_digits", must conform to the pattern /^\d{10}$/.'
end

@pattern_with_digits = pattern_with_digits
end

# Custom attribute writer method with validation
# @param [Object] pattern_with_digits_and_delimiter Value to be assigned
def pattern_with_digits_and_delimiter=(pattern_with_digits_and_delimiter)
if !pattern_with_digits_and_delimiter.nil? && pattern_with_digits_and_delimiter !~ Regexp.new(/^image_\d{1,3}$/i)
fail ArgumentError, 'invalid value for "pattern_with_digits_and_delimiter", must conform to the pattern /^image_\d{1,3}$/i.'
end

@pattern_with_digits_and_delimiter = pattern_with_digits_and_delimiter
end

# Checks equality by comparing each attribute.
# @param [Object] Object to be compared
def ==(o)
Expand All @@ -356,7 +404,9 @@ def ==(o)
date == o.date &&
date_time == o.date_time &&
uuid == o.uuid &&
password == o.password
password == o.password &&
pattern_with_digits == o.pattern_with_digits &&
pattern_with_digits_and_delimiter == o.pattern_with_digits_and_delimiter
end

# @see the `==` method
Expand All @@ -368,7 +418,7 @@ def eql?(o)
# Calculates hash code according to all attributes.
# @return [Fixnum] Hash code
def hash
[integer, int32, int64, number, float, double, string, byte, binary, date, date_time, uuid, password].hash
[integer, int32, int64, number, float, double, string, byte, binary, date, date_time, uuid, password, pattern_with_digits, pattern_with_digits_and_delimiter].hash
end

# Builds the object from hash
Expand Down
27 changes: 27 additions & 0 deletions samples/client/petstore/ruby/spec/models/format_test_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,31 @@
end
end

describe 'test attribute "pattern_with_digits"' do
it 'should accept string "1234567890"' do
@instance.pattern_with_digits = '1234567890'
end

it 'should accept string with leading zero "0123456789"' do
@instance.pattern_with_digits = '0123456789'
end

it 'should reject string with non digits "ABC3456789"' do
expect {@instance.pattern_with_digits = 'ABC3456789'}.to raise_error(ArgumentError)
end

it 'should reject string less than 10 in length "123456789"' do
expect {@instance.pattern_with_digits = '123456789'}.to raise_error(ArgumentError)
end

it 'should reject string more than 10 in length "0123456789123"' do
expect {@instance.pattern_with_digits = '0123456789123'}.to raise_error(ArgumentError)
end
end

describe 'test attribute "pattern_with_digits_and_delimiter"' do
it 'should accept string "Image_01"' do
@instance.pattern_with_digits_and_delimiter = 'Image_01'
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ Name | Type | Description | Notes
**date_time** | [**\DateTime**](\DateTime.md) | | [optional]
**uuid** | **string** | | [optional]
**password** | **string** | |
**pattern_with_digits** | **string** | A string that is a 10 digit number. Can have leading zeros. | [optional]
**pattern_with_digits_and_delimiter** | **string** | A string starting with &#39;image_&#39; (case insensitive) and one to three digits following i.e. Image_01. | [optional]

[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)

Expand Down
Loading

0 comments on commit 0e2e1bf

Please sign in to comment.