[BUG][Ruby] Using oneOf generated code has unexpected results
Created by: ssteeg-mdsol
Bug Report Checklist
-
Have you provided a full/minimal spec to reproduce the issue? -
Have you validated the input using an OpenAPI validator (example)? -
What's the version of OpenAPI Generator used? -
Have you search for related issues/PRs? -
What's the actual output vs expected output? -
[Optional] Bounty to sponsor the fix (example)
Description
When a oneOf is used, the generated model only includes one of the possible classes. Using the client, objects for all class types are forced into the same class, even if they're a different class.
In the below example, if the API returns a AuthenticationMonitor MonitorResult object, the generated client forces it into the SmokeTestMonitor class type. This is a problem as it's the incorrect class type and as a result the model attributes are all nil.
Current output for the AuthenticationMonitor MonitorResult object:
...
@items=[#<ClientExample::SmokeTestResult:0x00007fb45220eaa0>]
...
Expected output for the AuthenticationMonitor MonitorResult object:
...
@items=[#<ClientExample::AuthenticationMonitorItems:0x00007fb45220eaa0 @result=true>]
...
openapi-generator version
4.1.0, seems to occur in every 4.x version. Haven't checked previous versions.
OpenAPI declaration file content or url
OpenAPI spec
AuthenticationMonitor and SmokeTestMonitor are the same but the property name inside items is different
openapi: 3.0.2
info:
title: Example
version: v1
paths:
/endpoint:
get:
summary: Get All Monitors
responses:
'200':
description: Successful operation.
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/Monitor'
components:
schemas:
Monitor:
type: object
properties:
results:
$ref: '#/components/schemas/MonitorResults'
MonitorResults:
oneOf:
- $ref: '#/components/schemas/AuthenticationMonitor'
- $ref: '#/components/schemas/SmokeTestMonitor'
AuthenticationMonitor:
type: object
properties:
items:
type: array
items:
type: object
properties:
result:
type: boolean
SmokeTestMonitor:
type: object
properties:
items:
type: array
description: Collection of smoke test result checks.
items:
type: object
properties:
passed:
type: boolean
Command line used for generation
openapi-generator generate -i <OPENAPI_SPEC> -g ruby -o <TEMP_DIR> -c <CONFIG_PATH>
Steps to reproduce
- Run the command to generate the Ruby client
- Look at the
Related issues/PRs
Maybe https://github.com/OpenAPITools/openapi-generator/issues/15 is related, but this issue is more specific to the Ruby client.
Another issue related to oneOf for the Ruby client is the generation of objects within a response schema. If you have the following OpenAPI spec, the class type gets generated as ExampleClient::OneOfsimpleObjectcomplexObject, and that class does not exist.
OpenAPI spec snippet
Here is the spec:paths:
/endpoint:
get:
summary: Get All Monitors
responses:
'200':
description: Successful operation.
content:
application/json:
schema:
oneOf:
- $ref: '#/components/schemas/simpleObject'
- $ref: '#/components/schemas/complexObject'
components:
schemas:
simpleObject:
type: object
properties:
simple_property1:
type: string
complexObject:
type: object
properties:
complex_property1:
type: string
complex_property2:
type: string
Suggest a fix
In https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/ruby-client/partial_model_generic.mustache#L49, the generated client only includes one option Array<SmokeTestResult>. I'm not sure about a specific fix, but has there been any Ruby-specific development into this area?