Skip to content

Commit b89fbf4

Browse files
committed
Fix json_encoders warning
Signed-off-by: Madhav Kandukuri <[email protected]>
1 parent 99648a8 commit b89fbf4

File tree

2 files changed

+12
-58
lines changed

2 files changed

+12
-58
lines changed

mcpgateway/schemas.py

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@
4242
model_validator,
4343
root_validator,
4444
validator,
45-
ConfigDict
45+
ConfigDict,
46+
field_serializer
4647
)
4748

4849
logger = logging.getLogger(__name__)
@@ -96,36 +97,11 @@ class BaseModelWithConfigDict(BaseModel):
9697
from_attributes=True,
9798
alias_generator=to_camel_case,
9899
populate_by_name=True,
99-
json_encoders={datetime: encode_datetime},
100100
use_enum_values=True,
101101
extra="ignore",
102102
json_schema_extra={"nullable": True},
103103
)
104104

105-
# class Config:
106-
# """
107-
# A configuration class that provides default behaviors for how to handle serialization,
108-
# alias generation, enum values, and extra fields when working with models.
109-
110-
# Attributes:
111-
# from_attributes (bool): Flag to indicate if attributes should be taken from model fields.
112-
# alias_generator (callable): Function used to generate aliases for field names (e.g., converting to camelCase).
113-
# populate_by_name (bool): Flag to specify whether to populate fields by name during initialization.
114-
# json_encoders (dict): Custom JSON encoders for specific types, such as datetime encoding.
115-
# use_enum_values (bool): Flag to determine if enum values should be serialized or the enum type itself.
116-
# extra (str): Defines behavior for extra fields in models. The "ignore" option means extra fields are ignored.
117-
# json_schema_extra (dict): Additional schema information, e.g., specifying that fields can be nullable.
118-
119-
# """
120-
121-
# from_attributes = True
122-
# alias_generator = to_camel_case
123-
# populate_by_name = True
124-
# json_encoders = {datetime: encode_datetime}
125-
# use_enum_values = True
126-
# extra = "ignore"
127-
# json_schema_extra = {"nullable": True}
128-
129105
def to_dict(self, use_alias: bool = False) -> Dict[str, Any]:
130106
"""
131107
Converts the model instance into a dictionary representation.
@@ -548,6 +524,11 @@ class ResourceNotification(BaseModelWithConfigDict):
548524
content: ResourceContent
549525
timestamp: datetime = Field(default_factory=lambda: datetime.now(timezone.utc))
550526

527+
@field_serializer("timestamp")
528+
def serialize_timestamp(self, dt: datetime) -> str:
529+
# now returns ISO string with Z
530+
return dt.astimezone(timezone.utc).isoformat().replace("+00:00", "Z")
531+
551532

552533
# --- Prompt Schemas ---
553534

@@ -1027,6 +1008,11 @@ class EventMessage(BaseModelWithConfigDict):
10271008
data: Dict[str, Any] = Field(..., description="Event payload")
10281009
timestamp: datetime = Field(default_factory=lambda: datetime.now(timezone.utc))
10291010

1011+
@field_serializer("timestamp")
1012+
def serialize_timestamp(self, dt: datetime) -> str:
1013+
# now returns ISO string with Z
1014+
return dt.astimezone(timezone.utc).isoformat().replace("+00:00", "Z")
1015+
10301016

10311017
class AdminToolCreate(BaseModelWithConfigDict):
10321018
"""Schema for creating tools via admin UI.

mcpgateway/types.py

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -260,20 +260,8 @@ class InitializeRequest(BaseModel):
260260

261261
model_config = ConfigDict(
262262
populate_by_name=True,
263-
allow_population_by_field_name=True,
264263
)
265264

266-
# class Config:
267-
# """Configuration for InitializeRequest.
268-
269-
# Attributes:
270-
# populate_by_name (bool): Enables population by field name.
271-
# allow_population_by_field_name (bool): Allows backward compatibility with older Pydantic versions.
272-
# """
273-
274-
# populate_by_name = True
275-
# allow_population_by_field_name = True # Use this for backward compatibility with older Pydantic versions
276-
277265

278266
class InitializeResult(BaseModel):
279267
"""Server's response to the initialization request.
@@ -292,20 +280,8 @@ class InitializeResult(BaseModel):
292280

293281
model_config = ConfigDict(
294282
populate_by_name=True,
295-
allow_population_by_field_name=True,
296283
)
297284

298-
# class Config:
299-
# """
300-
# Configuration class for Pydantic models.
301-
302-
# Enables population of model fields by name and by field name.
303-
# """
304-
305-
# populate_by_name = True
306-
# allow_population_by_field_name = True
307-
308-
309285
# Message types
310286
class Message(BaseModel):
311287
"""A message in a conversation.
@@ -486,16 +462,8 @@ class ListResourceTemplatesResult(BaseModel):
486462

487463
model_config = ConfigDict(
488464
populate_by_name=True,
489-
allow_population_by_field_name=True,
490465
)
491466

492-
# class Config:
493-
# """Configuration for model serialization."""
494-
495-
# populate_by_name = True
496-
# allow_population_by_field_name = True
497-
498-
499467
# Root types
500468
class FileUrl(AnyUrl):
501469
"""A specialized URL type for local file-scheme resources.

0 commit comments

Comments
 (0)