You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm trying to generate a Pydantic v2 model and then import it. The OpenAPI spec includes some objects in the schema that have patterned fields with patterns that are unsupported by Pydantic v2. Here's an example of a field from a class generated from that spec
beneficial_owner_id: Annotated[
Optional[str],
Field(
description='The ID associated to be with the beneficial owner for future requests. If not provided on creation, Canopy will generate this field.',
example='0x1234',
pattern='^(?!can_).*$',
),
Then import fails when Pydantic tries to create the validator
(venv) morgan@LAPTOP-O1G4SPR0:~/d/lab/python/codegen$ python -c "import model.py"
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/home/morgan/d/lab/python/codegen/model.py", line 4092, in <module>
class BeneficialOwner(BaseModel):
File "/home/morgan/d/lab/python/codegen/venv/lib/python3.10/site-packages/pydantic/_internal/_model_construction.py", line 184, in __new__
complete_model_class(
File "/home/morgan/d/lab/python/codegen/venv/lib/python3.10/site-packages/pydantic/_internal/_model_construction.py", line 499, in complete_model_class
cls.__pydantic_validator__ = create_schema_validator(schema, core_config, config_wrapper.plugin_settings)
File "/home/morgan/d/lab/python/codegen/venv/lib/python3.10/site-packages/pydantic/plugin/_schema_validator.py", line 34, in create_schema_validator
return SchemaValidator(schema, config)
pydantic_core._pydantic_core.SchemaError: Error building "model" validator:
SchemaError: Error building "model-fields" validator:
SchemaError: Field "beneficial_owner_id":
SchemaError: Error building "default" validator:
SchemaError: Error building "nullable" validator:
SchemaError: Error building "str" validator:
SchemaError: regex parse error:
^(?!can_).*$
^^^
Do I just need to strip out the pattern? Is there a command option that can do that? Should this be submitted as an issue, the fact the spec is converted even though some fields are invalid (according to Pydantic v2)?
Here's the command used to generate the models datamodel-codegen --url https://docs.canopyservicing.com/openapi/645bb7f0ea9f151255ed1f70 --output model.py --output-model-type pydantic_v2.BaseModel --use-annotated
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I'm trying to generate a Pydantic v2 model and then import it. The OpenAPI spec includes some objects in the schema that have patterned fields with patterns that are unsupported by Pydantic v2. Here's an example of a field from a class generated from that spec
Then import fails when Pydantic tries to create the validator
Do I just need to strip out the pattern? Is there a command option that can do that? Should this be submitted as an issue, the fact the spec is converted even though some fields are invalid (according to Pydantic v2)?
Here's the command used to generate the models
datamodel-codegen --url https://docs.canopyservicing.com/openapi/645bb7f0ea9f151255ed1f70 --output model.py --output-model-type pydantic_v2.BaseModel --use-annotated
Beta Was this translation helpful? Give feedback.
All reactions