Skip to content

[ruby] fix oneOf handling #5706

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 23, 2020
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 @@ -149,8 +149,9 @@ module {{moduleName}}
data.each { |k, v| hash[k] = convert_to_type(v, sub_type) }
end
else
# models, e.g. Pet
{{moduleName}}.const_get(return_type).build_from_hash(data)
# models (e.g. Pet) or oneOf
klass = {{moduleName}}.const_get(return_type)
klass.respond_to?(:openapi_one_of) ? klass.build(data) : klass.build_from_hash(data)
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@

All URIs are relative to *{{basePath}}*

Method | HTTP request | Description
------------- | ------------- | -------------
{{#operations}}{{#operation}}[**{{operationId}}**]({{classname}}.md#{{operationId}}) | **{{httpMethod}}** {{path}} | {{#summary}}{{summary}}{{/summary}}
{{/operation}}{{/operations}}
| Method | HTTP request | Description |
| ------ | ------------ | ----------- |
{{#operations}}
{{#operation}}
| [**{{operationId}}**]({{classname}}.md#{{operationId}}) | **{{httpMethod}}** {{path}} | {{#summary}}{{summary}}{{/summary}} |
{{/operation}}
{{/operations}}

{{#operations}}
{{#operation}}
Expand Down Expand Up @@ -67,10 +70,15 @@ end

### Parameters

{{^allParams}}This endpoint does not need any parameter.{{/allParams}}{{#allParams}}{{#-last}}
Name | Type | Description | Notes
------------- | ------------- | ------------- | -------------{{/-last}}{{/allParams}}
{{#allParams}} **{{paramName}}** | {{#isPrimitiveType}}**{{dataType}}**{{/isPrimitiveType}}{{^isPrimitiveType}}{{#isFile}}**{{dataType}}**{{/isFile}}{{^isFile}}[**{{dataType}}**]({{baseType}}.md){{/isFile}}{{/isPrimitiveType}}| {{description}} | {{^required}}[optional] {{/required}}{{#defaultValue}}[default to {{defaultValue}}]{{/defaultValue}}
{{^allParams}}
This endpoint does not need any parameter.
{{/allParams}}
{{#allParams}}
{{#-first}}
| Name | Type | Description | Notes |
| ---- | ---- | ----------- | ----- |
{{/-first}}
| **{{paramName}}** | {{#isPrimitiveType}}**{{dataType}}**{{/isPrimitiveType}}{{^isPrimitiveType}}{{#isFile}}**{{dataType}}**{{/isFile}}{{^isFile}}[**{{dataType}}**]({{baseType}}.md){{/isFile}}{{/isPrimitiveType}} | {{description}} | {{^required}}[optional]{{/required}}{{#defaultValue}}[default to {{defaultValue}}]{{/defaultValue}} |
{{/allParams}}

### Return type
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@
end
end
else # model
{{moduleName}}.const_get(type).build_from_hash(value)
# models (e.g. Pet) or oneOf
klass = {{moduleName}}.const_get(type)
klass.respond_to?(:openapi_one_of) ? klass.build(value) : klass.build_from_hash(value)
end
end

Expand All @@ -93,7 +95,7 @@
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,14 @@ module {{moduleName}}
{{>partial_model_enum_class}}
{{/isEnum}}
{{^isEnum}}
{{#oneOf}}
{{#-first}}
{{>partial_oneof_module}}
{{/-first}}
{{/oneOf}}
{{^oneOf}}
{{>partial_model_generic}}
{{/oneOf}}
{{/isEnum}}
{{/model}}
{{/models}}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
{{#models}}{{#model}}# {{moduleName}}::{{classname}}

## Properties

Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
{{#vars}}**{{name}}** | {{#isPrimitiveType}}**{{dataType}}**{{/isPrimitiveType}}{{^isPrimitiveType}}[**{{dataType}}**]({{complexType}}.md){{/isPrimitiveType}} | {{description}} | {{^required}}[optional] {{/required}}{{#isReadOnly}}[readonly] {{/isReadOnly}}{{#defaultValue}}[default to {{defaultValue}}]{{/defaultValue}}
{{/vars}}

## Code Sample

```ruby
require '{{moduleName}}'

instance = {{moduleName}}::{{classname}}.new({{#vars}}{{name}}: {{example}}{{^-last}},
{{/-last}}{{/vars}})
```

{{/model}}{{/models}}
{{#models}}
{{#model}}
{{#oneOf}}
{{#-first}}
{{>partial_oneof_module_doc}}
{{/-first}}
{{/oneOf}}
{{^oneOf}}
{{>partial_model_generic_doc}}
{{/oneOf}}
{{/model}}
{{/models}}
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,13 @@ require 'date'
# Please update as you see appropriate
{{#models}}
{{#model}}
describe '{{classname}}' do
before do
# run before each test
@instance = {{moduleName}}::{{classname}}.new
end

after do
# run after each test
end
describe {{moduleName}}::{{classname}} do
{{^oneOf}}
let(:instance) { {{moduleName}}::{{classname}}.new }

describe 'test an instance of {{classname}}' do
it 'should create an instance of {{classname}}' do
expect(@instance).to be_instance_of({{moduleName}}::{{classname}})
expect(instance).to be_instance_of({{moduleName}}::{{classname}})
end
end
{{#vars}}
Expand All @@ -33,7 +27,7 @@ describe '{{classname}}' do
# assertion here. ref: https://www.relishapp.com/rspec/rspec-expectations/docs/built-in-matchers
# validator = Petstore::EnumTest::EnumAttributeValidator.new('{{{dataType}}}', [{{#allowableValues}}{{#enumVars}}{{{value}}}{{^-last}}, {{/-last}}{{/enumVars}}{{/allowableValues}}])
# validator.allowable_values.each do |value|
# expect { @instance.{{name}} = value }.not_to raise_error
# expect { instance.{{name}} = value }.not_to raise_error
# end
{{/isEnum}}
{{^isEnum}}
Expand All @@ -43,6 +37,42 @@ describe '{{classname}}' do
end

{{/vars}}
{{/oneOf}}
{{#oneOf}}
{{#-first}}
describe '.openapi_one_of' do
it 'lists the models referenced in the oneOf array' do
expect(described_class.openapi_one_of).to_not be_empty
described_class.openapi_one_of.each { |klass| expect { {{moduleName}}.const_get(klass) }.to_not raise_error }
end
end

{{#discriminator}}
{{#propertyName}}
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

{{/propertyName}}
{{#mappedModels}}
{{#-first}}
describe '.openapi_discriminator_mapping' do
it 'returns the key/values of the "mapping" property' do
expect(described_class.openapi_discriminator_mapping.values.sort).to eq(described_class.openapi_one_of.sort)
end
end

{{/-first}}
{{/mappedModels}}
{{/discriminator}}
describe '.build' do
it 'returns the correct model' do
end
end
{{/-first}}
{{/oneOf}}
end
{{/model}}
{{/models}}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@
}
end

# Returns all the JSON keys this model knows about{{#parent}}, including the ones defined in its parent(s){{/parent}}
def self.acceptable_attributes
{{^parent}}
attribute_map.values
{{/parent}}
{{#parent}}
attribute_map.values.concat(superclass.acceptable_attributes)
{{/parent}}
end

# Attribute type mapping.
def self.openapi_types
{
Expand Down Expand Up @@ -75,19 +85,6 @@

{{/-last}}
{{/anyOf}}
{{#oneOf}}
{{#-first}}
# List of class defined in oneOf (OpenAPI v3)
def self.openapi_one_of
[
{{/-first}}
:'{{{.}}}'{{^-last}},{{/-last}}
{{#-last}}
]
end

{{/-last}}
{{/oneOf}}
{{#allOf}}
{{#-first}}
# List of class defined in allOf (OpenAPI v3)
Expand Down Expand Up @@ -270,26 +267,6 @@

{{/-first}}
{{/anyOf}}
{{#oneOf}}
{{#-first}}
_one_of_found = false
self.class.openapi_one_of.each do |_class|
_one_of = {{moduleName}}.const_get(_class).build_from_hash(self.to_hash)
if _one_of.valid?
if _one_of_found
return false
else
_one_of_found = true
end
end
end

if !_one_of_found
return false
end

{{/-first}}
{{/oneOf}}
true{{#parent}} && super{{/parent}}
end

Expand Down Expand Up @@ -391,4 +368,4 @@
end

{{> base_object}}
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# {{moduleName}}::{{classname}}

## Properties

| Name | Type | Description | Notes |
| ---- | ---- | ----------- | ----- |
{{#vars}}
| **{{name}}** | {{#isPrimitiveType}}**{{dataType}}**{{/isPrimitiveType}}{{^isPrimitiveType}}[**{{dataType}}**]({{complexType}}.md){{/isPrimitiveType}} | {{description}} | {{^required}}[optional]{{/required}}{{#isReadOnly}}[readonly]{{/isReadOnly}}{{#defaultValue}}[default to {{defaultValue}}]{{/defaultValue}} |
{{/vars}}

## Code Sample

```ruby
require '{{{gemName}}}'

{{^vars}}
instance = {{moduleName}}::{{classname}}.new()
{{/vars}}
{{#vars}}
{{#-first}}
instance = {{moduleName}}::{{classname}}.new(
{{/-first}}
{{name}}: {{example}}{{^-last}},{{/-last}}
{{#-last}}
)
{{/-last}}
{{/vars}}
```
Loading