-
-
Notifications
You must be signed in to change notification settings - Fork 305
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
[16.0][IMP] extendable_fastapi: Add generics schemas #380
Merged
OCA-git-bot
merged 2 commits into
OCA:16.0
from
acsone:16.0-extendable-fastapi-paging-models
Oct 13, 2023
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
* New base schemas: *PagedCollection*. This schema is used to define the | ||
the structure of a paged collection of resources. This schema is similar | ||
to the ones defined in the Odoo's **fastapi** addon but works as/with | ||
extendable models. | ||
|
||
* The *StrictExtendableBaseModel* has been moved to the *extendable_pydantic* | ||
python lib. You should consider to import it from there. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,19 @@ | ||
from extendable_pydantic import ExtendableBaseModel | ||
from typing import Annotated, Generic, TypeVar | ||
|
||
from extendable_pydantic import StrictExtendableBaseModel | ||
|
||
class StrictExtendableBaseModel( | ||
ExtendableBaseModel, | ||
revalidate_instances="always", | ||
validate_assignment=True, | ||
extra="forbid", | ||
): | ||
""" | ||
An ExtendableBaseModel with strict validation. | ||
from pydantic import Field | ||
|
||
By default, Pydantic does not revalidate instances during validation, nor | ||
when the data is changed. Validation only occurs when the model is created. | ||
This is not suitable for a REST API, where the data is changed after the | ||
model is created or the model is created from a partial set of data and | ||
then updated with more data. This class enforces strict validation by | ||
forcing the revalidation of instances when the method `model_validate` is | ||
called and by ensuring that the values assignment is validated. | ||
T = TypeVar("T") | ||
|
||
The following parameters are added: | ||
* revalidate_instances="always": model instances are revalidated during validation | ||
(default is "never") | ||
* validate_assignment=True: revalidate the model when the data is changed (default is False) | ||
* extra="forbid": Forbid any extra attributes (default is "ignore") | ||
""" | ||
|
||
class PagedCollection(StrictExtendableBaseModel, Generic[T]): | ||
"""A paged collection of items""" | ||
|
||
# This is a generic model. The type of the items is defined by the generic type T. | ||
# It provides you a common way to return a paged collection of items of | ||
# extendable models. It's based on the StrictExtendableBaseModel to ensure | ||
# a strict validation when used within the odoo fastapi framework. | ||
|
||
count: Annotated[int, Field(..., description="The count of items into the system")] | ||
items: list[T] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
The field *total* in the *PagedCollection* schema is replaced by the field *count*. | ||
The field *total* is now deprecated and will be removed in the next major version. | ||
This change is backward compatible. The json document returned will now | ||
contain both fields *total* and *count* with the same value. In your python | ||
code the field *total*, if used, will fill the field *count* with the same | ||
value. You are encouraged to use the field *count* instead of *total* and adapt | ||
your code accordingly. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@sbidoul @sebastienbeau @qgroulard I'm not sure it's the right place to define these base schemas. Even if they are mainly used with fastapi, we also use the
StrictExtendableBaseModel
when we define models to be stored into a search engine. This leads to a useless dependency on fastapi. Do we've to create a specific addon for theStrictExtendableBaseModel
? I could also move it to theextendable_fastapi
python lib.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But this PR is for
extendable_fastapi
?Question: why would one want to extend PagedCollection or Paging? Or is this necessary for compatibility when T is extendable?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, at least for PagedCollection, it's necessary for compatibility when T is extendable.
Regarding
StrictExtendableBaseModel
, it's not defined at the right place or we should not use-it when defining schemas for the search engine index.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, yes I see. So you mean defining StrictExtendableBaseModel in
extendable_pydantic
, right?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, makes sense to me. Let's do that.