-
Notifications
You must be signed in to change notification settings - Fork 12
Description
Using the old specification parallel to Pydantic v2 produces warnings using models package.
Warning content:
.../.../python3.9/site-packages/pydantic/fields.py:804: PydanticDeprecatedSince20: Using extra keyword arguments on Fieldis deprecated and will be removed. Usejson_schema_extra instead. (Extra keys: 'example'). Deprecated in Pydantic V2.0 to be removed in V3.0. See Pydantic V2 Migration Guide at https://errors.pydantic.dev/2.7/migration/
Roots of warning - models package, for example:
class BaseObject(BaseModel):
oddrn: str = Field(..., example='//aws/glue/{account_id}/{database}/{tablename}')
name: str
version: Optional[str] = None
description: Optional[str] = None
owner: Optional[str] = Field(None, example='//aws/iam/{account_id}/user/name')
metadata: Optional[List[MetadataExtension]] = None
tags: Optional[List[Tag]] = None
according to pedantic v2 Field should not have example attribute. With Pydantic v3 it will be considered as error.
As our models are generated based on specification with use of datamodel-code-generator we are somewhat dependent on it.
Right now there is a way to modify the specification in the way Pydantic v2 will not throw errors, but with this approach OpenAPI (Swagger) used for documentation will not correctly understand the examples.
- Old specification (Pydantic throws errors, documentation is OK):
BaseObject:
type: object
properties:
oddrn:
type: string
example: "//aws/glue/{account_id}/{database}/{tablename}"
- Version where Pydantic will not throw errors, but will be problems displaying examples in documentation:
BaseObject:
type: object
properties:
oddrn:
type: string
json_schema_extra:
examples:
- "//aws/glue/{account_id}/{database}/{tablename}"
Invocation command for datamodel-code-generator:
datamodel-codegen --input ./opendatadiscovery-specification/specification/entities.yaml --output odd_models/models/models.py --input-file-type openapi --output-model-type pydantic_v2.BaseModel --field-extra-keys json_schema_extra
Maybe with later versions of datamodel-code-generator this will be fixed.