mirror of
https://github.com/OpenAPITools/openapi-generator.git
synced 2025-10-13 16:03:43 +00:00
Add tests for oneOf models in ruby client (#16137)
* add test for oneOf model in ruby * add new files * add tests for oneOf model without discriminator
This commit is contained in:
parent
7dee666826
commit
ddc2b3e560
@ -1,7 +1,7 @@
|
||||
generatorName: ruby
|
||||
outputDir: samples/client/petstore/ruby
|
||||
library: typhoeus
|
||||
inputSpec: modules/openapi-generator/src/test/resources/3_0/petstore-with-fake-endpoints-models-for-testing.yaml
|
||||
inputSpec: modules/openapi-generator/src/test/resources/3_0/ruby/petstore-with-fake-endpoints-models-for-testing.yaml
|
||||
templateDir: modules/openapi-generator/src/main/resources/ruby-client
|
||||
additionalProperties:
|
||||
gemVersion: 1.0.0
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -36,6 +36,8 @@ docs/FormatTest.md
|
||||
docs/HasOnlyReadOnly.md
|
||||
docs/HealthCheckResult.md
|
||||
docs/List.md
|
||||
docs/Mammal.md
|
||||
docs/MammalWithoutDiscriminator.md
|
||||
docs/MapTest.md
|
||||
docs/MixedPropertiesAndAdditionalPropertiesClass.md
|
||||
docs/Model200Response.md
|
||||
@ -60,6 +62,8 @@ docs/StoreApi.md
|
||||
docs/Tag.md
|
||||
docs/User.md
|
||||
docs/UserApi.md
|
||||
docs/Whale.md
|
||||
docs/Zebra.md
|
||||
git_push.sh
|
||||
lib/petstore.rb
|
||||
lib/petstore/api/another_fake_api.rb
|
||||
@ -98,6 +102,8 @@ lib/petstore/models/format_test.rb
|
||||
lib/petstore/models/has_only_read_only.rb
|
||||
lib/petstore/models/health_check_result.rb
|
||||
lib/petstore/models/list.rb
|
||||
lib/petstore/models/mammal.rb
|
||||
lib/petstore/models/mammal_without_discriminator.rb
|
||||
lib/petstore/models/map_test.rb
|
||||
lib/petstore/models/mixed_properties_and_additional_properties_class.rb
|
||||
lib/petstore/models/model200_response.rb
|
||||
@ -119,6 +125,8 @@ lib/petstore/models/single_ref_type.rb
|
||||
lib/petstore/models/special_model_name.rb
|
||||
lib/petstore/models/tag.rb
|
||||
lib/petstore/models/user.rb
|
||||
lib/petstore/models/whale.rb
|
||||
lib/petstore/models/zebra.rb
|
||||
lib/petstore/version.rb
|
||||
petstore.gemspec
|
||||
spec/api_client_spec.rb
|
||||
|
@ -147,6 +147,8 @@ Class | Method | HTTP request | Description
|
||||
- [Petstore::HasOnlyReadOnly](docs/HasOnlyReadOnly.md)
|
||||
- [Petstore::HealthCheckResult](docs/HealthCheckResult.md)
|
||||
- [Petstore::List](docs/List.md)
|
||||
- [Petstore::Mammal](docs/Mammal.md)
|
||||
- [Petstore::MammalWithoutDiscriminator](docs/MammalWithoutDiscriminator.md)
|
||||
- [Petstore::MapTest](docs/MapTest.md)
|
||||
- [Petstore::MixedPropertiesAndAdditionalPropertiesClass](docs/MixedPropertiesAndAdditionalPropertiesClass.md)
|
||||
- [Petstore::Model200Response](docs/Model200Response.md)
|
||||
@ -168,6 +170,8 @@ Class | Method | HTTP request | Description
|
||||
- [Petstore::SpecialModelName](docs/SpecialModelName.md)
|
||||
- [Petstore::Tag](docs/Tag.md)
|
||||
- [Petstore::User](docs/User.md)
|
||||
- [Petstore::Whale](docs/Whale.md)
|
||||
- [Petstore::Zebra](docs/Zebra.md)
|
||||
|
||||
|
||||
## Documentation for Authorization
|
||||
|
63
samples/client/petstore/ruby/docs/Mammal.md
Normal file
63
samples/client/petstore/ruby/docs/Mammal.md
Normal file
@ -0,0 +1,63 @@
|
||||
# Petstore::Mammal
|
||||
|
||||
## Class instance methods
|
||||
|
||||
### `openapi_one_of`
|
||||
|
||||
Returns the list of classes defined in oneOf.
|
||||
|
||||
#### Example
|
||||
|
||||
```ruby
|
||||
require 'petstore'
|
||||
|
||||
Petstore::Mammal.openapi_one_of
|
||||
# =>
|
||||
# [
|
||||
# :'Whale',
|
||||
# :'Zebra'
|
||||
# ]
|
||||
```
|
||||
|
||||
### `openapi_discriminator_name`
|
||||
|
||||
Returns the discriminator's property name.
|
||||
|
||||
#### Example
|
||||
|
||||
```ruby
|
||||
require 'petstore'
|
||||
|
||||
Petstore::Mammal.openapi_discriminator_name
|
||||
# => :'classname'
|
||||
```
|
||||
```
|
||||
|
||||
### build
|
||||
|
||||
Find the appropriate object from the `openapi_one_of` list and casts the data into it.
|
||||
|
||||
#### Example
|
||||
|
||||
```ruby
|
||||
require 'petstore'
|
||||
|
||||
Petstore::Mammal.build(data)
|
||||
# => #<Whale:0x00007fdd4aab02a0>
|
||||
|
||||
Petstore::Mammal.build(data_that_doesnt_match)
|
||||
# => nil
|
||||
```
|
||||
|
||||
#### Parameters
|
||||
|
||||
| Name | Type | Description |
|
||||
| ---- | ---- | ----------- |
|
||||
| **data** | **Mixed** | data to be matched against the list of oneOf items |
|
||||
|
||||
#### Return type
|
||||
|
||||
- `Whale`
|
||||
- `Zebra`
|
||||
- `nil` (if no type matches)
|
||||
|
@ -0,0 +1,49 @@
|
||||
# Petstore::MammalWithoutDiscriminator
|
||||
|
||||
## Class instance methods
|
||||
|
||||
### `openapi_one_of`
|
||||
|
||||
Returns the list of classes defined in oneOf.
|
||||
|
||||
#### Example
|
||||
|
||||
```ruby
|
||||
require 'petstore'
|
||||
|
||||
Petstore::MammalWithoutDiscriminator.openapi_one_of
|
||||
# =>
|
||||
# [
|
||||
# :'Whale',
|
||||
# :'Zebra'
|
||||
# ]
|
||||
```
|
||||
|
||||
### build
|
||||
|
||||
Find the appropriate object from the `openapi_one_of` list and casts the data into it.
|
||||
|
||||
#### Example
|
||||
|
||||
```ruby
|
||||
require 'petstore'
|
||||
|
||||
Petstore::MammalWithoutDiscriminator.build(data)
|
||||
# => #<Whale:0x00007fdd4aab02a0>
|
||||
|
||||
Petstore::MammalWithoutDiscriminator.build(data_that_doesnt_match)
|
||||
# => nil
|
||||
```
|
||||
|
||||
#### Parameters
|
||||
|
||||
| Name | Type | Description |
|
||||
| ---- | ---- | ----------- |
|
||||
| **data** | **Mixed** | data to be matched against the list of oneOf items |
|
||||
|
||||
#### Return type
|
||||
|
||||
- `Whale`
|
||||
- `Zebra`
|
||||
- `nil` (if no type matches)
|
||||
|
22
samples/client/petstore/ruby/docs/Whale.md
Normal file
22
samples/client/petstore/ruby/docs/Whale.md
Normal file
@ -0,0 +1,22 @@
|
||||
# Petstore::Whale
|
||||
|
||||
## Properties
|
||||
|
||||
| Name | Type | Description | Notes |
|
||||
| ---- | ---- | ----------- | ----- |
|
||||
| **has_baleen** | **Boolean** | | [optional] |
|
||||
| **has_teeth** | **Boolean** | | [optional] |
|
||||
| **classname** | **String** | | |
|
||||
|
||||
## Example
|
||||
|
||||
```ruby
|
||||
require 'petstore'
|
||||
|
||||
instance = Petstore::Whale.new(
|
||||
has_baleen: null,
|
||||
has_teeth: null,
|
||||
classname: null
|
||||
)
|
||||
```
|
||||
|
20
samples/client/petstore/ruby/docs/Zebra.md
Normal file
20
samples/client/petstore/ruby/docs/Zebra.md
Normal file
@ -0,0 +1,20 @@
|
||||
# Petstore::Zebra
|
||||
|
||||
## Properties
|
||||
|
||||
| Name | Type | Description | Notes |
|
||||
| ---- | ---- | ----------- | ----- |
|
||||
| **type** | **String** | | [optional] |
|
||||
| **classname** | **String** | | |
|
||||
|
||||
## Example
|
||||
|
||||
```ruby
|
||||
require 'petstore'
|
||||
|
||||
instance = Petstore::Zebra.new(
|
||||
type: null,
|
||||
classname: null
|
||||
)
|
||||
```
|
||||
|
@ -41,6 +41,8 @@ require 'petstore/models/format_test'
|
||||
require 'petstore/models/has_only_read_only'
|
||||
require 'petstore/models/health_check_result'
|
||||
require 'petstore/models/list'
|
||||
require 'petstore/models/mammal'
|
||||
require 'petstore/models/mammal_without_discriminator'
|
||||
require 'petstore/models/map_test'
|
||||
require 'petstore/models/mixed_properties_and_additional_properties_class'
|
||||
require 'petstore/models/model200_response'
|
||||
@ -62,6 +64,8 @@ require 'petstore/models/single_ref_type'
|
||||
require 'petstore/models/special_model_name'
|
||||
require 'petstore/models/tag'
|
||||
require 'petstore/models/user'
|
||||
require 'petstore/models/whale'
|
||||
require 'petstore/models/zebra'
|
||||
require 'petstore/models/cat'
|
||||
require 'petstore/models/dog'
|
||||
|
||||
|
43
samples/client/petstore/ruby/lib/petstore/models/mammal.rb
Normal file
43
samples/client/petstore/ruby/lib/petstore/models/mammal.rb
Normal file
@ -0,0 +1,43 @@
|
||||
=begin
|
||||
#OpenAPI Petstore
|
||||
|
||||
#This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\
|
||||
|
||||
The version of the OpenAPI document: 1.0.0
|
||||
|
||||
Generated by: https://openapi-generator.tech
|
||||
OpenAPI Generator version: 7.0.0-SNAPSHOT
|
||||
|
||||
=end
|
||||
|
||||
require 'date'
|
||||
require 'time'
|
||||
|
||||
module Petstore
|
||||
module Mammal
|
||||
class << self
|
||||
# List of class defined in oneOf (OpenAPI v3)
|
||||
def openapi_one_of
|
||||
[
|
||||
:'Whale',
|
||||
:'Zebra'
|
||||
]
|
||||
end
|
||||
|
||||
# Discriminator's property name (OpenAPI v3)
|
||||
def openapi_discriminator_name
|
||||
:'classname'
|
||||
end
|
||||
|
||||
# Builds the object
|
||||
# @param [Mixed] Data to be matched against the list of oneOf items
|
||||
# @return [Object] Returns the model or the data itself
|
||||
def build(data)
|
||||
discriminator_value = data[openapi_discriminator_name]
|
||||
return nil if discriminator_value.nil?
|
||||
Petstore.const_get(discriminator_value).build_from_hash(data)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
end
|
@ -0,0 +1,105 @@
|
||||
=begin
|
||||
#OpenAPI Petstore
|
||||
|
||||
#This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\
|
||||
|
||||
The version of the OpenAPI document: 1.0.0
|
||||
|
||||
Generated by: https://openapi-generator.tech
|
||||
OpenAPI Generator version: 7.0.0-SNAPSHOT
|
||||
|
||||
=end
|
||||
|
||||
require 'date'
|
||||
require 'time'
|
||||
|
||||
module Petstore
|
||||
module MammalWithoutDiscriminator
|
||||
class << self
|
||||
# List of class defined in oneOf (OpenAPI v3)
|
||||
def openapi_one_of
|
||||
[
|
||||
:'Whale',
|
||||
:'Zebra'
|
||||
]
|
||||
end
|
||||
|
||||
# Builds the object
|
||||
# @param [Mixed] Data to be matched against the list of oneOf items
|
||||
# @return [Object] Returns the model or the data itself
|
||||
def build(data)
|
||||
# Go through the list of oneOf items and attempt to identify the appropriate one.
|
||||
# Note:
|
||||
# - We do not attempt to check whether exactly one item matches.
|
||||
# - No advanced validation of types in some cases (e.g. "x: { type: string }" will happily match { x: 123 })
|
||||
# due to the way the deserialization is made in the base_object template (it just casts without verifying).
|
||||
# - TODO: scalar values are de facto behaving as if they were nullable.
|
||||
# - TODO: logging when debugging is set.
|
||||
openapi_one_of.each do |klass|
|
||||
begin
|
||||
next if klass == :AnyType # "nullable: true"
|
||||
typed_data = find_and_cast_into_type(klass, data)
|
||||
return typed_data if typed_data
|
||||
rescue # rescue all errors so we keep iterating even if the current item lookup raises
|
||||
end
|
||||
end
|
||||
|
||||
openapi_one_of.include?(:AnyType) ? data : nil
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
SchemaMismatchError = Class.new(StandardError)
|
||||
|
||||
# Note: 'File' is missing here because in the regular case we get the data _after_ a call to JSON.parse.
|
||||
def find_and_cast_into_type(klass, data)
|
||||
return if data.nil?
|
||||
|
||||
case klass.to_s
|
||||
when 'Boolean'
|
||||
return data if data.instance_of?(TrueClass) || data.instance_of?(FalseClass)
|
||||
when 'Float'
|
||||
return data if data.instance_of?(Float)
|
||||
when 'Integer'
|
||||
return data if data.instance_of?(Integer)
|
||||
when 'Time'
|
||||
return Time.parse(data)
|
||||
when 'Date'
|
||||
return Date.parse(data)
|
||||
when 'String'
|
||||
return data if data.instance_of?(String)
|
||||
when 'Object' # "type: object"
|
||||
return data if data.instance_of?(Hash)
|
||||
when /\AArray<(?<sub_type>.+)>\z/ # "type: array"
|
||||
if data.instance_of?(Array)
|
||||
sub_type = Regexp.last_match[:sub_type]
|
||||
return data.map { |item| find_and_cast_into_type(sub_type, item) }
|
||||
end
|
||||
when /\AHash<String, (?<sub_type>.+)>\z/ # "type: object" with "additionalProperties: { ... }"
|
||||
if data.instance_of?(Hash) && data.keys.all? { |k| k.instance_of?(Symbol) || k.instance_of?(String) }
|
||||
sub_type = Regexp.last_match[:sub_type]
|
||||
return data.each_with_object({}) { |(k, v), hsh| hsh[k] = find_and_cast_into_type(sub_type, v) }
|
||||
end
|
||||
else # model
|
||||
const = Petstore.const_get(klass)
|
||||
if const
|
||||
if const.respond_to?(:openapi_one_of) # nested oneOf model
|
||||
model = const.build(data)
|
||||
return model if model
|
||||
else
|
||||
# raise if data contains keys that are not known to the model
|
||||
raise unless (data.keys - const.acceptable_attributes).empty?
|
||||
model = const.build_from_hash(data)
|
||||
return model if model && model.valid?
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
raise # if no match by now, raise
|
||||
rescue
|
||||
raise SchemaMismatchError, "#{data} doesn't match the #{klass} type"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
end
|
242
samples/client/petstore/ruby/lib/petstore/models/whale.rb
Normal file
242
samples/client/petstore/ruby/lib/petstore/models/whale.rb
Normal file
@ -0,0 +1,242 @@
|
||||
=begin
|
||||
#OpenAPI Petstore
|
||||
|
||||
#This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\
|
||||
|
||||
The version of the OpenAPI document: 1.0.0
|
||||
|
||||
Generated by: https://openapi-generator.tech
|
||||
OpenAPI Generator version: 7.0.0-SNAPSHOT
|
||||
|
||||
=end
|
||||
|
||||
require 'date'
|
||||
require 'time'
|
||||
|
||||
module Petstore
|
||||
class Whale
|
||||
attr_accessor :has_baleen
|
||||
|
||||
attr_accessor :has_teeth
|
||||
|
||||
attr_accessor :classname
|
||||
|
||||
# Attribute mapping from ruby-style variable name to JSON key.
|
||||
def self.attribute_map
|
||||
{
|
||||
:'has_baleen' => :'hasBaleen',
|
||||
:'has_teeth' => :'hasTeeth',
|
||||
:'classname' => :'classname'
|
||||
}
|
||||
end
|
||||
|
||||
# Returns all the JSON keys this model knows about
|
||||
def self.acceptable_attributes
|
||||
attribute_map.values
|
||||
end
|
||||
|
||||
# Attribute type mapping.
|
||||
def self.openapi_types
|
||||
{
|
||||
:'has_baleen' => :'Boolean',
|
||||
:'has_teeth' => :'Boolean',
|
||||
:'classname' => :'String'
|
||||
}
|
||||
end
|
||||
|
||||
# List of attributes with nullable: true
|
||||
def self.openapi_nullable
|
||||
Set.new([
|
||||
])
|
||||
end
|
||||
|
||||
# Initializes the object
|
||||
# @param [Hash] attributes Model attributes in the form of hash
|
||||
def initialize(attributes = {})
|
||||
if (!attributes.is_a?(Hash))
|
||||
fail ArgumentError, "The input argument (attributes) must be a hash in `Petstore::Whale` initialize method"
|
||||
end
|
||||
|
||||
# check to see if the attribute exists and convert string to symbol for hash key
|
||||
attributes = attributes.each_with_object({}) { |(k, v), h|
|
||||
if (!self.class.attribute_map.key?(k.to_sym))
|
||||
fail ArgumentError, "`#{k}` is not a valid attribute in `Petstore::Whale`. Please check the name to make sure it's valid. List of attributes: " + self.class.attribute_map.keys.inspect
|
||||
end
|
||||
h[k.to_sym] = v
|
||||
}
|
||||
|
||||
if attributes.key?(:'has_baleen')
|
||||
self.has_baleen = attributes[:'has_baleen']
|
||||
end
|
||||
|
||||
if attributes.key?(:'has_teeth')
|
||||
self.has_teeth = attributes[:'has_teeth']
|
||||
end
|
||||
|
||||
if attributes.key?(:'classname')
|
||||
self.classname = attributes[:'classname']
|
||||
end
|
||||
end
|
||||
|
||||
# Show invalid properties with the reasons. Usually used together with valid?
|
||||
# @return Array for valid properties with the reasons
|
||||
def list_invalid_properties
|
||||
invalid_properties = Array.new
|
||||
if @classname.nil?
|
||||
invalid_properties.push('invalid value for "classname", classname cannot be nil.')
|
||||
end
|
||||
|
||||
invalid_properties
|
||||
end
|
||||
|
||||
# Check to see if the all the properties in the model are valid
|
||||
# @return true if the model is valid
|
||||
def valid?
|
||||
return false if @classname.nil?
|
||||
true
|
||||
end
|
||||
|
||||
# Checks equality by comparing each attribute.
|
||||
# @param [Object] Object to be compared
|
||||
def ==(o)
|
||||
return true if self.equal?(o)
|
||||
self.class == o.class &&
|
||||
has_baleen == o.has_baleen &&
|
||||
has_teeth == o.has_teeth &&
|
||||
classname == o.classname
|
||||
end
|
||||
|
||||
# @see the `==` method
|
||||
# @param [Object] Object to be compared
|
||||
def eql?(o)
|
||||
self == o
|
||||
end
|
||||
|
||||
# Calculates hash code according to all attributes.
|
||||
# @return [Integer] Hash code
|
||||
def hash
|
||||
[has_baleen, has_teeth, classname].hash
|
||||
end
|
||||
|
||||
# Builds the object from hash
|
||||
# @param [Hash] attributes Model attributes in the form of hash
|
||||
# @return [Object] Returns the model itself
|
||||
def self.build_from_hash(attributes)
|
||||
new.build_from_hash(attributes)
|
||||
end
|
||||
|
||||
# Builds the object from hash
|
||||
# @param [Hash] attributes Model attributes in the form of hash
|
||||
# @return [Object] Returns the model itself
|
||||
def build_from_hash(attributes)
|
||||
return nil unless attributes.is_a?(Hash)
|
||||
attributes = attributes.transform_keys(&:to_sym)
|
||||
self.class.openapi_types.each_pair do |key, type|
|
||||
if attributes[self.class.attribute_map[key]].nil? && self.class.openapi_nullable.include?(key)
|
||||
self.send("#{key}=", nil)
|
||||
elsif type =~ /\AArray<(.*)>/i
|
||||
# check to ensure the input is an array given that the attribute
|
||||
# is documented as an array but the input is not
|
||||
if attributes[self.class.attribute_map[key]].is_a?(Array)
|
||||
self.send("#{key}=", attributes[self.class.attribute_map[key]].map { |v| _deserialize($1, v) })
|
||||
end
|
||||
elsif !attributes[self.class.attribute_map[key]].nil?
|
||||
self.send("#{key}=", _deserialize(type, attributes[self.class.attribute_map[key]]))
|
||||
end
|
||||
end
|
||||
|
||||
self
|
||||
end
|
||||
|
||||
# Deserializes the data based on type
|
||||
# @param string type Data type
|
||||
# @param string value Value to be deserialized
|
||||
# @return [Object] Deserialized data
|
||||
def _deserialize(type, value)
|
||||
case type.to_sym
|
||||
when :Time
|
||||
Time.parse(value)
|
||||
when :Date
|
||||
Date.parse(value)
|
||||
when :String
|
||||
value.to_s
|
||||
when :Integer
|
||||
value.to_i
|
||||
when :Float
|
||||
value.to_f
|
||||
when :Boolean
|
||||
if value.to_s =~ /\A(true|t|yes|y|1)\z/i
|
||||
true
|
||||
else
|
||||
false
|
||||
end
|
||||
when :Object
|
||||
# generic object (usually a Hash), return directly
|
||||
value
|
||||
when /\AArray<(?<inner_type>.+)>\z/
|
||||
inner_type = Regexp.last_match[:inner_type]
|
||||
value.map { |v| _deserialize(inner_type, v) }
|
||||
when /\AHash<(?<k_type>.+?), (?<v_type>.+)>\z/
|
||||
k_type = Regexp.last_match[:k_type]
|
||||
v_type = Regexp.last_match[:v_type]
|
||||
{}.tap do |hash|
|
||||
value.each do |k, v|
|
||||
hash[_deserialize(k_type, k)] = _deserialize(v_type, v)
|
||||
end
|
||||
end
|
||||
else # model
|
||||
# models (e.g. Pet) or oneOf
|
||||
klass = Petstore.const_get(type)
|
||||
klass.respond_to?(:openapi_one_of) ? klass.build(value) : klass.build_from_hash(value)
|
||||
end
|
||||
end
|
||||
|
||||
# Returns the string representation of the object
|
||||
# @return [String] String presentation of the object
|
||||
def to_s
|
||||
to_hash.to_s
|
||||
end
|
||||
|
||||
# to_body is an alias to to_hash (backward compatibility)
|
||||
# @return [Hash] Returns the object in the form of hash
|
||||
def to_body
|
||||
to_hash
|
||||
end
|
||||
|
||||
# Returns the object in the form of hash
|
||||
# @return [Hash] Returns the object in the form of hash
|
||||
def to_hash
|
||||
hash = {}
|
||||
self.class.attribute_map.each_pair do |attr, param|
|
||||
value = self.send(attr)
|
||||
if value.nil?
|
||||
is_nullable = self.class.openapi_nullable.include?(attr)
|
||||
next if !is_nullable || (is_nullable && !instance_variable_defined?(:"@#{attr}"))
|
||||
end
|
||||
|
||||
hash[param] = _to_hash(value)
|
||||
end
|
||||
hash
|
||||
end
|
||||
|
||||
# Outputs non-array value in the form of hash
|
||||
# For object, use to_hash. Otherwise, just return the value
|
||||
# @param [Object] value Any valid value
|
||||
# @return [Hash] Returns the value in the form of hash
|
||||
def _to_hash(value)
|
||||
if value.is_a?(Array)
|
||||
value.compact.map { |v| _to_hash(v) }
|
||||
elsif value.is_a?(Hash)
|
||||
{}.tap do |hash|
|
||||
value.each { |k, v| hash[k] = _to_hash(v) }
|
||||
end
|
||||
elsif value.respond_to? :to_hash
|
||||
value.to_hash
|
||||
else
|
||||
value
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
267
samples/client/petstore/ruby/lib/petstore/models/zebra.rb
Normal file
267
samples/client/petstore/ruby/lib/petstore/models/zebra.rb
Normal file
@ -0,0 +1,267 @@
|
||||
=begin
|
||||
#OpenAPI Petstore
|
||||
|
||||
#This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\
|
||||
|
||||
The version of the OpenAPI document: 1.0.0
|
||||
|
||||
Generated by: https://openapi-generator.tech
|
||||
OpenAPI Generator version: 7.0.0-SNAPSHOT
|
||||
|
||||
=end
|
||||
|
||||
require 'date'
|
||||
require 'time'
|
||||
|
||||
module Petstore
|
||||
class Zebra
|
||||
attr_accessor :type
|
||||
|
||||
attr_accessor :classname
|
||||
|
||||
class EnumAttributeValidator
|
||||
attr_reader :datatype
|
||||
attr_reader :allowable_values
|
||||
|
||||
def initialize(datatype, allowable_values)
|
||||
@allowable_values = allowable_values.map do |value|
|
||||
case datatype.to_s
|
||||
when /Integer/i
|
||||
value.to_i
|
||||
when /Float/i
|
||||
value.to_f
|
||||
else
|
||||
value
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def valid?(value)
|
||||
!value || allowable_values.include?(value)
|
||||
end
|
||||
end
|
||||
|
||||
# Attribute mapping from ruby-style variable name to JSON key.
|
||||
def self.attribute_map
|
||||
{
|
||||
:'type' => :'type',
|
||||
:'classname' => :'classname'
|
||||
}
|
||||
end
|
||||
|
||||
# Returns all the JSON keys this model knows about
|
||||
def self.acceptable_attributes
|
||||
attribute_map.values
|
||||
end
|
||||
|
||||
# Attribute type mapping.
|
||||
def self.openapi_types
|
||||
{
|
||||
:'type' => :'String',
|
||||
:'classname' => :'String'
|
||||
}
|
||||
end
|
||||
|
||||
# List of attributes with nullable: true
|
||||
def self.openapi_nullable
|
||||
Set.new([
|
||||
])
|
||||
end
|
||||
|
||||
# Initializes the object
|
||||
# @param [Hash] attributes Model attributes in the form of hash
|
||||
def initialize(attributes = {})
|
||||
if (!attributes.is_a?(Hash))
|
||||
fail ArgumentError, "The input argument (attributes) must be a hash in `Petstore::Zebra` initialize method"
|
||||
end
|
||||
|
||||
# check to see if the attribute exists and convert string to symbol for hash key
|
||||
attributes = attributes.each_with_object({}) { |(k, v), h|
|
||||
if (!self.class.attribute_map.key?(k.to_sym))
|
||||
fail ArgumentError, "`#{k}` is not a valid attribute in `Petstore::Zebra`. Please check the name to make sure it's valid. List of attributes: " + self.class.attribute_map.keys.inspect
|
||||
end
|
||||
h[k.to_sym] = v
|
||||
}
|
||||
|
||||
if attributes.key?(:'type')
|
||||
self.type = attributes[:'type']
|
||||
end
|
||||
|
||||
if attributes.key?(:'classname')
|
||||
self.classname = attributes[:'classname']
|
||||
end
|
||||
end
|
||||
|
||||
# Show invalid properties with the reasons. Usually used together with valid?
|
||||
# @return Array for valid properties with the reasons
|
||||
def list_invalid_properties
|
||||
invalid_properties = Array.new
|
||||
if @classname.nil?
|
||||
invalid_properties.push('invalid value for "classname", classname cannot be nil.')
|
||||
end
|
||||
|
||||
invalid_properties
|
||||
end
|
||||
|
||||
# Check to see if the all the properties in the model are valid
|
||||
# @return true if the model is valid
|
||||
def valid?
|
||||
type_validator = EnumAttributeValidator.new('String', ["plains", "mountain", "grevys"])
|
||||
return false unless type_validator.valid?(@type)
|
||||
return false if @classname.nil?
|
||||
true
|
||||
end
|
||||
|
||||
# Custom attribute writer method checking allowed values (enum).
|
||||
# @param [Object] type Object to be assigned
|
||||
def type=(type)
|
||||
validator = EnumAttributeValidator.new('String', ["plains", "mountain", "grevys"])
|
||||
unless validator.valid?(type)
|
||||
fail ArgumentError, "invalid value for \"type\", must be one of #{validator.allowable_values}."
|
||||
end
|
||||
@type = type
|
||||
end
|
||||
|
||||
# Checks equality by comparing each attribute.
|
||||
# @param [Object] Object to be compared
|
||||
def ==(o)
|
||||
return true if self.equal?(o)
|
||||
self.class == o.class &&
|
||||
type == o.type &&
|
||||
classname == o.classname
|
||||
end
|
||||
|
||||
# @see the `==` method
|
||||
# @param [Object] Object to be compared
|
||||
def eql?(o)
|
||||
self == o
|
||||
end
|
||||
|
||||
# Calculates hash code according to all attributes.
|
||||
# @return [Integer] Hash code
|
||||
def hash
|
||||
[type, classname].hash
|
||||
end
|
||||
|
||||
# Builds the object from hash
|
||||
# @param [Hash] attributes Model attributes in the form of hash
|
||||
# @return [Object] Returns the model itself
|
||||
def self.build_from_hash(attributes)
|
||||
new.build_from_hash(attributes)
|
||||
end
|
||||
|
||||
# Builds the object from hash
|
||||
# @param [Hash] attributes Model attributes in the form of hash
|
||||
# @return [Object] Returns the model itself
|
||||
def build_from_hash(attributes)
|
||||
return nil unless attributes.is_a?(Hash)
|
||||
attributes = attributes.transform_keys(&:to_sym)
|
||||
self.class.openapi_types.each_pair do |key, type|
|
||||
if attributes[self.class.attribute_map[key]].nil? && self.class.openapi_nullable.include?(key)
|
||||
self.send("#{key}=", nil)
|
||||
elsif type =~ /\AArray<(.*)>/i
|
||||
# check to ensure the input is an array given that the attribute
|
||||
# is documented as an array but the input is not
|
||||
if attributes[self.class.attribute_map[key]].is_a?(Array)
|
||||
self.send("#{key}=", attributes[self.class.attribute_map[key]].map { |v| _deserialize($1, v) })
|
||||
end
|
||||
elsif !attributes[self.class.attribute_map[key]].nil?
|
||||
self.send("#{key}=", _deserialize(type, attributes[self.class.attribute_map[key]]))
|
||||
end
|
||||
end
|
||||
|
||||
self
|
||||
end
|
||||
|
||||
# Deserializes the data based on type
|
||||
# @param string type Data type
|
||||
# @param string value Value to be deserialized
|
||||
# @return [Object] Deserialized data
|
||||
def _deserialize(type, value)
|
||||
case type.to_sym
|
||||
when :Time
|
||||
Time.parse(value)
|
||||
when :Date
|
||||
Date.parse(value)
|
||||
when :String
|
||||
value.to_s
|
||||
when :Integer
|
||||
value.to_i
|
||||
when :Float
|
||||
value.to_f
|
||||
when :Boolean
|
||||
if value.to_s =~ /\A(true|t|yes|y|1)\z/i
|
||||
true
|
||||
else
|
||||
false
|
||||
end
|
||||
when :Object
|
||||
# generic object (usually a Hash), return directly
|
||||
value
|
||||
when /\AArray<(?<inner_type>.+)>\z/
|
||||
inner_type = Regexp.last_match[:inner_type]
|
||||
value.map { |v| _deserialize(inner_type, v) }
|
||||
when /\AHash<(?<k_type>.+?), (?<v_type>.+)>\z/
|
||||
k_type = Regexp.last_match[:k_type]
|
||||
v_type = Regexp.last_match[:v_type]
|
||||
{}.tap do |hash|
|
||||
value.each do |k, v|
|
||||
hash[_deserialize(k_type, k)] = _deserialize(v_type, v)
|
||||
end
|
||||
end
|
||||
else # model
|
||||
# models (e.g. Pet) or oneOf
|
||||
klass = Petstore.const_get(type)
|
||||
klass.respond_to?(:openapi_one_of) ? klass.build(value) : klass.build_from_hash(value)
|
||||
end
|
||||
end
|
||||
|
||||
# Returns the string representation of the object
|
||||
# @return [String] String presentation of the object
|
||||
def to_s
|
||||
to_hash.to_s
|
||||
end
|
||||
|
||||
# to_body is an alias to to_hash (backward compatibility)
|
||||
# @return [Hash] Returns the object in the form of hash
|
||||
def to_body
|
||||
to_hash
|
||||
end
|
||||
|
||||
# Returns the object in the form of hash
|
||||
# @return [Hash] Returns the object in the form of hash
|
||||
def to_hash
|
||||
hash = {}
|
||||
self.class.attribute_map.each_pair do |attr, param|
|
||||
value = self.send(attr)
|
||||
if value.nil?
|
||||
is_nullable = self.class.openapi_nullable.include?(attr)
|
||||
next if !is_nullable || (is_nullable && !instance_variable_defined?(:"@#{attr}"))
|
||||
end
|
||||
|
||||
hash[param] = _to_hash(value)
|
||||
end
|
||||
hash
|
||||
end
|
||||
|
||||
# Outputs non-array value in the form of hash
|
||||
# For object, use to_hash. Otherwise, just return the value
|
||||
# @param [Object] value Any valid value
|
||||
# @return [Hash] Returns the value in the form of hash
|
||||
def _to_hash(value)
|
||||
if value.is_a?(Array)
|
||||
value.compact.map { |v| _to_hash(v) }
|
||||
elsif value.is_a?(Hash)
|
||||
{}.tap do |hash|
|
||||
value.each { |k, v| hash[k] = _to_hash(v) }
|
||||
end
|
||||
elsif value.respond_to? :to_hash
|
||||
value.to_hash
|
||||
else
|
||||
value
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
52
samples/client/petstore/ruby/spec/custom/model_spec.rb
Normal file
52
samples/client/petstore/ruby/spec/custom/model_spec.rb
Normal file
@ -0,0 +1,52 @@
|
||||
require 'petstore_helper'
|
||||
require 'spec_helper'
|
||||
require 'json'
|
||||
|
||||
describe "Mammal" do
|
||||
before do
|
||||
end
|
||||
|
||||
after do
|
||||
end
|
||||
|
||||
describe "oneOf" do
|
||||
it "should construct a new oneOf object mammal" do
|
||||
whale = Petstore::Whale.new('classname' => "Whale", 'has_teeth' => true)
|
||||
zebra = Petstore::Zebra.new('classname' => "Zebra", 'type' => 'plains')
|
||||
|
||||
# oneOf whale test
|
||||
expect(whale.to_hash[:'classname']).to eq("Whale")
|
||||
result = Petstore::Mammal.build(whale.to_hash)
|
||||
expect(result).to be_a Petstore::Whale
|
||||
|
||||
# oneOf zebra test
|
||||
expect(zebra.to_hash[:'classname']).to eq("Zebra")
|
||||
result2 = Petstore::Mammal.build(zebra.to_hash)
|
||||
expect(result2).to be_a Petstore::Zebra
|
||||
|
||||
# invalid data/hash should result in nil
|
||||
result3 = Petstore::Mammal.build({"something": 123})
|
||||
expect(result3).to be_nil
|
||||
end
|
||||
|
||||
it "should construct a new oneOf object mammal_without_discriminator" do
|
||||
whale = Petstore::Whale.new('classname' => "Whale", 'has_teeth' => true)
|
||||
zebra = Petstore::Zebra.new('classname' => "Zebra", 'type' => 'plains')
|
||||
|
||||
# oneOf whale test
|
||||
expect(whale.to_hash[:'classname']).to eq("Whale")
|
||||
result = Petstore::MammalWithoutDiscriminator.build(whale.to_hash)
|
||||
expect(result).to be_a Petstore::Whale
|
||||
|
||||
# oneOf zebra test
|
||||
expect(zebra.to_hash[:'classname']).to eq("Zebra")
|
||||
result2 = Petstore::MammalWithoutDiscriminator.build(zebra.to_hash)
|
||||
expect(result2).to be_a Petstore::Zebra
|
||||
|
||||
# invalid data/hash should result in nil
|
||||
result3 = Petstore::MammalWithoutDiscriminator.build({"something": 123})
|
||||
expect(result3).to be_nil
|
||||
end
|
||||
|
||||
end
|
||||
end
|
37
samples/client/petstore/ruby/spec/models/mammal_spec.rb
Normal file
37
samples/client/petstore/ruby/spec/models/mammal_spec.rb
Normal file
@ -0,0 +1,37 @@
|
||||
=begin
|
||||
#OpenAPI Petstore
|
||||
|
||||
#This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\
|
||||
|
||||
The version of the OpenAPI document: 1.0.0
|
||||
|
||||
Generated by: https://openapi-generator.tech
|
||||
OpenAPI Generator version: 7.0.0-SNAPSHOT
|
||||
|
||||
=end
|
||||
|
||||
require 'spec_helper'
|
||||
require 'json'
|
||||
require 'date'
|
||||
|
||||
# Unit tests for Petstore::Mammal
|
||||
# Automatically generated by openapi-generator (https://openapi-generator.tech)
|
||||
# Please update as you see appropriate
|
||||
describe Petstore::Mammal do
|
||||
describe '.openapi_one_of' do
|
||||
it 'lists the items referenced in the oneOf array' do
|
||||
expect(described_class.openapi_one_of).to_not be_empty
|
||||
end
|
||||
end
|
||||
|
||||
describe '.openapi_discriminator_name' do
|
||||
it 'returns the value of the "discriminator" property' do
|
||||
expect(described_class.openapi_discriminator_name).to_not be_empty
|
||||
end
|
||||
end
|
||||
|
||||
describe '.build' do
|
||||
it 'returns the correct model' do
|
||||
end
|
||||
end
|
||||
end
|
@ -0,0 +1,31 @@
|
||||
=begin
|
||||
#OpenAPI Petstore
|
||||
|
||||
#This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\
|
||||
|
||||
The version of the OpenAPI document: 1.0.0
|
||||
|
||||
Generated by: https://openapi-generator.tech
|
||||
OpenAPI Generator version: 7.0.0-SNAPSHOT
|
||||
|
||||
=end
|
||||
|
||||
require 'spec_helper'
|
||||
require 'json'
|
||||
require 'date'
|
||||
|
||||
# Unit tests for Petstore::MammalWithoutDiscriminator
|
||||
# Automatically generated by openapi-generator (https://openapi-generator.tech)
|
||||
# Please update as you see appropriate
|
||||
describe Petstore::MammalWithoutDiscriminator do
|
||||
describe '.openapi_one_of' do
|
||||
it 'lists the items referenced in the oneOf array' do
|
||||
expect(described_class.openapi_one_of).to_not be_empty
|
||||
end
|
||||
end
|
||||
|
||||
describe '.build' do
|
||||
it 'returns the correct model' do
|
||||
end
|
||||
end
|
||||
end
|
46
samples/client/petstore/ruby/spec/models/whale_spec.rb
Normal file
46
samples/client/petstore/ruby/spec/models/whale_spec.rb
Normal file
@ -0,0 +1,46 @@
|
||||
=begin
|
||||
#OpenAPI Petstore
|
||||
|
||||
#This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\
|
||||
|
||||
The version of the OpenAPI document: 1.0.0
|
||||
|
||||
Generated by: https://openapi-generator.tech
|
||||
OpenAPI Generator version: 7.0.0-SNAPSHOT
|
||||
|
||||
=end
|
||||
|
||||
require 'spec_helper'
|
||||
require 'json'
|
||||
require 'date'
|
||||
|
||||
# Unit tests for Petstore::Whale
|
||||
# Automatically generated by openapi-generator (https://openapi-generator.tech)
|
||||
# Please update as you see appropriate
|
||||
describe Petstore::Whale do
|
||||
let(:instance) { Petstore::Whale.new }
|
||||
|
||||
describe 'test an instance of Whale' do
|
||||
it 'should create an instance of Whale' do
|
||||
expect(instance).to be_instance_of(Petstore::Whale)
|
||||
end
|
||||
end
|
||||
describe 'test attribute "has_baleen"' do
|
||||
it 'should work' do
|
||||
# assertion here. ref: https://rspec.info/features/3-12/rspec-expectations/built-in-matchers/
|
||||
end
|
||||
end
|
||||
|
||||
describe 'test attribute "has_teeth"' do
|
||||
it 'should work' do
|
||||
# assertion here. ref: https://rspec.info/features/3-12/rspec-expectations/built-in-matchers/
|
||||
end
|
||||
end
|
||||
|
||||
describe 'test attribute "class_name"' do
|
||||
it 'should work' do
|
||||
# assertion here. ref: https://rspec.info/features/3-12/rspec-expectations/built-in-matchers/
|
||||
end
|
||||
end
|
||||
|
||||
end
|
44
samples/client/petstore/ruby/spec/models/zebra_spec.rb
Normal file
44
samples/client/petstore/ruby/spec/models/zebra_spec.rb
Normal file
@ -0,0 +1,44 @@
|
||||
=begin
|
||||
#OpenAPI Petstore
|
||||
|
||||
#This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\
|
||||
|
||||
The version of the OpenAPI document: 1.0.0
|
||||
|
||||
Generated by: https://openapi-generator.tech
|
||||
OpenAPI Generator version: 7.0.0-SNAPSHOT
|
||||
|
||||
=end
|
||||
|
||||
require 'spec_helper'
|
||||
require 'json'
|
||||
require 'date'
|
||||
|
||||
# Unit tests for Petstore::Zebra
|
||||
# Automatically generated by openapi-generator (https://openapi-generator.tech)
|
||||
# Please update as you see appropriate
|
||||
describe Petstore::Zebra do
|
||||
let(:instance) { Petstore::Zebra.new }
|
||||
|
||||
describe 'test an instance of Zebra' do
|
||||
it 'should create an instance of Zebra' do
|
||||
expect(instance).to be_instance_of(Petstore::Zebra)
|
||||
end
|
||||
end
|
||||
describe 'test attribute "type"' do
|
||||
it 'should work' do
|
||||
# assertion here. ref: https://rspec.info/features/3-12/rspec-expectations/built-in-matchers/
|
||||
# validator = Petstore::EnumTest::EnumAttributeValidator.new('String', ["plains", "mountain", "grevys"])
|
||||
# validator.allowable_values.each do |value|
|
||||
# expect { instance.type = value }.not_to raise_error
|
||||
# end
|
||||
end
|
||||
end
|
||||
|
||||
describe 'test attribute "class_name"' do
|
||||
it 'should work' do
|
||||
# assertion here. ref: https://rspec.info/features/3-12/rspec-expectations/built-in-matchers/
|
||||
end
|
||||
end
|
||||
|
||||
end
|
Loading…
x
Reference in New Issue
Block a user