-
-
Notifications
You must be signed in to change notification settings - Fork 304
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
Wrong parent class for pydantic V2 root models #1435
Comments
@andreyLetenkovWefox |
Sorry, I thought the problem could be reproduced via a CLI generator call, but it really only appears when using the python package: from pathlib import Path
from datamodel_code_generator import DataModelType, generate
generate(
input_=Path("custom_id.yaml"),
output=Path("model.py"),
output_model_type=DataModelType.PydanticV2BaseModel,
) |
Signed-off-by: Etienne Wodey <etienne.wodey@aqt.eu>
We hit this issue in a slightly different setting: if I drafted a fix in #1448 but it still needs quite some work (mostly because it only works with Pydantic v2 at the moment, and relies on using a @koxudaxi would you like to see further work in the direction of #1448? or do you have a simpler idea for the fix? It seems that the handling of |
I was also running into this issue, but not in the same way as @andreyLetenkovWefox. Apparently, if you set the See the following example:
datamodel-codegen --input custom_id.yaml --base-class "utils.dto_class.BaseDtoModel" --output-model-type pydantic_v2.BaseModel This will output: from __future__ import annotations
from uuid import UUID
from pydantic import Field
from utils.dto_class import BaseDtoModel
class CustomId(BaseDtoModel):
root: UUID = Field(..., description='My custom ID') This, for Pydantic V2, is wrong as it will be interpreted as a normal model with a literal But here arises another question: Should |
Also noticed the difference like @barreeeiroo mentioned. I think it's a problem for us now. |
Hey everyone, I'm sorry that the v2 root models have problems. |
Should we add the base_class option for the RootModel in v2? 🤔 |
That's the big question... In my opinion, we shouldn't. Think about |
I agree with you. |
* main: fix #1435 for pydantic v2 only Signed-off-by: Etienne Wodey <etienne.wodey@aqt.eu> * Fix for v1 * Remove unused code * Add unittest * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Fix unittest --------- Signed-off-by: Etienne Wodey <etienne.wodey@aqt.eu> Co-authored-by: Koudai Aono <koxudaxi@gmail.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Describe the bug
Generator uses
pydantic.BaseModel
as parent class for root model instead ofpydantic.RootModel
Example schema (
custom_id.yaml
):Used commandline:
Contents of
model.py
:Expected behavior
Version:
The text was updated successfully, but these errors were encountered: