|
10 | 10 | )
|
11 | 11 |
|
12 | 12 |
|
13 |
| -@with_generated_client_fixture( |
14 |
| -""" |
15 |
| -components: |
16 |
| - schemas: |
17 |
| - StringOrInt: |
18 |
| - type: ["string", "integer"] |
19 |
| - MyModel: |
20 |
| - type: object |
21 |
| - properties: |
22 |
| - stringOrIntProp: |
23 |
| - type: ["string", "integer"] |
24 |
| -""" |
25 |
| -) |
26 |
| -@with_generated_code_imports( |
27 |
| - ".models.MyModel", |
28 |
| - ".types.Unset" |
29 |
| -) |
30 |
| -class TestSimpleTypeList: |
31 |
| - def test_decode_encode(self, MyModel): |
32 |
| - assert_model_decode_encode(MyModel, {"stringOrIntProp": "a"}, MyModel(string_or_int_prop="a")) |
33 |
| - assert_model_decode_encode(MyModel, {"stringOrIntProp": 1}, MyModel(string_or_int_prop=1)) |
34 |
| - |
35 |
| - def test_type_hints(self, MyModel, Unset): |
36 |
| - assert_model_property_type_hint(MyModel, "string_or_int_prop", Union[str, int, Unset]) |
37 |
| - |
38 |
| - |
39 | 13 | @with_generated_client_fixture(
|
40 | 14 | """
|
41 | 15 | # Various use cases for oneOf
|
@@ -271,88 +245,8 @@ def test_type_hints(self, MyModel, MyEnum, MyEnumIncludingNull, Unset):
|
271 | 245 |
|
272 | 246 | @with_generated_client_fixture(
|
273 | 247 | """
|
274 |
| -# Test use cases where there's a union of types *and* an explicit list of multiple "type:"s |
| 248 | +# Tests for using a discriminator property |
275 | 249 |
|
276 |
| -components: |
277 |
| - schemas: |
278 |
| - MyStringEnum: |
279 |
| - type: string |
280 |
| - enum: ["a", "b"] |
281 |
| - MyIntEnum: |
282 |
| - type: integer |
283 |
| - enum: [1, 2] |
284 |
| - MyEnumIncludingNull: |
285 |
| - type: ["string", "null"] |
286 |
| - enum: ["a", "b", null] |
287 |
| - MyObject: |
288 |
| - type: object |
289 |
| - properties: |
290 |
| - name: |
291 |
| - type: string |
292 |
| - MyModel: |
293 |
| - properties: |
294 |
| - enumsWithListOfTypesProp: |
295 |
| - type: ["string", "integer"] |
296 |
| - oneOf: |
297 |
| - - {"$ref": "#/components/schemas/MyStringEnum"} |
298 |
| - - {"$ref": "#/components/schemas/MyIntEnum"} |
299 |
| - enumIncludingNullProp: {"$ref": "#/components/schemas/MyEnumIncludingNull"} |
300 |
| - nullableObjectWithListOfTypesProp: |
301 |
| - type: ["string", "object"] |
302 |
| - oneOf: |
303 |
| - - {"$ref": "#/components/schemas/MyObject"} |
304 |
| - - type: "null" |
305 |
| -""") |
306 |
| -@with_generated_code_imports( |
307 |
| - ".models.MyStringEnum", |
308 |
| - ".models.MyIntEnum", |
309 |
| - ".models.MyEnumIncludingNull", |
310 |
| - ".models.MyObject", |
311 |
| - ".models.MyModel", |
312 |
| - ".types.Unset", |
313 |
| -) |
314 |
| -class TestUnionsWithExplicitListOfTypes: |
315 |
| - # This covers some use cases where combining "oneOf" with "type: [list of types]" (which is fine |
316 |
| - # to do in OpenAPI) used to generate enum/model classes incorrectly. |
317 |
| - |
318 |
| - def test_union_of_enums(self, MyModel, MyStringEnum, MyIntEnum): |
319 |
| - assert_model_decode_encode( |
320 |
| - MyModel, |
321 |
| - {"enumsWithListOfTypesProp": "b"}, |
322 |
| - MyModel(enums_with_list_of_types_prop=MyStringEnum.B), |
323 |
| - ) |
324 |
| - assert_model_decode_encode( |
325 |
| - MyModel, |
326 |
| - {"enumsWithListOfTypesProp": 2}, |
327 |
| - MyModel(enums_with_list_of_types_prop=MyIntEnum.VALUE_2), |
328 |
| - ) |
329 |
| - |
330 |
| - def test_union_of_enum_with_null(self, MyModel, MyEnumIncludingNull): |
331 |
| - assert_model_decode_encode( |
332 |
| - MyModel, |
333 |
| - {"enumIncludingNullProp": "b"}, |
334 |
| - MyModel(enum_including_null_prop=MyEnumIncludingNull.B), |
335 |
| - ) |
336 |
| - assert_model_decode_encode( |
337 |
| - MyModel, |
338 |
| - {"enumIncludingNullProp": None}, |
339 |
| - MyModel(enum_including_null_prop=None), |
340 |
| - ) |
341 |
| - |
342 |
| - def test_nullable_object_with_list_of_types(self, MyModel, MyObject): |
343 |
| - assert_model_decode_encode( |
344 |
| - MyModel, |
345 |
| - {"nullableObjectWithListOfTypesProp": {"name": "a"}}, |
346 |
| - MyModel(nullable_object_with_list_of_types_prop=MyObject(name="a")), |
347 |
| - ) |
348 |
| - assert_model_decode_encode( |
349 |
| - MyModel, |
350 |
| - {"nullableObjectWithListOfTypesProp": None}, |
351 |
| - MyModel(nullable_object_with_list_of_types_prop=None), |
352 |
| - ) |
353 |
| - |
354 |
| -@with_generated_client_fixture( |
355 |
| -""" |
356 | 250 | components:
|
357 | 251 | schemas:
|
358 | 252 | ModelType1:
|
@@ -503,3 +397,112 @@ def test_nested_with_different_property(self, ModelType1, Schnauzer, WithNestedD
|
503 | 397 | {"unionProp": {"modelType": "irrelevant", "dogType": "Schnauzer", "name": "a"}},
|
504 | 398 | WithNestedDiscriminatorsDifferentProperty(union_prop=Schnauzer(model_type="irrelevant", dog_type="Schnauzer", name="a")),
|
505 | 399 | )
|
| 400 | + |
| 401 | + |
| 402 | +@with_generated_client_fixture( |
| 403 | +""" |
| 404 | +# Tests for using multiple values of "type:" in one schema (OpenAPI 3.1) |
| 405 | +
|
| 406 | +components: |
| 407 | + schemas: |
| 408 | + StringOrInt: |
| 409 | + type: ["string", "integer"] |
| 410 | + MyModel: |
| 411 | + type: object |
| 412 | + properties: |
| 413 | + stringOrIntProp: |
| 414 | + type: ["string", "integer"] |
| 415 | +""" |
| 416 | +) |
| 417 | +@with_generated_code_imports( |
| 418 | + ".models.MyModel", |
| 419 | + ".types.Unset" |
| 420 | +) |
| 421 | +class TestListOfSimpleTypes: |
| 422 | + def test_decode_encode(self, MyModel): |
| 423 | + assert_model_decode_encode(MyModel, {"stringOrIntProp": "a"}, MyModel(string_or_int_prop="a")) |
| 424 | + assert_model_decode_encode(MyModel, {"stringOrIntProp": 1}, MyModel(string_or_int_prop=1)) |
| 425 | + |
| 426 | + def test_type_hints(self, MyModel, Unset): |
| 427 | + assert_model_property_type_hint(MyModel, "string_or_int_prop", Union[str, int, Unset]) |
| 428 | + |
| 429 | + |
| 430 | +@with_generated_client_fixture( |
| 431 | +""" |
| 432 | +# Test cases where there's a union of types *and* an explicit list of multiple "type:"s - |
| 433 | +# there was a bug where this could cause enum/model classes to be generated incorrectly |
| 434 | +
|
| 435 | +components: |
| 436 | + schemas: |
| 437 | + MyStringEnum: |
| 438 | + type: string |
| 439 | + enum: ["a", "b"] |
| 440 | + MyIntEnum: |
| 441 | + type: integer |
| 442 | + enum: [1, 2] |
| 443 | + MyEnumIncludingNull: |
| 444 | + type: ["string", "null"] |
| 445 | + enum: ["a", "b", null] |
| 446 | + MyObject: |
| 447 | + type: object |
| 448 | + properties: |
| 449 | + name: |
| 450 | + type: string |
| 451 | + MyModel: |
| 452 | + properties: |
| 453 | + enumsWithListOfTypesProp: |
| 454 | + type: ["string", "integer"] |
| 455 | + oneOf: |
| 456 | + - {"$ref": "#/components/schemas/MyStringEnum"} |
| 457 | + - {"$ref": "#/components/schemas/MyIntEnum"} |
| 458 | + enumIncludingNullProp: {"$ref": "#/components/schemas/MyEnumIncludingNull"} |
| 459 | + nullableObjectWithListOfTypesProp: |
| 460 | + type: ["string", "object"] |
| 461 | + oneOf: |
| 462 | + - {"$ref": "#/components/schemas/MyObject"} |
| 463 | + - type: "null" |
| 464 | +""") |
| 465 | +@with_generated_code_imports( |
| 466 | + ".models.MyStringEnum", |
| 467 | + ".models.MyIntEnum", |
| 468 | + ".models.MyEnumIncludingNull", |
| 469 | + ".models.MyObject", |
| 470 | + ".models.MyModel", |
| 471 | + ".types.Unset", |
| 472 | +) |
| 473 | +class TestUnionsWithListOfSimpleTypes: |
| 474 | + def test_union_of_enums(self, MyModel, MyStringEnum, MyIntEnum): |
| 475 | + assert_model_decode_encode( |
| 476 | + MyModel, |
| 477 | + {"enumsWithListOfTypesProp": "b"}, |
| 478 | + MyModel(enums_with_list_of_types_prop=MyStringEnum.B), |
| 479 | + ) |
| 480 | + assert_model_decode_encode( |
| 481 | + MyModel, |
| 482 | + {"enumsWithListOfTypesProp": 2}, |
| 483 | + MyModel(enums_with_list_of_types_prop=MyIntEnum.VALUE_2), |
| 484 | + ) |
| 485 | + |
| 486 | + def test_union_of_enum_with_null(self, MyModel, MyEnumIncludingNull): |
| 487 | + assert_model_decode_encode( |
| 488 | + MyModel, |
| 489 | + {"enumIncludingNullProp": "b"}, |
| 490 | + MyModel(enum_including_null_prop=MyEnumIncludingNull.B), |
| 491 | + ) |
| 492 | + assert_model_decode_encode( |
| 493 | + MyModel, |
| 494 | + {"enumIncludingNullProp": None}, |
| 495 | + MyModel(enum_including_null_prop=None), |
| 496 | + ) |
| 497 | + |
| 498 | + def test_nullable_object_with_list_of_types(self, MyModel, MyObject): |
| 499 | + assert_model_decode_encode( |
| 500 | + MyModel, |
| 501 | + {"nullableObjectWithListOfTypesProp": {"name": "a"}}, |
| 502 | + MyModel(nullable_object_with_list_of_types_prop=MyObject(name="a")), |
| 503 | + ) |
| 504 | + assert_model_decode_encode( |
| 505 | + MyModel, |
| 506 | + {"nullableObjectWithListOfTypesProp": None}, |
| 507 | + MyModel(nullable_object_with_list_of_types_prop=None), |
| 508 | + ) |
0 commit comments