|
1 | 1 | from typing import Annotated
|
2 | 2 |
|
3 | 3 | import annotated_types
|
| 4 | +import json |
4 | 5 | import pytest
|
5 | 6 | from pydantic import BaseModel, Field
|
6 | 7 |
|
@@ -235,8 +236,48 @@ async def check_call(args):
|
235 | 236 |
|
236 | 237 |
|
237 | 238 | def test_complex_function_json_schema():
|
| 239 | + """Test JSON schema generation for complex function arguments. |
| 240 | + |
| 241 | + Note: This test accepts two equivalent JSON Schema formats for models with defaults: |
| 242 | + 1. Pre-pydantic 2.7.2: |
| 243 | + { |
| 244 | + "$ref": "#/$defs/Model", |
| 245 | + "default": {} |
| 246 | + } |
| 247 | + |
| 248 | + 2. Pydantic 2.7.2+: |
| 249 | + { |
| 250 | + "allOf": [ |
| 251 | + { |
| 252 | + "$ref": "#/$defs/Model" |
| 253 | + } |
| 254 | + ], |
| 255 | + "default": {} |
| 256 | + } |
| 257 | + |
| 258 | + Both formats are valid JSON Schema and represent the same validation rules. |
| 259 | + The newer format using allOf is more correct according to the JSON Schema spec |
| 260 | + as it properly composes the reference with additional properties. |
| 261 | + |
| 262 | + This change in format does not affect runtime behavior since: |
| 263 | + 1. Both schemas validate the same way |
| 264 | + 2. The actual model classes and validation logic are unchanged |
| 265 | + 3. func_metadata uses model_validate/model_dump, not the schema directly |
| 266 | + """ |
238 | 267 | meta = func_metadata(complex_arguments_fn)
|
239 |
| - assert meta.arg_model.model_json_schema() == { |
| 268 | + actual_schema = meta.arg_model.model_json_schema() |
| 269 | + |
| 270 | + # Create a copy of the actual schema to normalize |
| 271 | + normalized_schema = actual_schema.copy() |
| 272 | + |
| 273 | + # Normalize the my_model_a_with_default field to handle both pydantic formats |
| 274 | + if 'allOf' in actual_schema['properties']['my_model_a_with_default']: |
| 275 | + normalized_schema['properties']['my_model_a_with_default'] = { |
| 276 | + '$ref': '#/$defs/SomeInputModelA', |
| 277 | + 'default': {} |
| 278 | + } |
| 279 | + |
| 280 | + assert normalized_schema == { |
240 | 281 | "$defs": {
|
241 | 282 | "InnerModel": {
|
242 | 283 | "properties": {"x": {"title": "X", "type": "integer"}},
|
|
0 commit comments