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 @@ -29,8 +29,7 @@
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
return find_and_cast_into_type(klass, data)
rescue # rescue all errors so we keep iterating even if the current item lookup raises
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,7 @@
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
return find_and_cast_into_type(klass, data)
rescue # rescue all errors so we keep iterating even if the current item lookup raises
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2162,3 +2162,33 @@ components:
type: string
type_:
type: string
OneOfPrimitiveTypes:
oneOf:
- type: string
- type: number
- type: integer
- type: boolean
- type: string
format: date
- type: string
format: date-time
- type: array
items:
type: string
- type: array
items:
type: number
- type: array
items:
type: integer
- type: array
items:
type: boolean
- type: array
items:
type: string
format: date
- type: array
items:
type: string
format: date-time
2 changes: 2 additions & 0 deletions samples/client/petstore/ruby-httpx/.openapi-generator/FILES
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ docs/Name.md
docs/NullableClass.md
docs/NumberOnly.md
docs/ObjectWithDeprecatedFields.md
docs/OneOfPrimitiveTypes.md
docs/Order.md
docs/OuterComposite.md
docs/OuterEnum.md
Expand Down Expand Up @@ -118,6 +119,7 @@ lib/petstore/models/name.rb
lib/petstore/models/nullable_class.rb
lib/petstore/models/number_only.rb
lib/petstore/models/object_with_deprecated_fields.rb
lib/petstore/models/one_of_primitive_types.rb
lib/petstore/models/order.rb
lib/petstore/models/outer_composite.rb
lib/petstore/models/outer_enum.rb
Expand Down
1 change: 1 addition & 0 deletions samples/client/petstore/ruby-httpx/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ Class | Method | HTTP request | Description
- [Petstore::NullableClass](docs/NullableClass.md)
- [Petstore::NumberOnly](docs/NumberOnly.md)
- [Petstore::ObjectWithDeprecatedFields](docs/ObjectWithDeprecatedFields.md)
- [Petstore::OneOfPrimitiveTypes](docs/OneOfPrimitiveTypes.md)
- [Petstore::Order](docs/Order.md)
- [Petstore::OuterComposite](docs/OuterComposite.md)
- [Petstore::OuterEnum](docs/OuterEnum.md)
Expand Down
69 changes: 69 additions & 0 deletions samples/client/petstore/ruby-httpx/docs/OneOfPrimitiveTypes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# Petstore::OneOfPrimitiveTypes

## Class instance methods

### `openapi_one_of`

Returns the list of classes defined in oneOf.

#### Example

```ruby
require 'petstore'

Petstore::OneOfPrimitiveTypes.openapi_one_of
# =>
# [
# :'Array<Boolean>',
# :'Array<Date>',
# :'Array<Float>',
# :'Array<Integer>',
# :'Array<String>',
# :'Array<Time>',
# :'Boolean',
# :'Date',
# :'Float',
# :'Integer',
# :'String',
# :'Time'
# ]
```

### build

Find the appropriate object from the `openapi_one_of` list and casts the data into it.

#### Example

```ruby
require 'petstore'

Petstore::OneOfPrimitiveTypes.build(data)
# => #<Array<Boolean>:0x00007fdd4aab02a0>

Petstore::OneOfPrimitiveTypes.build(data_that_doesnt_match)
# => nil
```

#### Parameters

| Name | Type | Description |
| ---- | ---- | ----------- |
| **data** | **Mixed** | data to be matched against the list of oneOf items |

#### Return type

- `Array<Boolean>`
- `Array<Date>`
- `Array<Float>`
- `Array<Integer>`
- `Array<String>`
- `Array<Time>`
- `Boolean`
- `Date`
- `Float`
- `Integer`
- `String`
- `Time`
- `nil` (if no type matches)

1 change: 1 addition & 0 deletions samples/client/petstore/ruby-httpx/lib/petstore.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
require 'petstore/models/nullable_class'
require 'petstore/models/number_only'
require 'petstore/models/object_with_deprecated_fields'
require 'petstore/models/one_of_primitive_types'
require 'petstore/models/order'
require 'petstore/models/outer_composite'
require 'petstore/models/outer_enum'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ def build(data)
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
return find_and_cast_into_type(klass, data)
rescue # rescue all errors so we keep iterating even if the current item lookup raises
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ def build(data)
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
return find_and_cast_into_type(klass, data)
rescue # rescue all errors so we keep iterating even if the current item lookup raises
end
end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
=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
Generator version: 7.16.0-SNAPSHOT

=end

require 'date'
require 'time'

module Petstore
module OneOfPrimitiveTypes
class << self
# List of class defined in oneOf (OpenAPI v3)
def openapi_one_of
[
:'Array<Boolean>',
:'Array<Date>',
:'Array<Float>',
:'Array<Integer>',
:'Array<String>',
:'Array<Time>',
:'Boolean',
:'Date',
:'Float',
:'Integer',
:'String',
:'Time'
]
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"
return find_and_cast_into_type(klass, 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.iso8601(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 if const.respond_to?(:acceptable_attributes) && !(data.keys - const.acceptable_attributes).empty?
model = const.build_from_hash(data)
return model if model
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
@@ -0,0 +1,32 @@
=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
Generator version: 7.16.0-SNAPSHOT

=end

require 'spec_helper'
require 'json'
require 'date'

# Unit tests for Petstore::OneOfPrimitiveTypes
# Automatically generated by openapi-generator (https://openapi-generator.tech)
# Please update as you see appropriate
describe Petstore::OneOfPrimitiveTypes 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
# assertion here. ref: https://rspec.info/features/3-12/rspec-expectations/built-in-matchers/
end
end
end
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 @@ -48,6 +48,7 @@ docs/Name.md
docs/NullableClass.md
docs/NumberOnly.md
docs/ObjectWithDeprecatedFields.md
docs/OneOfPrimitiveTypes.md
docs/Order.md
docs/OuterComposite.md
docs/OuterEnum.md
Expand Down Expand Up @@ -118,6 +119,7 @@ lib/petstore/models/name.rb
lib/petstore/models/nullable_class.rb
lib/petstore/models/number_only.rb
lib/petstore/models/object_with_deprecated_fields.rb
lib/petstore/models/one_of_primitive_types.rb
lib/petstore/models/order.rb
lib/petstore/models/outer_composite.rb
lib/petstore/models/outer_enum.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 @@ -164,6 +164,7 @@ Class | Method | HTTP request | Description
- [Petstore::NullableClass](docs/NullableClass.md)
- [Petstore::NumberOnly](docs/NumberOnly.md)
- [Petstore::ObjectWithDeprecatedFields](docs/ObjectWithDeprecatedFields.md)
- [Petstore::OneOfPrimitiveTypes](docs/OneOfPrimitiveTypes.md)
- [Petstore::Order](docs/Order.md)
- [Petstore::OuterComposite](docs/OuterComposite.md)
- [Petstore::OuterEnum](docs/OuterEnum.md)
Expand Down
Loading
Loading