Skip to content

Commit d1aacf2

Browse files
committed
fix(tool): handle empty additionalProperties in schema
Updated ToolSchema to correctly handle cases where additionalProperties is an empty schema, ensuring that the original semantics are preserved. Added unit tests to verify this behavior. 修复(tool):处理 schema 中的空 additionalProperties 更新 ToolSchema,以正确处理 additionalProperties 为空 schema 的情况,确保保留原语义。添加单元测试以验证此行为。 Change-Id: Idf4c5fd6a530da24a9a691765c44c61be7831713 Signed-off-by: OhYee <oyohyee@oyohyee.com>
1 parent e1dc4e2 commit d1aacf2

3 files changed

Lines changed: 55 additions & 3 deletions

File tree

agentrun/toolset/model.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,9 +181,13 @@ def from_any_openapi_schema(cls, schema: Any) -> "ToolSchema":
181181

182182
additional_properties_raw = pg(schema, "additionalProperties")
183183
additional_properties = (
184-
cls.from_any_openapi_schema(additional_properties_raw)
185-
if isinstance(additional_properties_raw, dict)
186-
else additional_properties_raw
184+
cls()
185+
if additional_properties_raw == {}
186+
else (
187+
cls.from_any_openapi_schema(additional_properties_raw)
188+
if isinstance(additional_properties_raw, dict)
189+
else additional_properties_raw
190+
)
187191
)
188192

189193
return cls(

tests/unittests/tool/test_model.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,30 @@ def test_from_any_openapi_schema_additional_properties_schema(self):
377377
== "integer"
378378
)
379379

380+
def test_from_any_openapi_schema_empty_additional_properties_schema(self):
381+
"""测试 additionalProperties 为空 schema 时保留原语义"""
382+
openapi_schema = {
383+
"type": "object",
384+
"properties": {
385+
"metadata": {
386+
"type": "object",
387+
"additionalProperties": {},
388+
}
389+
},
390+
}
391+
392+
schema = ToolSchema.from_any_openapi_schema(openapi_schema)
393+
394+
assert schema.properties is not None
395+
metadata_schema = schema.properties["metadata"]
396+
assert metadata_schema.additional_properties is not None
397+
assert metadata_schema.additional_properties.type is None
398+
399+
json_schema = schema.to_json_schema()
400+
assert (
401+
json_schema["properties"]["metadata"]["additionalProperties"] == {}
402+
)
403+
380404
def test_to_json_schema_simple(self):
381405
"""测试转换为 JSON Schema - 简单情况"""
382406
schema = ToolSchema(

tests/unittests/toolset/test_model.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,30 @@ def test_from_any_openapi_schema_with_schema_additional_properties(self):
487487
== "integer"
488488
)
489489

490+
def test_from_any_openapi_schema_with_empty_additional_properties(self):
491+
"""测试 additionalProperties 为空 schema 时保留原语义"""
492+
openapi_schema = {
493+
"type": "object",
494+
"properties": {
495+
"metadata": {
496+
"type": "object",
497+
"additionalProperties": {},
498+
}
499+
},
500+
}
501+
502+
schema = ToolSchema.from_any_openapi_schema(openapi_schema)
503+
504+
assert schema.properties is not None
505+
metadata_schema = schema.properties["metadata"]
506+
assert metadata_schema.additional_properties is not None
507+
assert metadata_schema.additional_properties.type is None
508+
509+
json_schema = schema.to_json_schema()
510+
assert (
511+
json_schema["properties"]["metadata"]["additionalProperties"] == {}
512+
)
513+
490514
def test_from_any_openapi_schema_with_items(self):
491515
"""测试从带 items 的数组 schema 创建"""
492516
openapi_schema = {

0 commit comments

Comments
 (0)