forked from loafle/openapi-generator-original
add validation to ruby model
This commit is contained in:
parent
d225da082c
commit
e143c6cd2f
@ -42,6 +42,7 @@ public class CodegenProperty {
|
|||||||
public Map<String, Object> allowableValues;
|
public Map<String, Object> allowableValues;
|
||||||
public CodegenProperty items;
|
public CodegenProperty items;
|
||||||
public Map<String, Object> vendorExtensions;
|
public Map<String, Object> vendorExtensions;
|
||||||
|
public Boolean needValidation; // true if pattern, maximum, etc are set (only used in the mustache template)
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int hashCode()
|
public int hashCode()
|
||||||
|
@ -327,6 +327,16 @@ public class DefaultCodegen {
|
|||||||
this.ensureUniqueParams = ensureUniqueParams;
|
this.ensureUniqueParams = ensureUniqueParams;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the JSON schema pattern (http://json-schema.org/latest/json-schema-validation.html#anchor33)
|
||||||
|
*
|
||||||
|
* @param pattern the pattern (regular expression)
|
||||||
|
* @return properly-escaped pattern
|
||||||
|
*/
|
||||||
|
public String toJSONSchemaPattern(String pattern) {
|
||||||
|
return escapeText(pattern);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the file name of the Api Test
|
* Return the file name of the Api Test
|
||||||
*
|
*
|
||||||
@ -1094,6 +1104,10 @@ public class DefaultCodegen {
|
|||||||
property.exclusiveMinimum = np.getExclusiveMinimum();
|
property.exclusiveMinimum = np.getExclusiveMinimum();
|
||||||
property.exclusiveMaximum = np.getExclusiveMaximum();
|
property.exclusiveMaximum = np.getExclusiveMaximum();
|
||||||
|
|
||||||
|
// check if any validation rule defined
|
||||||
|
if (property.minimum != null || property.maximum != null || property.exclusiveMinimum != null || property.exclusiveMaximum != null)
|
||||||
|
property.needValidation = true;
|
||||||
|
|
||||||
// legacy support
|
// legacy support
|
||||||
Map<String, Object> allowableValues = new HashMap<String, Object>();
|
Map<String, Object> allowableValues = new HashMap<String, Object>();
|
||||||
if (np.getMinimum() != null) {
|
if (np.getMinimum() != null) {
|
||||||
@ -1111,7 +1125,12 @@ public class DefaultCodegen {
|
|||||||
StringProperty sp = (StringProperty) p;
|
StringProperty sp = (StringProperty) p;
|
||||||
property.maxLength = sp.getMaxLength();
|
property.maxLength = sp.getMaxLength();
|
||||||
property.minLength = sp.getMinLength();
|
property.minLength = sp.getMinLength();
|
||||||
property.pattern = sp.getPattern();
|
property.pattern = toJSONSchemaPattern(sp.getPattern());
|
||||||
|
|
||||||
|
// check if any validation rule defined
|
||||||
|
if (property.pattern != null || property.minLength != null || property.maxLength != null)
|
||||||
|
property.needValidation = true;
|
||||||
|
|
||||||
property.isString = true;
|
property.isString = true;
|
||||||
if (sp.getEnum() != null) {
|
if (sp.getEnum() != null) {
|
||||||
List<String> _enum = sp.getEnum();
|
List<String> _enum = sp.getEnum();
|
||||||
@ -1802,9 +1821,6 @@ public class DefaultCodegen {
|
|||||||
if (model.complexType != null) {
|
if (model.complexType != null) {
|
||||||
imports.add(model.complexType);
|
imports.add(model.complexType);
|
||||||
}
|
}
|
||||||
p.maxLength = qp.getMaxLength();
|
|
||||||
p.minLength = qp.getMinLength();
|
|
||||||
p.pattern = qp.getPattern();
|
|
||||||
|
|
||||||
p.maximum = qp.getMaximum();
|
p.maximum = qp.getMaximum();
|
||||||
p.exclusiveMaximum = qp.isExclusiveMaximum();
|
p.exclusiveMaximum = qp.isExclusiveMaximum();
|
||||||
@ -1812,7 +1828,7 @@ public class DefaultCodegen {
|
|||||||
p.exclusiveMinimum = qp.isExclusiveMinimum();
|
p.exclusiveMinimum = qp.isExclusiveMinimum();
|
||||||
p.maxLength = qp.getMaxLength();
|
p.maxLength = qp.getMaxLength();
|
||||||
p.minLength = qp.getMinLength();
|
p.minLength = qp.getMinLength();
|
||||||
p.pattern = qp.getPattern();
|
p.pattern = toJSONSchemaPattern(qp.getPattern());
|
||||||
p.maxItems = qp.getMaxItems();
|
p.maxItems = qp.getMaxItems();
|
||||||
p.minItems = qp.getMinItems();
|
p.minItems = qp.getMinItems();
|
||||||
p.uniqueItems = qp.isUniqueItems();
|
p.uniqueItems = qp.isUniqueItems();
|
||||||
|
@ -31,6 +31,7 @@ Then either install the gem locally:
|
|||||||
```shell
|
```shell
|
||||||
gem install ./{{{gemName}}}-{{{gemVersion}}}.gem
|
gem install ./{{{gemName}}}-{{{gemVersion}}}.gem
|
||||||
```
|
```
|
||||||
|
(for development, run `gem install --dev ./{{{gemName}}}-{{{gemVersion}}}.gem` to install the development dependencies)
|
||||||
|
|
||||||
or publish the gem to a gem hosting service, e.g. [RubyGems](https://rubygems.org/).
|
or publish the gem to a gem hosting service, e.g. [RubyGems](https://rubygems.org/).
|
||||||
|
|
||||||
|
@ -47,7 +47,9 @@ module {{moduleName}}{{#models}}{{#model}}{{#description}}
|
|||||||
end
|
end
|
||||||
{{/vars}}
|
{{/vars}}
|
||||||
end
|
end
|
||||||
{{#vars}}{{#isEnum}}
|
|
||||||
|
{{#vars}}
|
||||||
|
{{#isEnum}}
|
||||||
# Custom attribute writer method checking allowed values (enum).
|
# Custom attribute writer method checking allowed values (enum).
|
||||||
# @param [Object] {{{name}}} Object to be assigned
|
# @param [Object] {{{name}}} Object to be assigned
|
||||||
def {{{name}}}=({{{name}}})
|
def {{{name}}}=({{{name}}})
|
||||||
@ -57,7 +59,29 @@ module {{moduleName}}{{#models}}{{#model}}{{#description}}
|
|||||||
end
|
end
|
||||||
@{{{name}}} = {{{name}}}
|
@{{{name}}} = {{{name}}}
|
||||||
end
|
end
|
||||||
{{/isEnum}}{{/vars}}
|
|
||||||
|
{{/isEnum}}
|
||||||
|
{{^isEnum}}
|
||||||
|
{{#needValidation}}
|
||||||
|
# Custom attribute writer method with validation
|
||||||
|
# @param [Object] {{{name}}} Value to be assigned
|
||||||
|
def {{{name}}}=({{{name}}})
|
||||||
|
{{#maximum}}
|
||||||
|
if {{{name}}} > {{{maximum}}}
|
||||||
|
fail "invalid value for '{{{name}}}', must be smaller than or equal to {{{maximum}}}"
|
||||||
|
end
|
||||||
|
{{/maximum}}
|
||||||
|
{{#minimum}}
|
||||||
|
if {{{name}}} < {{{minimum}}}
|
||||||
|
fail "invalid value for '{{{name}}}', must be greater than or equal to {{{minimum}}}"
|
||||||
|
end
|
||||||
|
{{/minimum}}
|
||||||
|
@{{{name}}} = {{{name}}}
|
||||||
|
end
|
||||||
|
|
||||||
|
{{/needValidation}}
|
||||||
|
{{/isEnum}}
|
||||||
|
{{/vars}}
|
||||||
# Checks equality by comparing each attribute.
|
# Checks equality by comparing each attribute.
|
||||||
# @param [Object] Object to be compared
|
# @param [Object] Object to be compared
|
||||||
def ==(o)
|
def ==(o)
|
||||||
|
@ -761,22 +761,33 @@ definitions:
|
|||||||
properties:
|
properties:
|
||||||
integer:
|
integer:
|
||||||
type: integer
|
type: integer
|
||||||
|
maximum: 100
|
||||||
|
minimum: 10
|
||||||
int32:
|
int32:
|
||||||
type: integer
|
type: integer
|
||||||
format: int32
|
format: int32
|
||||||
|
maximum: 200
|
||||||
|
minimum: 20
|
||||||
int64:
|
int64:
|
||||||
type: integer
|
type: integer
|
||||||
format: int64
|
format: int64
|
||||||
number:
|
number:
|
||||||
|
maximum: 543.2
|
||||||
|
minimum: 32.1
|
||||||
type: number
|
type: number
|
||||||
float:
|
float:
|
||||||
type: number
|
type: number
|
||||||
format: float
|
format: float
|
||||||
|
maximum: 987.6
|
||||||
|
minimum: 54.3
|
||||||
double:
|
double:
|
||||||
type: number
|
type: number
|
||||||
format: double
|
format: double
|
||||||
|
maximum: 123.4
|
||||||
|
minimum: 67.8
|
||||||
string:
|
string:
|
||||||
type: string
|
type: string
|
||||||
|
pattern: /[a‑z]/i
|
||||||
byte:
|
byte:
|
||||||
type: string
|
type: string
|
||||||
format: byte
|
format: byte
|
||||||
|
@ -22,29 +22,31 @@ GEM
|
|||||||
ethon (0.8.1)
|
ethon (0.8.1)
|
||||||
ffi (>= 1.3.0)
|
ffi (>= 1.3.0)
|
||||||
ffi (1.9.8)
|
ffi (1.9.8)
|
||||||
|
hashdiff (0.3.0)
|
||||||
json (1.8.3)
|
json (1.8.3)
|
||||||
rspec (3.2.0)
|
rspec (3.4.0)
|
||||||
rspec-core (~> 3.2.0)
|
rspec-core (~> 3.4.0)
|
||||||
rspec-expectations (~> 3.2.0)
|
rspec-expectations (~> 3.4.0)
|
||||||
rspec-mocks (~> 3.2.0)
|
rspec-mocks (~> 3.4.0)
|
||||||
rspec-core (3.2.2)
|
rspec-core (3.4.4)
|
||||||
rspec-support (~> 3.2.0)
|
rspec-support (~> 3.4.0)
|
||||||
rspec-expectations (3.2.0)
|
rspec-expectations (3.4.0)
|
||||||
diff-lcs (>= 1.2.0, < 2.0)
|
diff-lcs (>= 1.2.0, < 2.0)
|
||||||
rspec-support (~> 3.2.0)
|
rspec-support (~> 3.4.0)
|
||||||
rspec-mocks (3.2.1)
|
rspec-mocks (3.4.1)
|
||||||
diff-lcs (>= 1.2.0, < 2.0)
|
diff-lcs (>= 1.2.0, < 2.0)
|
||||||
rspec-support (~> 3.2.0)
|
rspec-support (~> 3.4.0)
|
||||||
rspec-support (3.2.2)
|
rspec-support (3.4.1)
|
||||||
safe_yaml (1.0.4)
|
safe_yaml (1.0.4)
|
||||||
sys-uname (0.9.2)
|
sys-uname (0.9.2)
|
||||||
ffi (>= 1.0.0)
|
ffi (>= 1.0.0)
|
||||||
typhoeus (1.0.1)
|
typhoeus (1.0.1)
|
||||||
ethon (>= 0.8.0)
|
ethon (>= 0.8.0)
|
||||||
vcr (2.9.3)
|
vcr (3.0.1)
|
||||||
webmock (1.21.0)
|
webmock (1.24.5)
|
||||||
addressable (>= 2.3.6)
|
addressable (>= 2.3.6)
|
||||||
crack (>= 0.3.2)
|
crack (>= 0.3.2)
|
||||||
|
hashdiff
|
||||||
|
|
||||||
PLATFORMS
|
PLATFORMS
|
||||||
ruby
|
ruby
|
||||||
@ -55,6 +57,6 @@ DEPENDENCIES
|
|||||||
autotest-growl (~> 0.2, >= 0.2.16)
|
autotest-growl (~> 0.2, >= 0.2.16)
|
||||||
autotest-rails-pure (~> 4.1, >= 4.1.2)
|
autotest-rails-pure (~> 4.1, >= 4.1.2)
|
||||||
petstore!
|
petstore!
|
||||||
rspec (~> 3.2, >= 3.2.0)
|
rspec (~> 3.4, >= 3.4.0)
|
||||||
vcr (~> 2.9, >= 2.9.3)
|
vcr (~> 3.0, >= 3.0.1)
|
||||||
webmock (~> 1.6, >= 1.6.2)
|
webmock (~> 1.24, >= 1.24.3)
|
||||||
|
@ -8,7 +8,7 @@ This SDK is automatically generated by the [Swagger Codegen](https://github.com/
|
|||||||
|
|
||||||
- API version: 1.0.0
|
- API version: 1.0.0
|
||||||
- Package version: 1.0.0
|
- Package version: 1.0.0
|
||||||
- Build date: 2016-04-20T18:46:00.664+08:00
|
- Build date: 2016-04-25T16:31:40.260+08:00
|
||||||
- Build package: class io.swagger.codegen.languages.RubyClientCodegen
|
- Build package: class io.swagger.codegen.languages.RubyClientCodegen
|
||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
@ -26,6 +26,7 @@ Then either install the gem locally:
|
|||||||
```shell
|
```shell
|
||||||
gem install ./petstore-1.0.0.gem
|
gem install ./petstore-1.0.0.gem
|
||||||
```
|
```
|
||||||
|
(for development, run `gem install --dev ./petstore-1.0.0.gem` to install the development dependencies)
|
||||||
|
|
||||||
or publish the gem to a gem hosting service, e.g. [RubyGems](https://rubygems.org/).
|
or publish the gem to a gem hosting service, e.g. [RubyGems](https://rubygems.org/).
|
||||||
|
|
||||||
|
@ -10,10 +10,10 @@ Name | Type | Description | Notes
|
|||||||
**float** | **Float** | | [optional]
|
**float** | **Float** | | [optional]
|
||||||
**double** | **Float** | | [optional]
|
**double** | **Float** | | [optional]
|
||||||
**string** | **String** | | [optional]
|
**string** | **String** | | [optional]
|
||||||
**byte** | **String** | | [optional]
|
**byte** | **String** | |
|
||||||
**binary** | **String** | | [optional]
|
**binary** | **String** | | [optional]
|
||||||
**date** | **Date** | | [optional]
|
**date** | **Date** | |
|
||||||
**date_time** | **DateTime** | | [optional]
|
**date_time** | **DateTime** | | [optional]
|
||||||
**password** | **String** | | [optional]
|
**password** | **String** | |
|
||||||
|
|
||||||
|
|
||||||
|
@ -5,5 +5,6 @@ Name | Type | Description | Notes
|
|||||||
------------ | ------------- | ------------- | -------------
|
------------ | ------------- | ------------- | -------------
|
||||||
**name** | **Integer** | |
|
**name** | **Integer** | |
|
||||||
**snake_case** | **Integer** | | [optional]
|
**snake_case** | **Integer** | | [optional]
|
||||||
|
**property** | **String** | | [optional]
|
||||||
|
|
||||||
|
|
||||||
|
@ -124,6 +124,72 @@ module Petstore
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Custom attribute writer method with validation
|
||||||
|
# @param [Object] integer Value to be assigned
|
||||||
|
def integer=(integer)
|
||||||
|
if integer > 100.0
|
||||||
|
fail "invalid value for 'integer', must be smaller than or equal to 100.0"
|
||||||
|
end
|
||||||
|
if integer < 10.0
|
||||||
|
fail "invalid value for 'integer', must be greater than or equal to 10.0"
|
||||||
|
end
|
||||||
|
@integer = integer
|
||||||
|
end
|
||||||
|
|
||||||
|
# Custom attribute writer method with validation
|
||||||
|
# @param [Object] int32 Value to be assigned
|
||||||
|
def int32=(int32)
|
||||||
|
if int32 > 200.0
|
||||||
|
fail "invalid value for 'int32', must be smaller than or equal to 200.0"
|
||||||
|
end
|
||||||
|
if int32 < 20.0
|
||||||
|
fail "invalid value for 'int32', must be greater than or equal to 20.0"
|
||||||
|
end
|
||||||
|
@int32 = int32
|
||||||
|
end
|
||||||
|
|
||||||
|
# Custom attribute writer method with validation
|
||||||
|
# @param [Object] number Value to be assigned
|
||||||
|
def number=(number)
|
||||||
|
if number > 543.2
|
||||||
|
fail "invalid value for 'number', must be smaller than or equal to 543.2"
|
||||||
|
end
|
||||||
|
if number < 32.1
|
||||||
|
fail "invalid value for 'number', must be greater than or equal to 32.1"
|
||||||
|
end
|
||||||
|
@number = number
|
||||||
|
end
|
||||||
|
|
||||||
|
# Custom attribute writer method with validation
|
||||||
|
# @param [Object] float Value to be assigned
|
||||||
|
def float=(float)
|
||||||
|
if float > 987.6
|
||||||
|
fail "invalid value for 'float', must be smaller than or equal to 987.6"
|
||||||
|
end
|
||||||
|
if float < 54.3
|
||||||
|
fail "invalid value for 'float', must be greater than or equal to 54.3"
|
||||||
|
end
|
||||||
|
@float = float
|
||||||
|
end
|
||||||
|
|
||||||
|
# Custom attribute writer method with validation
|
||||||
|
# @param [Object] double Value to be assigned
|
||||||
|
def double=(double)
|
||||||
|
if double > 123.4
|
||||||
|
fail "invalid value for 'double', must be smaller than or equal to 123.4"
|
||||||
|
end
|
||||||
|
if double < 67.8
|
||||||
|
fail "invalid value for 'double', must be greater than or equal to 67.8"
|
||||||
|
end
|
||||||
|
@double = double
|
||||||
|
end
|
||||||
|
|
||||||
|
# Custom attribute writer method with validation
|
||||||
|
# @param [Object] string Value to be assigned
|
||||||
|
def string=(string)
|
||||||
|
@string = string
|
||||||
|
end
|
||||||
|
|
||||||
# Checks equality by comparing each attribute.
|
# Checks equality by comparing each attribute.
|
||||||
# @param [Object] Object to be compared
|
# @param [Object] Object to be compared
|
||||||
def ==(o)
|
def ==(o)
|
||||||
|
@ -23,11 +23,14 @@ module Petstore
|
|||||||
|
|
||||||
attr_accessor :snake_case
|
attr_accessor :snake_case
|
||||||
|
|
||||||
|
attr_accessor :property
|
||||||
|
|
||||||
# Attribute mapping from ruby-style variable name to JSON key.
|
# Attribute mapping from ruby-style variable name to JSON key.
|
||||||
def self.attribute_map
|
def self.attribute_map
|
||||||
{
|
{
|
||||||
:'name' => :'name',
|
:'name' => :'name',
|
||||||
:'snake_case' => :'snake_case'
|
:'snake_case' => :'snake_case',
|
||||||
|
:'property' => :'property'
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -35,7 +38,8 @@ module Petstore
|
|||||||
def self.swagger_types
|
def self.swagger_types
|
||||||
{
|
{
|
||||||
:'name' => :'Integer',
|
:'name' => :'Integer',
|
||||||
:'snake_case' => :'Integer'
|
:'snake_case' => :'Integer',
|
||||||
|
:'property' => :'String'
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -53,6 +57,9 @@ module Petstore
|
|||||||
if attributes[:'snake_case']
|
if attributes[:'snake_case']
|
||||||
self.snake_case = attributes[:'snake_case']
|
self.snake_case = attributes[:'snake_case']
|
||||||
end
|
end
|
||||||
|
if attributes[:'property']
|
||||||
|
self.property = attributes[:'property']
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Checks equality by comparing each attribute.
|
# Checks equality by comparing each attribute.
|
||||||
@ -61,7 +68,8 @@ module Petstore
|
|||||||
return true if self.equal?(o)
|
return true if self.equal?(o)
|
||||||
self.class == o.class &&
|
self.class == o.class &&
|
||||||
name == o.name &&
|
name == o.name &&
|
||||||
snake_case == o.snake_case
|
snake_case == o.snake_case &&
|
||||||
|
property == o.property
|
||||||
end
|
end
|
||||||
|
|
||||||
# @see the `==` method
|
# @see the `==` method
|
||||||
@ -73,7 +81,7 @@ module Petstore
|
|||||||
# Calculates hash code according to all attributes.
|
# Calculates hash code according to all attributes.
|
||||||
# @return [Fixnum] Hash code
|
# @return [Fixnum] Hash code
|
||||||
def hash
|
def hash
|
||||||
[name, snake_case].hash
|
[name, snake_case, property].hash
|
||||||
end
|
end
|
||||||
|
|
||||||
# Builds the object from hash
|
# Builds the object from hash
|
||||||
|
@ -56,5 +56,15 @@ describe 'Name' do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe 'test attribute "property"' do
|
||||||
|
it 'should work' do
|
||||||
|
# assertion here
|
||||||
|
# should be_a()
|
||||||
|
# should be_nil
|
||||||
|
# should ==
|
||||||
|
# should_not ==
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user