OpenAPITools / OpenAPITools/openapi-generator
[BUG] [Ruby] Inherited (allOf) properties not (de)serialized
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
Using the generated Ruby (client) code, model attributes inherited from a parent class do not get parsed and assigned to object instances when deserializing API responses.
Let's use a classic Dog < Pet example (see OpenAPI spec here) where Dog has an attribute packSize and includes (allOf) Pet's attributes name and petType.
When GETting a Dog from the API, the only attribute read from the response and assigned to the instance is packSize.
Obviously, name and petType should be assigned as well.
From what I gather, the problem is in the build_from_hash method in the generated Pet class:
in this case, this method is called by the super call in the same method of the Dog class and the code is referring to openapi_types and attribute_map called on on self.class. But since the object being parsed here is a Dog, self.class refers to Dog and the only attribute returned from these methods is packSize.
Similarly, when serializing objects of type Dog, method to_hash ignores the attributes defined in Pet.
Also, constructors of child classes do not accept parent class attributes. The following code fails because initialize in Dog checks whether the provided attributes are included in the attribute_map (which does not contain attributes from the parent class).
dog = OpenapiClient::Dog.new(pack_size: 1, name: "Fido")
openapi-generator version
v5.3.1
OpenAPI declaration file content or url
Generation Details
docker run --rm -v ${PWD}:/local openapitools/openapi-generator-cli:v5.3.1 generate -i /local/oas.yaml -g ruby -o /local/
Steps to reproduce
Generate the code using the linked OpenAPI spec, possibly also run a mock server like API Sprout (docker run -p 8000:8000 -v ${PWD}/oas.yaml:/oas.yaml danielgtaylor/apisprout /oas.yaml) and:
- GET a list of dogs and observe the attributes the objects are assigned.
client = OpenapiClient::DefaultApi.new
dogs = client.dogs_get()
puts dogs[0]
- Create a dog instance and POST it:
dog = OpenapiClient::Dog.new(pack_size: 1, name: "Fido")
client.dogs_post(
dog: dog
)
Suggest a fix
See description above for (my impression of) what's causing the problem.
As I see it, a simple fix would be to change the build_from_hash and to_hash methods in the template by replacing self.class calls with {{classname}}.
I would gladly submit a PR after getting some feedback that this is indeed a bug and I'm not actually using something in a wrong/unexpected way.
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 the Ruby client from the linked OpenAPI spec using the provided Docker command, then inspect the generated build_from_hash, to_hash, and initialize methods in the Pet and Dog classes. Reproduce deserialization, serialization, and construction with inherited attributes. Done means parent attributes are accepted, parsed, assigned, and included when serializing child instances.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- ruby
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100