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: schema for generic wrapped return types with DTO #3371

Merged
merged 4 commits into from
Apr 11, 2024

Conversation

peterschutt
Copy link
Contributor

@peterschutt peterschutt commented Apr 10, 2024

Changelog

Fix schema generated for DTOs where the supported type is wrapped in a generic outer type.

Description

Prior behavior of using the backend.annotation as the basis for generating the openapi schema for the represented type is not applicable for the case where the DTO supported type is wrapped in a generic outer object. In that case backend.annotation only represents the type of the attribute on the generic type that holds the DTO supported type annotation.

This PR detects the case where we unwrap an outer generic type, and rebuilds the generic annotation in a manner appropriate for schema generation, before generating the schema for the annotation. It does this by substituting the DTOs transfer model for the original model in the original annotations type arguments.

<tangent>
This is really treating a symptom of the generic outer type being removed from the annotation before the DTO backend receives it, which is a design flaw that adds quite a bit of complexity. The DTOs should treat annotations at a whole, rebuild the complete annotation substituting out any supported types found within with transfer models.

Also, we don't really have a need for a separate backend anymore, now that we are only supporting one serialization library (backends were introduced when we were trying to support either pydantic or msgspec (and attrs?)) throughout the whole application.
</tangent>

Closes

Closes #2929

tests/helpers.py Outdated Show resolved Hide resolved
@github-actions github-actions bot added the area/dto This PR involves changes to the DTOs label Apr 10, 2024
@peterschutt peterschutt changed the title fix: Fix schema for generic wrapped return types with DTO fix: schema for generic wrapped return types with DTO Apr 10, 2024
Copy link

codecov bot commented Apr 10, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.28%. Comparing base (dd9c401) to head (930ac31).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #3371   +/-   ##
=======================================
  Coverage   98.28%   98.28%           
=======================================
  Files         323      323           
  Lines       14773    14777    +4     
  Branches     2345     2347    +2     
=======================================
+ Hits        14519    14523    +4     
  Misses        116      116           
  Partials      138      138           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@peterschutt peterschutt force-pushed the 2929-openapi-for-dto-wrapped-generic branch from 6b3a18d to aa57371 Compare April 10, 2024 23:57
Prior behavior of using the `backend.annotation` as the basis for generating the openapi schema for the represented type is not applicable for the case where the DTO supported type is wrapped in a generic outer object.

This PR detects the case where we unwrap an outer generic type, and rebuilds the generic annotation in a manner appropriate for schema generation, before generating the schema for the annotation. It does this by substituting the DTOs transfer model for the original model in the original annotations type arguments.

This is really treating a symptom of the generic outer type being removed from the annotation before the DTO backend receives it, which is a design flaw that adds quite a bit of complexity. The DTOs should treat annotations at a whole, rebuild the complete annotation substituting out any supported types found within with transfer models.

Also, we don't really have a need for a separate backend anymore, now that we are only supporting one serialization library (backend were introduced when we were trying to support either pydantic or msgspec (and attrs?) throughout the whole application.
@peterschutt peterschutt force-pushed the 2929-openapi-for-dto-wrapped-generic branch from aa57371 to 8a209c6 Compare April 11, 2024 00:05
Copy link

sonarcloud bot commented Apr 11, 2024

Copy link

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

@peterschutt peterschutt merged commit 40a5685 into main Apr 11, 2024
29 checks passed
@peterschutt peterschutt deleted the 2929-openapi-for-dto-wrapped-generic branch April 11, 2024 03:44
peterschutt added a commit that referenced this pull request Apr 11, 2024
* fix: Fix schema for generic wrapped return types with DTO

Adds failing test.

Closes #2929

* fix: remove redundant type var

* fix: fix openapi schema for generic return types with dto

Prior behavior of using the `backend.annotation` as the basis for generating the openapi schema for the represented type is not applicable for the case where the DTO supported type is wrapped in a generic outer object.

This PR detects the case where we unwrap an outer generic type, and rebuilds the generic annotation in a manner appropriate for schema generation, before generating the schema for the annotation. It does this by substituting the DTOs transfer model for the original model in the original annotations type arguments.

This is really treating a symptom of the generic outer type being removed from the annotation before the DTO backend receives it, which is a design flaw that adds quite a bit of complexity. The DTOs should treat annotations at a whole, rebuild the complete annotation substituting out any supported types found within with transfer models.

Also, we don't really have a need for a separate backend anymore, now that we are only supporting one serialization library (backend were introduced when we were trying to support either pydantic or msgspec (and attrs?) throughout the whole application.

* test: add another test.

(cherry picked from commit 40a5685)
peterschutt added a commit that referenced this pull request Apr 11, 2024
* fix: Fix schema for generic wrapped return types with DTO

Adds failing test.

Closes #2929

* fix: remove redundant type var

* fix: fix openapi schema for generic return types with dto

Prior behavior of using the `backend.annotation` as the basis for generating the openapi schema for the represented type is not applicable for the case where the DTO supported type is wrapped in a generic outer object.

This PR detects the case where we unwrap an outer generic type, and rebuilds the generic annotation in a manner appropriate for schema generation, before generating the schema for the annotation. It does this by substituting the DTOs transfer model for the original model in the original annotations type arguments.

This is really treating a symptom of the generic outer type being removed from the annotation before the DTO backend receives it, which is a design flaw that adds quite a bit of complexity. The DTOs should treat annotations at a whole, rebuild the complete annotation substituting out any supported types found within with transfer models.

Also, we don't really have a need for a separate backend anymore, now that we are only supporting one serialization library (backend were introduced when we were trying to support either pydantic or msgspec (and attrs?) throughout the whole application.

* test: add another test.

(cherry picked from commit 40a5685)
peterschutt added a commit that referenced this pull request May 1, 2024
* fix: Fix schema for generic wrapped return types with DTO

Adds failing test.

Closes #2929

* fix: remove redundant type var

* fix: fix openapi schema for generic return types with dto

Prior behavior of using the `backend.annotation` as the basis for generating the openapi schema for the represented type is not applicable for the case where the DTO supported type is wrapped in a generic outer object.

This PR detects the case where we unwrap an outer generic type, and rebuilds the generic annotation in a manner appropriate for schema generation, before generating the schema for the annotation. It does this by substituting the DTOs transfer model for the original model in the original annotations type arguments.

This is really treating a symptom of the generic outer type being removed from the annotation before the DTO backend receives it, which is a design flaw that adds quite a bit of complexity. The DTOs should treat annotations at a whole, rebuild the complete annotation substituting out any supported types found within with transfer models.

Also, we don't really have a need for a separate backend anymore, now that we are only supporting one serialization library (backend were introduced when we were trying to support either pydantic or msgspec (and attrs?) throughout the whole application.

* test: add another test.

(cherry picked from commit 40a5685)
peterschutt added a commit that referenced this pull request May 2, 2024
* fix: Fix schema for generic wrapped return types with DTO

Adds failing test.

Closes #2929

* fix: remove redundant type var

* fix: fix openapi schema for generic return types with dto

Prior behavior of using the `backend.annotation` as the basis for generating the openapi schema for the represented type is not applicable for the case where the DTO supported type is wrapped in a generic outer object.

This PR detects the case where we unwrap an outer generic type, and rebuilds the generic annotation in a manner appropriate for schema generation, before generating the schema for the annotation. It does this by substituting the DTOs transfer model for the original model in the original annotations type arguments.

This is really treating a symptom of the generic outer type being removed from the annotation before the DTO backend receives it, which is a design flaw that adds quite a bit of complexity. The DTOs should treat annotations at a whole, rebuild the complete annotation substituting out any supported types found within with transfer models.

Also, we don't really have a need for a separate backend anymore, now that we are only supporting one serialization library (backend were introduced when we were trying to support either pydantic or msgspec (and attrs?) throughout the whole application.

* test: add another test.

(cherry picked from commit 40a5685)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/dto This PR involves changes to the DTOs pr/internal size: small type/bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

bug: openapi schema generation for DTO with generic wrapper
2 participants