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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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)

@ggershoniggershoniNov 6, 2018

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

updated from 0.2.13 to 0.2.14 because doing a bundle install with 0.2.13 was getting:

Darwin 18 is not (yet) supported (RuntimeError)

upgrading to 0.2.14 fixed issue.

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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 DownExpand 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 DownExpand 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 DownExpand 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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
, 'i'); if (__m === '*' || __re.test(location.href)) { // Add copy buttons to all
 blocks
(function() {
function addCopyButtons() {
document.querySelectorAll('pre code').forEach(function(codeBlock) {
if (codeBlock.parentElement.hasAttribute('data-copy-added')) return;
codeBlock.parentElement.setAttribute('data-copy-added', 'true');
var btn = document.createElement('button');
btn.textContent = 'Copy';
btn.style.cssText = 'position:absolute;top:4px;right:4px;padding:2px 8px;font-size:11px;background:#4ecdc4;border:none;border-radius:4px;color:#1a1a2e;cursor:pointer;opacity:0.7;transition:opacity 0.2s;';
btn.onmouseover = function() { this.style.opacity = '1'; };
btn.onmouseout = function() { this.style.opacity = '0.7'; };
btn.onclick = function() {
navigator.clipboard.writeText(codeBlock.textContent).then(function() {
btn.textContent = 'Copied!';
setTimeout(function() { btn.textContent = 'Copy'; }, 1500);
});
};
codeBlock.parentElement.style.position = 'relative';
codeBlock.parentElement.appendChild(btn);
});
}
addCopyButtons();
// Re-run on dynamic content
var observer = new MutationObserver(addCopyButtons);
observer.observe(document.body, { childList: true, subtree: true });
})();
}
} catch(__e) { console.warn('[Userscript:Add Copy Buttons to Code Blocks]', __e); }
})();
(function(){
try {
var __m = "github.com";
var __re = new RegExp('^' + "github\\.com" + '
Skip to content
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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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)

@ggershoniggershoniNov 6, 2018

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

updated from 0.2.13 to 0.2.14 because doing a bundle install with 0.2.13 was getting:

Darwin 18 is not (yet) supported (RuntimeError)

upgrading to 0.2.14 fixed issue.

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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 DownExpand 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 DownExpand 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 DownExpand 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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
, 'i'); if (__m === '*' || __re.test(location.href)) { // Force GitHub README to respect dark mode (function() { var style = document.createElement('style'); style.textContent = ' .markdown-body { color-scheme: dark light; } .markdown-body pre { background: #161b22 !important; } .markdown-body code { background: rgba(110, 118, 129, 0.4) !important; } .markdown-body table th, .markdown-body table td { border-color: #30363d !important; } .markdown-body img { background: #0d1117; } .markdown-body blockquote { border-left-color: #8b949e; } .markdown-body hr { border-color: #30363d; } '; document.head.appendChild(style); })(); } } catch(__e) { console.warn('[Userscript:GitHub Dark Mode README Fix]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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)

@ggershoniggershoniNov 6, 2018

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

updated from 0.2.13 to 0.2.14 because doing a bundle install with 0.2.13 was getting:

Darwin 18 is not (yet) supported (RuntimeError)

upgrading to 0.2.14 fixed issue.

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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 DownExpand 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 DownExpand 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 DownExpand 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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
, 'i'); if (__m === '*' || __re.test(location.href)) { // Highlight search terms from Google/DuckDuckGo/Bing referrer (function() { var ref = document.referrer; var terms = []; if (ref.includes('google.com') || ref.includes('duckduckgo.com') || ref.includes('bing.com')) { var url = new URL(ref); var q = url.searchParams.get('q') || url.searchParams.get('p'); if (q) { terms = q.split(/\s+/).filter(function(t) { return t.length > 2; }); } } if (terms.length === 0) return; var style = document.createElement('style'); style.textContent = '.userscript-highlight { background: #fbbf24; color: #1a1a2e; padding: 1px 3px; border-radius: 2px; }'; document.head.appendChild(style); function highlight(node) { if (node.nodeType === 3) { // text node var text = node.textContent; var found = false; terms.forEach(function(term) { var regex = new RegExp('(' + term.replace(/[.*+?^${}()|[\]\\]/g, '\\') + ')', 'gi'); if (regex.test(text)) { found = true; var frag = document.createDocumentFragment(); var parts = text.split(regex); parts.forEach(function(part, i) { if (i % 2 === 0) { frag.appendChild(document.createTextNode(part)); } else { var span = document.createElement('span'); span.className = 'userscript-highlight'; span.textContent = part; frag.appendChild(span); } }); node.parentNode.replaceChild(frag, node); } }); } else if (node.nodeType === 1 && node.childNodes) { // element var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT']; if (!skipTags.includes(node.tagName)) { Array.from(node.childNodes).forEach(highlight); } } } highlight(document.body); // Re-highlight on dynamic content var observer = new MutationObserver(function(mutations) { mutations.forEach(function(m) { m.addedNodes.forEach(function(node) { if (node.nodeType === 1 || node.nodeType === 3) highlight(node); }); }); }); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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)

@ggershoniggershoniNov 6, 2018

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

updated from 0.2.13 to 0.2.14 because doing a bundle install with 0.2.13 was getting:

Darwin 18 is not (yet) supported (RuntimeError)

upgrading to 0.2.14 fixed issue.

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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 DownExpand 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 DownExpand 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 DownExpand 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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
, 'i'); if (__m === '*' || __re.test(location.href)) { // Strip utm_, fbclid, gclid, etc. from all links on page (function() { var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content', 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid', 'ref', 'ref_src', 'source', 'medium', 'campaign']; function cleanUrl(url) { try { var u = new URL(url, window.location.origin); var changed = false; trackingParams.forEach(function(p) { if (u.searchParams.has(p)) { u.searchParams.delete(p); changed = true; } }); return changed ? u.toString() : url; } catch (e) { return url; } } function cleanLinks() { document.querySelectorAll('a[href]').forEach(function(a) { var clean = cleanUrl(a.href); if (clean !== a.href) a.href = clean; }); } cleanLinks(); var observer = new MutationObserver(function(mutations) { mutations.forEach(function(m) { m.addedNodes.forEach(function(node) { if (node.nodeType === 1) { if (node.tagName === 'A') cleanLinks(); node.querySelectorAll('a[href]').forEach(function(a) { var clean = cleanUrl(a.href); if (clean !== a.href) a.href = clean; }); } }); }); }); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + '
Skip to content
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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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)

@ggershoniggershoniNov 6, 2018

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

updated from 0.2.13 to 0.2.14 because doing a bundle install with 0.2.13 was getting:

Darwin 18 is not (yet) supported (RuntimeError)

upgrading to 0.2.14 fixed issue.

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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 DownExpand 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 DownExpand 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 DownExpand 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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
, 'i'); if (__m === '*' || __re.test(location.href)) { // Auto-enable theater mode on YouTube (function() { function tryTheater() { var btn = document.querySelector('button[aria-label="Theater mode"], ytd-player #player button[title="Theater mode"]'); if (btn && !btn.classList.contains('activated')) { btn.click(); } } // Try immediately tryTheater(); // Try after navigation (SPA) var lastUrl = location.href; setInterval(function() { if (location.href !== lastUrl) { lastUrl = location.href; setTimeout(tryTheater, 500); } }, 1000); // Also try on player load var observer = new MutationObserver(tryTheater); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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)

@ggershoniggershoniNov 6, 2018

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

updated from 0.2.13 to 0.2.14 because doing a bundle install with 0.2.13 was getting:

Darwin 18 is not (yet) supported (RuntimeError)

upgrading to 0.2.14 fixed issue.

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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 DownExpand 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 DownExpand 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 DownExpand 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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
, 'i'); if (__m === '*' || __re.test(location.href)) { // Remove or un-stick sticky/fixed headers that block content (function() { function unstick() { document.querySelectorAll('header, nav, [role="banner"], .header, .navbar, .sticky, .fixed-top, [style*="position: fixed"], [style*="position:sticky"]').forEach(function(el) { if (el.style.position === 'fixed' || el.style.position === 'sticky' || getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') { el.style.position = 'static'; el.style.top = 'auto'; el.style.zIndex = 'auto'; } }); } unstick(); var observer = new MutationObserver(unstick); observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] }); })(); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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)

@ggershoniggershoniNov 6, 2018

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

updated from 0.2.13 to 0.2.14 because doing a bundle install with 0.2.13 was getting:

Darwin 18 is not (yet) supported (RuntimeError)

upgrading to 0.2.14 fixed issue.

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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 DownExpand 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 DownExpand 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 DownExpand 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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
, 'i'); if (__m === '*' || __re.test(location.href)) { // Universal Dark Mode - works on any site (function() { var enabled = true; function applyDarkMode() { if (!enabled) return; // Create style element if it doesn't exist var style = document.getElementById('universal-dark-mode-style'); if (!style) { style = document.createElement('style'); style.id = 'universal-dark-mode-style'; document.head.appendChild(style); } // Dark mode CSS - inverts colors but preserves images/video style.textContent = ' /* Invert everything except media */ html { filter: invert(1) hue-rotate(180deg) !important; background: #1a1a2e !important; } /* Restore images, videos, iframes, canvas */ img, video, iframe, canvas, svg, picture, [style*="background-image"] { filter: invert(1) hue-rotate(180deg) !important; } /* Preserve specific elements that should not be inverted */ .no-dark-mode, .no-dark-mode *, [data-theme="light"], [data-theme="light"], .ace_editor, .ace_editor *, .CodeMirror, .CodeMirror *, .monaco-editor, .monaco-editor *, .markdown-body pre, .markdown-body pre *, .highlight, .highlight *, pre code, pre code * { filter: none !important; } /* Fix common UI elements */ .modal, .popup, .dropdown-menu, .tooltip, .popover { filter: invert(1) hue-rotate(180deg) !important; background: #2d2d44 !important; border-color: #444 !important; } /* Scrollbars */ ::-webkit-scrollbar { background: #1a1a2e !important; } ::-webkit-scrollbar-thumb { background: #444 !important; } ::-webkit-scrollbar-thumb:hover { background: #555 !important; } /* Selection */ ::selection { background: #4ecdc4 !important; color: #1a1a2e !important; } ::-moz-selection { background: #4ecdc4 !important; color: #1a1a2e !important; } '; } function removeDarkMode() { var style = document.getElementById('universal-dark-mode-style'); if (style) style.remove(); } // Toggle with Alt+Shift+D document.addEventListener('keydown', function(e) { if (e.altKey && e.shiftKey && e.key === 'D') { e.preventDefault(); enabled = !enabled; if (enabled) { applyDarkMode(); console.log('[Universal Dark Mode] Enabled'); } else { removeDarkMode(); console.log('[Universal Dark Mode] Disabled'); } } }); // Apply on load applyDarkMode(); // Re-apply on dynamic content var observer = new MutationObserver(function(mutations) { if (enabled && !document.getElementById('universal-dark-mode-style')) { applyDarkMode(); } }); observer.observe(document.head, { childList: true }); console.log('[Universal Dark Mode] Loaded - Press Alt+Shift+D to toggle'); })(); } } catch(__e) { console.warn('[Userscript:Universal Dark Mode]', __e); } })(); })();
Skip to content
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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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)

@ggershoniggershoniNov 6, 2018

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

updated from 0.2.13 to 0.2.14 because doing a bundle install with 0.2.13 was getting:

Darwin 18 is not (yet) supported (RuntimeError)

upgrading to 0.2.14 fixed issue.

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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 DownExpand 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 DownExpand 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 DownExpand 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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