-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature/mx-1417 fix temporal serialization (#273)
# PR Context - fixes robert-koch-institut/mex-backend#106 (review) # Changes - harmonize signatures/docs of pydantic core/json schema manipulating methods # Fixed - fix schema tests not starting with diverging model names in common and mex-model - fix serialization for temporal entity instances within pydantic models --------- Signed-off-by: Nicolas Drebenstedt <[email protected]>
- Loading branch information
1 parent
d33ce89
commit 5234dc6
Showing
8 changed files
with
73 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
from typing import Any | ||
|
||
from pydantic import GetJsonSchemaHandler | ||
from pydantic.json_schema import JsonSchemaValue | ||
from pydantic import GetJsonSchemaHandler, json_schema | ||
from pydantic_core import core_schema | ||
|
||
EMAIL_PATTERN = r"^[^@ \t\r\n]+@[^@ \t\r\n]+\.[^@ \t\r\n]+$" | ||
|
@@ -11,17 +10,17 @@ class Email(str): | |
"""Email address of a person, organization or other entity.""" | ||
|
||
@classmethod | ||
def __get_pydantic_core_schema__(cls, _source: type[Any]) -> core_schema.CoreSchema: | ||
"""Get pydantic core schema.""" | ||
def __get_pydantic_core_schema__(cls, source: type[Any]) -> core_schema.CoreSchema: | ||
"""Modify the core schema to add the email regex.""" | ||
return core_schema.str_schema(pattern=EMAIL_PATTERN) | ||
|
||
@classmethod | ||
def __get_pydantic_json_schema__( | ||
cls, core_schema_: core_schema.CoreSchema, handler: GetJsonSchemaHandler | ||
) -> JsonSchemaValue: | ||
"""Add title and format.""" | ||
field_schema = handler(core_schema_) | ||
field_schema["title"] = cls.__name__ | ||
field_schema["format"] = "email" | ||
field_schema["examples"] = ["[email protected]"] | ||
return field_schema | ||
) -> json_schema.JsonSchemaValue: | ||
"""Modify the json schema to add a title, format and examples.""" | ||
json_schema_ = handler(core_schema_) | ||
json_schema_["title"] = cls.__name__ | ||
json_schema_["format"] = "email" | ||
json_schema_["examples"] = ["[email protected]"] | ||
return json_schema_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters