diff --git a/src/ome_types/_pydantic_compat.py b/src/ome_types/_pydantic_compat.py index 4f9d4bb7..1a27fdad 100644 --- a/src/ome_types/_pydantic_compat.py +++ b/src/ome_types/_pydantic_compat.py @@ -20,10 +20,13 @@ def field_type(field: FieldInfo) -> Any: def field_regex(obj: type[BaseModel], field_name: str) -> str | None: field_info = obj.model_fields[field_name] - if field_info.json_schema_extra and isinstance( - field_info.json_schema_extra, dict - ): - return field_info.json_schema_extra.get("pattern") # type: ignore + meta = field_info.json_schema_extra or {} + # if a "metadata" key exists... use it. + # After pydantic-compat 0.2, this is where it will be. + if "metadata" in meta: # type: ignore + meta = meta["metadata"] # type: ignore + if meta: + return meta.get("pattern") # type: ignore return None def get_default(f: FieldInfo) -> Any: diff --git a/src/xsdata_pydantic_basemodel/pydantic_compat.py b/src/xsdata_pydantic_basemodel/pydantic_compat.py index 138557a9..cf1c5816 100644 --- a/src/xsdata_pydantic_basemodel/pydantic_compat.py +++ b/src/xsdata_pydantic_basemodel/pydantic_compat.py @@ -21,11 +21,16 @@ from pydantic_core import PydanticUndefined as Undefined def _get_metadata(pydantic_field: FieldInfo) -> dict[str, Any]: - return ( + meta = ( pydantic_field.json_schema_extra if isinstance(pydantic_field.json_schema_extra, dict) else {} ) + # if a "metadata" key exists... use it. + # After pydantic-compat 0.2, this is where it will be. + if "metadata" in meta: + meta = meta["metadata"] # type: ignore + return meta else: from pydantic.fields import Undefined as Undefined # type: ignore