-
-
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
[16.0][IMP] extendable_fastapi: Add generics schemas #380
Conversation
4c0428a
to
dfdc908
Compare
@@ -24,3 +26,28 @@ class StrictExtendableBaseModel( | |||
* validate_assignment=True: revalidate the model when the data is changed (default is False) | |||
* extra="forbid": Forbid any extra attributes (default is "ignore") | |||
""" | |||
|
|||
|
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 the StrictExtendableBaseModel
? I could also move it to the extendable_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.
I could also move it to the extendable_fastapi python lib.
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.
dfdc908
to
dad0fb4
Compare
extendable_fastapi/schemas.py
Outdated
# extendable models. It's based on the StrictExtendableBaseModel to ensure | ||
# a strict validation when used within the odoo fastapi framework. | ||
|
||
total: int |
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.
Probably too late but... is count
a better field name than total
?
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.
IMO, It's not too late. What do you propose? count
?
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 count, but it's not very important.
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.
Wording is important. I find a way to ensure the consistency with the same version of the model defined in the fastapi odoo addon but not supporting extendable items. The idea is to deprecated the use of the total
field but keeping the compatibility on both side: json and python
import json
from typing import Annotated
from pydantic import BaseModel, computed_field, Field, AliasChoices
class Paginated(BaseModel):
count: Annotated[
int,
Field(
...,
desciption="Count of items into the system.\n Replaces the total field which is deprecated",
validation_alias=AliasChoices("count", "total"),
),
]
@computed_field()
@property
def total(self) -> int:
return self.count
@total.setter
def total(self, value: int):
self.count = value
p = Paginated(count=10)
print(p.model_dump_json())
>> {"count":10,"total":10}
p = Paginated(total=20)
print(p.model_dump_json())
>> {"count":20,"total":20}
p.total = 30
print(p.model_dump_json())
>> {"count":30,"total":30}
print(json.dumps(p.model_json_schema(), indent=2))
>> {
"properties": {
"count": {
"desciption": "Count of items into the system.\n Replaces the total field which is deprecated",
"title": "Count",
"type": "integer"
}
},
"required": [
"count"
],
"title": "Paginated",
"type": "object"
}
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.
I've modified fastapi to ensure consistency between the two declarations in this template
A specialized verion of the PagedCollection from odoo.addons.fastapi.schemas but supporting extendable models as type a args.
dad0fb4
to
4b196ef
Compare
@qgroulard @AnizR What do you think about this PR? |
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.
Great!
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.
Looks good.
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.
LTGM, tested
/ocabot merge minor |
On my way to merge this fine PR! |
Congratulations, your PR was merged at e9b65cf. Thanks a lot for contributing to OCA. ❤️ |
Add base schemas to be used to define pagination and paginated results when defining a search router.