OpenAPITools / OpenAPITools/openapi-generator
[BUG][Ruby] oneOf with discriminator fails to deserialize when propertyName is transfored for ruby attribute name
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 26.8k
- Forks
- 7.7k
- PR merge metrics
- PR metrics pending
Description
Bug Report Checklist
- Have you provided a full/minimal spec to reproduce the issue?
- Have you validated the input using an OpenAPI validator (example)?
- Have you tested with the latest master to confirm the issue still exists?
- Have you searched for related issues/PRs?
- What's the actual output vs expected output?
- [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description
oneOf fails to deserialize when using discriminator when the attribute name is transformed for the internal Ruby attribute.
e.g. when using a discriminator with the attribute name _TYPE, this will always fail to deserialize as it is looking for _type in the JSON.
openapi-generator version
openapi-generator-cli 6.0.1
OpenAPI declaration file content or url
Here is an example spec.
openapi: '3.0.0'
info:
version: "1.0"
title: Example
paths:
/example:
get:
responses:
'200':
description: 'Can return one of two objects'
content:
application/json:
schema:
$ref: '#/components/schemas/TopLevelObject'
components:
schemas:
TopLevelObject:
oneOf:
- $ref: '#/components/schemas/FirstObject'
- $ref: '#/components/schemas/SecondObject'
discriminator:
propertyName: '_TYPE'
FirstObject:
type: object
properties:
_TYPE:
type: string
SecondObject:
type: object
properties:
_TYPE:
type: string
Generation Details
I generated a ruby gem using the faraday library.
Steps to reproduce
When parsing the JSON
{
"_TYPE": "FirstObject"
}
It should an instance of FirstObject. However it returns nil.
Related issues/PRs
I haven't found any related issues/ PRs
Suggest a fix
Inside the oneOf ruby model it picks the type using the following code:
module OpenapiClient
module TopLevelObject
class << self
# List of class defined in oneOf (OpenAPI v3)
def openapi_one_of
[
:'FirstObject',
:'SecondObject'
]
end
# Discriminator's property name (OpenAPI v3)
def openapi_discriminator_name
:'_type'
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?
OpenapiClient.const_get(discriminator_value).build_from_hash(data)
end
end
end
end
The openapi_discriminator_name method should return _TYPE instead of returning _type.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Generate a Ruby gem with the supplied OpenAPI YAML and inspect the generated TopLevelObject oneOf model. Start at openapi_discriminator_name and the build method, then reproduce parsing {"_TYPE":"FirstObject"}. Done means the discriminator lookup uses the declared property name and returns an instance of FirstObject instead of nil.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- ruby
- Domain
- api, tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 45/100