Skip to content

Commit

Permalink
[ruby] fix oneOf handling (#5706)
Browse files Browse the repository at this point in the history
* [ruby] fix oneOf handling

* use previous ruby configs due to issue #4690

* check for oneOf model in base_object

* validate the attributes in partial_oneof_module
jfeltesse-mdsol authored Nov 23, 2020

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 6f6822a commit 522faf8
Showing 221 changed files with 2,719 additions and 1,732 deletions.
Original file line number Diff line number Diff line change
@@ -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

Original file line number Diff line number Diff line change
@@ -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}}
@@ -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
Original file line number Diff line number Diff line change
@@ -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

@@ -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
Original file line number Diff line number Diff line change
@@ -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}}
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
@@ -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}}
@@ -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}}
@@ -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
@@ -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
{
@@ -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)
@@ -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

@@ -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

0 comments on commit 522faf8

Please sign in to comment.