Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: recursive schema generation #2869

Merged
merged 13 commits into from
Dec 13, 2023
Merged

Conversation

peterschutt
Copy link
Contributor

This PR modifies the way we generate schemas for objects that should be able to be referenced by other schemas.

It adds a new method to SchemaCreator called create_component_schema that should be used to create a schema that should be able to be referenced by other schemas, and itself.

This method takes care to first create the schema in the registry so that it can be immediately referenced.

In SchemaCreator.for_field_definition(), before we generate a schema for any plugin-supported type, we first check whether a reference can be made to an already existing schema and return that if possible.

Closes #2429

This PR adopts OpenAPISchemaPlugin for generation of schemas for all supported model types.
@peterschutt peterschutt requested review from a team as code owners December 10, 2023 08:48
This PR modifies the way we generate schemas for objects that should be able to be referenced by other schemas.

It adds a new method to `SchemaCreator` called `create_component_schema` that should be used to create a schema that should be able to be referenced by other schemas, and itself.

This method takes care to first create the schema in the registry so that it can be immediately referenced.

In `SchemaCreator.for_field_definition()`, before we generate a schema for any plugin-supported type, we first check whether a reference can be made to an already existing schema and return that if possible.

Closes #2429
@peterschutt peterschutt force-pushed the support-schemas-for-recursive-types branch from 8b4de0d to 33b829a Compare December 10, 2023 08:51
Fixes an error where types such as `UnionType` do not have a `__qualname__` by ensuring we handle the union type annotation before we attempt to apply plugin handling to the type.
@peterschutt peterschutt added Bug 🐛 This is something that is not working as expected Refactor This is an improvement to existing code or configuration OpenAPI This is related to our OpenAPI schema labels Dec 11, 2023
@peterschutt peterschutt self-assigned this Dec 11, 2023
Base automatically changed from use-openapi-schema-plugin-for-supported-types to main December 11, 2023 01:30
# Conflicts:
#	litestar/_openapi/schema_generation/plugins/dataclass.py
#	litestar/_openapi/schema_generation/plugins/struct.py
#	litestar/_openapi/schema_generation/plugins/typed_dict.py
#	litestar/_openapi/schema_generation/schema.py
#	litestar/plugins/base.py
If we don't do this, then users won't have the chance to add a plugin that might override our handling of a collection type etc.
The condition where the schema type is a sequence is possible accoring to the schema spec, however in the TYPE_MAP, we don't have any types where the schema type is a sequence.
@peterschutt
Copy link
Contributor Author

For some reason, sonar is pinging me for coverage lines and conditions that have nothing to do with this PR :/

We have this thing called a `SchemaDataContainer` that is untested, and undocumented. Sonar is reporting the missing coverage on this because I've modified the method, but I have no clue what this thing is, or does.
At this point I'm just guessing WTH sonar is asking for.
@JacobCoffee
Copy link
Member

93.75% Condition Coverage on New Code (required ≥ 98%)

could you get coverage up a bit?

@peterschutt
Copy link
Contributor Author

93.75% Condition Coverage on New Code (required ≥ 98%)

could you get coverage up a bit?

image

@peterschutt peterschutt enabled auto-merge (squash) December 13, 2023 06:04
Copy link

@peterschutt peterschutt merged commit 0f2c4dd into main Dec 13, 2023
@peterschutt peterschutt deleted the support-schemas-for-recursive-types branch December 13, 2023 06:14
Copy link

Documentation preview will be available shortly at https://litestar-org.github.io/litestar-docs-preview/2869

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug 🐛 This is something that is not working as expected OpenAPI This is related to our OpenAPI schema Refactor This is an improvement to existing code or configuration
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bug: OpenAPI schema generation fails with recursive models
3 participants