mykola-shyshov commented on issue #57726:
URL: https://github.com/apache/airflow/issues/57726#issuecomment-3506754633

   for mode=python user needs to wrap class. case for AnyUrl (pydantic )
   
   ```
   class UrlWrapper:
       anyurl: AnyUrl
   
       def __init__(self, url):
           self.anyurl = AnyUrl(url)
   
       def serialize(self):
           """Airflow will call this"""
           return {"url": str(self.anyurl)}
   
       @classmethod
       def deserialize(cls, data, version):
           """Airflow will call this"""
           return cls(data["url"])
   
   class MyModel(BaseModel):
       model_config = {"arbitrary_types_allowed": True}
       name: str
       url: UrlWrapper  # Use wrapper instead of AnyUrl directly
   ```
   
   for mode=json user needs to write model serializers and validators. case for 
ObjectStoragePath
   
   ```
   class PathModel(BaseModel):
       model_config = {"arbitrary_types_allowed": True}
       path: ObjectStoragePath
   
       @field_validator("path", mode="before")
       @classmethod
       def parse_path(cls, v):
           return ObjectStoragePath(v)
   
       @field_serializer("path")
       def dump_path(self, v: ObjectStoragePath):
           """Convert ObjectStoragePath to string for JSON serialization"""
           return str(v)
   ```


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to