Skip to content

Commit

Permalink
[#38] custom serialization of url field of Submission model
Browse files Browse the repository at this point in the history
  • Loading branch information
pkdash committed Oct 16, 2023
1 parent d761cae commit 0f61ba7
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions api/models/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from typing import List, Optional, TYPE_CHECKING

from beanie import Document, Link, PydanticObjectId
from pydantic import HttpUrl, model_validator
from pydantic import HttpUrl, model_validator, field_serializer

if TYPE_CHECKING:
# this avoids circular imports
Expand All @@ -19,11 +19,17 @@ class Submission(Document):
repository_identifier: Optional[str] = None

@model_validator(mode='after')
def validate_url(self):
def url_to_string(self):
if self.url is not None:
self.url = str(self.url)
return self

@field_serializer('url')
def serialize_url(self, v: str, _info):
if v is not None:
return HttpUrl(v)


class User(Document):
access_token: str
orcid: str
Expand Down

0 comments on commit 0f61ba7

Please sign in to comment.