[BUG] Generator return type is incorrect for objects with both properties and additionalProperties specified
Bug Report Checklist
- [x] Have you provided a full/minimal spec to reproduce the issue?
- [x] Have you validated the input using an OpenAPI validator (example)?
- [x] What's the version of OpenAPI Generator used?
- [x] Have you search for related issues/PRs?
- [ ] What's the actual output vs expected output?
- [ ] [Optional] Bounty to sponsor the fix (example)
Description
When using OAS3 schema as shown below, generator creates incorrect return type.
Eg. for Python return type is dict(str, InlineResponse200). Similar problem for typescript and other outputs.
Also the default generator debug output shows:
"returnType" : "dict(str, InlineResponse200)",
"returnBaseType" : "InlineResponse200",
Return type is incorrect as service_status field is of type string and not InlineResponse200.
It seems that properties data are ignored in this case and only info from additionalProperties is used.
openapi-generator version
4.2.3
OpenAPI declaration file content or url
openapi: 3.0.1
servers:
- url: 'localhost:8080'
info:
version: 1.0.0
title: OpenAPI Test
license:
name: Apache-2.0
url: 'https://www.apache.org/licenses/LICENSE-2.0.html'
paths:
/animals:
get:
operationId: testComposed
responses:
200:
description: test function
content:
application/json:
schema:
type: object
title: Segments
description: Collection of segments.
properties:
service_status:
type: string
description: Status of the request.
example: ok
additionalProperties:
type: object
description: property name is the id of the segment.
properties:
seatmap:
type: string
description: Animal seatmap.
status:
type: string
description: >
Status of the cage for the animals, if it is available or not.
example: available
Command line used for generation
java -jar openapi-generator-cli.jar generate -i issue.yaml -o test-python -g python -DdebugOperations
Steps to reproduce
Related issues/PRs
Similar issue, but not only for Java. This seems to be a problem of generator itself, not just the language outputs: https://github.com/OpenAPITools/openapi-generator/issues/1466
Suggest a fix
properties data should not be ignored
👍 Thanks for opening this issue! 🏷 I have applied any labels matching special text in your issue.
The team will review the labels and make any necessary changes.
Any updates on this issue?
Using this operation:
/addpropsContainsObjectSchema:
get:
operationId: addpropsContainsObjectSchema
tags:
- fake
responses:
200:
description: addpropsContainsObjectSchema
content:
application/json:
schema:
type: object
title: Segments
description: Collection of segments.
properties:
service_status:
type: string
description: Status of the request.
example: ok
additionalProperties:
type: object
description: property name is the id of the segment.
properties:
seatmap:
type: string
description: Animal seatmap.
status:
type: string
description: >
Status of the cage for the animals, if it is available or not.
example: available
this bug still exists.
From my investigation into it using the current 5.1.0 branch
- InlineModelResolver extracts the response schema.additionalProperties schema into a model called InlineResponse200.
- looking at the response type thought it describes the response as
({str: (InlineResponse200,)},)when one would expect to just see(InlineResponse200,) - The definition for InlineResponse200 contains only definition from schema.additionalProperties, so it has the properties seatmap and status
- We are missing the service_status property in the response schema
- additional_properties_type should not be None in InlineResponse200
Work around solution
If you extract this response schema into its own component it should work correctly. You may also need to extract the additional properties schema into its own component also. Extracted components will need to be referred to with $refs This is happening because our tooling does not handle inline complex schemas well. The code attempts to handle this case with the InlineModelResolver but that extracted the wrong schema into a component.
Long term solutions here:
- Fix inline model resolver so it extracts the correct components (easier)
- Make the generators capable of handling inline schema definitions (harder)
This bug has been fixed in the python-experimental generator.
That generator handles inline schemas at any location and of any depth.
Return types are fully defined with schema classes and those schemas can include properties and additionalProperties definitions