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

pydantic_v2 base class does not use the RootModel #2234

Open
amarion35 opened this issue Dec 18, 2024 · 0 comments
Open

pydantic_v2 base class does not use the RootModel #2234

amarion35 opened this issue Dec 18, 2024 · 0 comments

Comments

@amarion35
Copy link

Describe the bug
I want to generate models from this schema https://vega.github.io/schema/vega-lite/v5.json. I want to use pydantic v2 but the generated code is incorrect.

Firstly, the BaseModel is imported like this : from pydantic_v2 import BaseModel
Secondly, it uses the BaseModel but I expect it to use the RootModel.

To Reproduce

Example schema:
https://vega.github.io/schema/vega-lite/v5.json

Used commandline:

datamodel-codegen \
    --input-file-type jsonschema \
    --input v5.json \
    --output ./vegalite_spec.py \
    --target-python-version 3.11 \
    --base-class pydantic_v2.BaseModel \
    --use-standard-collections \
    --use-union-operator \
    --use-double-quotes

Observed behavior

# generated by datamodel-codegen:
#   filename:  v5.json
#   timestamp: 2024-12-18T16:56:18+00:00

from __future__ import annotations

from enum import Enum
from typing import Any

from pydantic import AnyUrl, Extra, Field, confloat, constr
from pydantic_v2 import BaseModel
...

Expected behavior
The import statements should be a valid import statement.
It should use RootModel instead of BaseModel.

# generated by datamodel-codegen:
#   filename:  v5.json
#   timestamp: 2024-09-30T08:54:36+00:00

from __future__ import annotations

from enum import Enum
from typing import Any, Literal

from pydantic import AnyUrl, BaseModel, ConfigDict, Field, RootModel
...

Version:

  • OS: Ubuntu 20.04
  • Python version: 3.11.6
  • datamodel-code-generator version: 0.26.4

Additional context
I managed to generate the correct model a few month ago but I lost the exact command I used. So I have the expected code.

Related Issues
#2201
#1435

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant