OpenAPITools / OpenAPITools/openapi-generator

[BUG] Child Schemas are not able to be populated into model list

Open
#7,408 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
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

I am using bundled code from swagger cli. In my case, the bundler is putting $ref to Schemas that are a few levels deep into response and requestBodies (i.e. properties of objects that are properties of object...etc.). Currently, in the default generator, the models are gathered by this call:

''' java
final Map<String, Schema> schemas = ModelUtils.getSchemas(this.openAPI);
'''

which, in the javadocs, is stated the following:

'''java
/**
* Return a Map of the schemas defined under /components/schemas in the OAS
* document. The returned Map only includes the direct children of
* /components/schemas in the OAS document; the Map does not include inlined
* schemas.
*
* @param openAPI the OpenAPI document.
* @return a map of schemas in the OAS document.
*/
'''
So the problem is that there is never a search for second level+ models. This cause a few issues, namely, that any model that references these other models will resolve them to Object. The correct thing that should be done here is that all models should be included. There is even a function right below this one that returns all Schemas, however it does it in list form.

In my local clone, I have added a function to "true up" the schemas above by looking for all schemas and then adding the new ones to the map. Later, everything seems to work correctly, especially when I have fixed the path walking issue in #7400. However, this seems like a separate issue, so I am filing it separately.

All of my work has been based off of v4.3.1. I can create a PR upon request, but I am not sure if my solution is the correct one for the maintainers.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with ModelUtils.getSchemas and the nearby function that returns all schemas, then inspect how the default generator collects models from nested request bodies, responses, and properties. Reproduce the issue with a bundled OpenAPI specification and confirm that referenced child schemas are included and generated models no longer resolve them to Object.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, openapi
Domain
api
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.