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 number Diff line number Diff line change
Expand Up @@ -17,8 +17,15 @@ module {{moduleName}}
{{>partial_oneof_module}}
{{/-first}}
{{/oneOf}}
{{#anyOf}}
{{#-first}}
{{>partial_anyof_module}}
{{/-first}}
{{/anyOf}}
{{^oneOf}}
{{^anyOf}}
{{>partial_model_generic}}
{{/anyOf}}
{{/oneOf}}
{{/isEnum}}
{{/model}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ require 'date'
{{#model}}
describe {{moduleName}}::{{classname}} do
{{^oneOf}}
{{^anyOf}}
let(:instance) { {{moduleName}}::{{classname}}.new }

describe 'test an instance of {{classname}}' do
Expand All @@ -37,6 +38,7 @@ describe {{moduleName}}::{{classname}} do
end

{{/vars}}
{{/anyOf}}
{{/oneOf}}
{{#oneOf}}
{{#-first}}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
{{#description}}
# {{{.}}}
{{/description}}
module {{classname}}
class << self
{{#anyOf}}
{{#-first}}
# List of class defined in anyOf (OpenAPI v3)
def openapi_any_of
[
{{/-first}}
:'{{{.}}}'{{^-last}},{{/-last}}
{{#-last}}
]
end

{{/-last}}
{{/anyOf}}
# Builds the object
# @param [Mixed] Data to be matched against the list of anyOf items
# @return [Object] Returns the model or the data itself
def build(data)
# Go through the list of anyOf items and attempt to identify the appropriate one.
# Note:
# - 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_any_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_any_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 = {{moduleName}}.const_get(klass)
if const
if const.respond_to?(:openapi_any_of) # nested anyOf 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
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@
# 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?
return model if model
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1961,6 +1961,10 @@ components:
enum:
- admin
- user
mammal_anyof:
anyOf:
- $ref: '#/components/schemas/whale'
- $ref: '#/components/schemas/zebra'
mammal_without_discriminator:
oneOf:
- $ref: '#/components/schemas/whale'
Expand Down
2 changes: 2 additions & 0 deletions samples/client/petstore/ruby/.openapi-generator/FILES
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ docs/HasOnlyReadOnly.md
docs/HealthCheckResult.md
docs/List.md
docs/Mammal.md
docs/MammalAnyof.md
docs/MammalWithoutDiscriminator.md
docs/MapTest.md
docs/MixedPropertiesAndAdditionalPropertiesClass.md
Expand Down Expand Up @@ -103,6 +104,7 @@ 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_anyof.rb
lib/petstore/models/mammal_without_discriminator.rb
lib/petstore/models/map_test.rb
lib/petstore/models/mixed_properties_and_additional_properties_class.rb
Expand Down
1 change: 1 addition & 0 deletions samples/client/petstore/ruby/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ Class | Method | HTTP request | Description
- [Petstore::HealthCheckResult](docs/HealthCheckResult.md)
- [Petstore::List](docs/List.md)
- [Petstore::Mammal](docs/Mammal.md)
- [Petstore::MammalAnyof](docs/MammalAnyof.md)
- [Petstore::MammalWithoutDiscriminator](docs/MammalWithoutDiscriminator.md)
- [Petstore::MapTest](docs/MapTest.md)
- [Petstore::MixedPropertiesAndAdditionalPropertiesClass](docs/MixedPropertiesAndAdditionalPropertiesClass.md)
Expand Down
24 changes: 24 additions & 0 deletions samples/client/petstore/ruby/docs/MammalAnyof.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Petstore::MammalAnyof

## Properties

| Name | Type | Description | Notes |
| ---- | ---- | ----------- | ----- |
| **has_baleen** | **Boolean** | | [optional] |
| **has_teeth** | **Boolean** | | [optional] |
| **classname** | **String** | | |
| **type** | **String** | | [optional] |

## Example

```ruby
require 'petstore'

instance = Petstore::MammalAnyof.new(
has_baleen: null,
has_teeth: null,
classname: null,
type: null
)
```

1 change: 1 addition & 0 deletions samples/client/petstore/ruby/lib/petstore.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
require 'petstore/models/health_check_result'
require 'petstore/models/list'
require 'petstore/models/mammal'
require 'petstore/models/mammal_anyof'
require 'petstore/models/mammal_without_discriminator'
require 'petstore/models/map_test'
require 'petstore/models/mixed_properties_and_additional_properties_class'
Expand Down
104 changes: 104 additions & 0 deletions samples/client/petstore/ruby/lib/petstore/models/mammal_anyof.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
=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 MammalAnyof
class << self
# List of class defined in anyOf (OpenAPI v3)
def openapi_any_of
[
:'Whale',
:'Zebra'
]
end

# Builds the object
# @param [Mixed] Data to be matched against the list of anyOf items
# @return [Object] Returns the model or the data itself
def build(data)
# Go through the list of anyOf items and attempt to identify the appropriate one.
# Note:
# - 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_any_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_any_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_any_of) # nested anyOf 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
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def find_and_cast_into_type(klass, data)
# 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?
return model if model
end
end
end
Expand Down
31 changes: 14 additions & 17 deletions samples/client/petstore/ruby/lib/petstore/models/whale.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,15 @@ def initialize(attributes = {})

if attributes.key?(:'classname')
self.classname = attributes[:'classname']
else
self.classname = nil
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
warn '[DEPRECATED] the `list_invalid_properties` method is obsolete'
invalid_properties = Array.new
if @classname.nil?
invalid_properties.push('invalid value for "classname", classname cannot be nil.')
Expand All @@ -92,6 +95,7 @@ def list_invalid_properties
# Check to see if the all the properties in the model are valid
# @return true if the model is valid
def valid?
warn '[DEPRECATED] the `valid?` method is obsolete'
return false if @classname.nil?
true
end
Expand Down Expand Up @@ -122,37 +126,30 @@ def 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)
transformed_hash = {}
openapi_types.each_pair do |key, type|
if attributes.key?(attribute_map[key]) && attributes[attribute_map[key]].nil?
transformed_hash["#{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) })
if attributes[attribute_map[key]].is_a?(Array)
transformed_hash["#{key}"] = attributes[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]]))
elsif !attributes[attribute_map[key]].nil?
transformed_hash["#{key}"] = _deserialize(type, attributes[attribute_map[key]])
end
end

self
new(transformed_hash)
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)
def self._deserialize(type, value)
case type.to_sym
when :Time
Time.parse(value)
Expand Down
Loading