Skip to content

Conversation

goodsonjr
Copy link
Contributor

This PR adds support for reference schemas (#800).

The idea with this change is that during parsing of the schemas, when the parser encounters a Reference schema, rather than aborting it will attempt to fully resolve that reference by following the chain of references until it encounters a Schema. When it does, it will attempt to fetch that parsed Schema from the dictionary of available components, which should include all possible referent schemas. If it exists, it will parse the current/original reference Schema as if it was its ultimate referent. If it reaches a reference that does not exist, it logs an error and moves on: it won't be able to parse this Reference.

The result of this is that every reference schema in the API doc will result in a separate-but-identical class, aside from the class name. This feels like a safe, basic way to handle the situation, since collapsing identical classes might break compatibility between the API and the codegen at some point. I suppose we could instead have references be subclasses or some trick like that to cut down on code duplication and let identical API objects be equivalent? If that would be preferable it is probably doable downstream of this change, since with this parsing the Schema objects added as references in the Schemas class should be identical objects which could be identified and collapsed in some fashion during code templating. Perhaps it would be useful to record the direction of references during parsing to better choose what a base class would be... That would require some more thought.

In any case, this works both for single references as well as arbitrarily deep nested references, something useful for the API I'm developing for.

Also includes some functional tests which include both a valid and invalid (circular reference) case for reference schemas and adjusts an existing unit test which assumed that the reference schema would be skipped to one that tests whether it has been parsed.

Copy link
Collaborator

@dbanty dbanty left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great to me, thanks!

@dbanty dbanty changed the title feat: Reference schemas feat: Reference schema support (#800) Sep 13, 2025
@dbanty dbanty added this pull request to the merge queue Sep 13, 2025
Merged via the queue into openapi-generators:main with commit a2a66e2 Sep 13, 2025
22 checks passed
@knope-bot knope-bot bot mentioned this pull request Sep 13, 2025
github-merge-queue bot pushed a commit that referenced this pull request Sep 13, 2025
> [!IMPORTANT]
> Merging this pull request will create this release

## Features

- Reference schema support (#800) (#1307)
- Support Ruff 0.13

Co-authored-by: knope-bot[bot] <152252888+knope-bot[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants