|
12 | 12 |
|
13 | 13 | @with_generated_client_fixture(
|
14 | 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 |
| - |
| 15 | +# Various use cases for oneOf |
38 | 16 |
|
39 |
| -@with_generated_client_fixture( |
40 |
| -""" |
41 | 17 | components:
|
42 | 18 | schemas:
|
43 | 19 | ThingA:
|
@@ -154,6 +130,123 @@ def test_type_hints(self, ModelWithUnion, ModelWithRequiredUnion, ModelWithUnion
|
154 | 130 |
|
155 | 131 | @with_generated_client_fixture(
|
156 | 132 | """
|
| 133 | +# Various use cases for a oneOf where one of the variants is null, since these are handled |
| 134 | +# a bit differently in the generator |
| 135 | +
|
| 136 | +components: |
| 137 | + schemas: |
| 138 | + MyEnum: |
| 139 | + type: string |
| 140 | + enum: ["a", "b"] |
| 141 | + MyObject: |
| 142 | + type: object |
| 143 | + properties: |
| 144 | + name: |
| 145 | + type: string |
| 146 | + MyModel: |
| 147 | + properties: |
| 148 | + nullableEnumProp: |
| 149 | + oneOf: |
| 150 | + - {"$ref": "#/components/schemas/MyEnum"} |
| 151 | + - type: "null" |
| 152 | + nullableObjectProp: |
| 153 | + oneOf: |
| 154 | + - {"$ref": "#/components/schemas/MyObject"} |
| 155 | + - type: "null" |
| 156 | + inlineNullableObject: |
| 157 | + # Note, the generated class for this should be called "MyModelInlineNullableObject", |
| 158 | + # since the generator's rule for inline schemas that require their own class is to |
| 159 | + # concatenate the property name to the parent schema name. |
| 160 | + oneOf: |
| 161 | + - type: object |
| 162 | + properties: |
| 163 | + name: |
| 164 | + type: string |
| 165 | + - type: "null" |
| 166 | +""") |
| 167 | +@with_generated_code_imports( |
| 168 | + ".models.MyEnum", |
| 169 | + ".models.MyObject", |
| 170 | + ".models.MyModel", |
| 171 | + ".models.MyModelInlineNullableObject", |
| 172 | + ".types.Unset", |
| 173 | +) |
| 174 | +class TestUnionsWithNull: |
| 175 | + def test_nullable_enum_prop(self, MyModel, MyEnum): |
| 176 | + assert_model_decode_encode(MyModel, {"nullableEnumProp": "b"}, MyModel(nullable_enum_prop=MyEnum.B)) |
| 177 | + assert_model_decode_encode(MyModel, {"nullableEnumProp": None}, MyModel(nullable_enum_prop=None)) |
| 178 | + |
| 179 | + def test_nullable_object_prop(self, MyModel, MyObject): |
| 180 | + assert_model_decode_encode( MyModel, {"nullableObjectProp": None}, MyModel(nullable_object_prop=None)) |
| 181 | + assert_model_decode_encode( MyModel, {"nullableObjectProp": None}, MyModel(nullable_object_prop=None)) |
| 182 | + |
| 183 | + def test_nullable_object_prop_with_inline_schema(self, MyModel, MyModelInlineNullableObject): |
| 184 | + assert_model_decode_encode( |
| 185 | + MyModel, |
| 186 | + {"inlineNullableObject": {"name": "a"}}, |
| 187 | + MyModel(inline_nullable_object=MyModelInlineNullableObject(name="a")), |
| 188 | + ) |
| 189 | + assert_model_decode_encode( MyModel, {"inlineNullableObject": None}, MyModel(inline_nullable_object=None)) |
| 190 | + |
| 191 | + def test_type_hints(self, MyModel, MyEnum, Unset): |
| 192 | + assert_model_property_type_hint(MyModel, "nullable_enum_prop", Union[MyEnum, None, Unset]) |
| 193 | + assert_model_property_type_hint(MyModel, "nullable_object_prop", Union[ForwardRef("MyObject"), None, Unset]) |
| 194 | + assert_model_property_type_hint( |
| 195 | + MyModel, |
| 196 | + "inline_nullable_object", |
| 197 | + Union[ForwardRef("MyModelInlineNullableObject"), None, Unset], |
| 198 | + ) |
| 199 | + |
| 200 | + |
| 201 | +@with_generated_client_fixture( |
| 202 | +""" |
| 203 | +# Tests for combining the OpenAPI 3.0 "nullable" attribute with an enum |
| 204 | +
|
| 205 | +openapi: 3.0.0 |
| 206 | +
|
| 207 | +components: |
| 208 | + schemas: |
| 209 | + MyEnum: |
| 210 | + type: string |
| 211 | + enum: ["a", "b"] |
| 212 | + MyEnumIncludingNull: |
| 213 | + type: string |
| 214 | + nullable: true |
| 215 | + enum: ["a", "b", null] |
| 216 | + MyModel: |
| 217 | + properties: |
| 218 | + nullableEnumProp: |
| 219 | + allOf: |
| 220 | + - {"$ref": "#/components/schemas/MyEnum"} |
| 221 | + nullable: true |
| 222 | + enumIncludingNullProp: {"$ref": "#/components/schemas/MyEnumIncludingNull"} |
| 223 | +""") |
| 224 | +@with_generated_code_imports( |
| 225 | + ".models.MyEnum", |
| 226 | + ".models.MyEnumIncludingNull", |
| 227 | + ".models.MyModel", |
| 228 | + ".types.Unset", |
| 229 | +) |
| 230 | +class TestNullableEnumsInOpenAPI30: |
| 231 | + def test_nullable_enum_prop(self, MyModel, MyEnum, MyEnumIncludingNull): |
| 232 | + assert_model_decode_encode(MyModel, {"nullableEnumProp": "b"}, MyModel(nullable_enum_prop=MyEnum.B)) |
| 233 | + assert_model_decode_encode(MyModel, {"nullableEnumProp": None}, MyModel(nullable_enum_prop=None)) |
| 234 | + assert_model_decode_encode( |
| 235 | + MyModel, |
| 236 | + {"enumIncludingNullProp": "a"}, |
| 237 | + MyModel(enum_including_null_prop=MyEnumIncludingNull.A), |
| 238 | + ) |
| 239 | + assert_model_decode_encode( MyModel, {"enumIncludingNullProp": None}, MyModel(enum_including_null_prop=None)) |
| 240 | + |
| 241 | + def test_type_hints(self, MyModel, MyEnum, MyEnumIncludingNull, Unset): |
| 242 | + assert_model_property_type_hint(MyModel, "nullable_enum_prop", Union[MyEnum, None, Unset]) |
| 243 | + assert_model_property_type_hint(MyModel, "enum_including_null_prop", Union[MyEnumIncludingNull, None, Unset]) |
| 244 | + |
| 245 | + |
| 246 | +@with_generated_client_fixture( |
| 247 | +""" |
| 248 | +# Tests for using a discriminator property |
| 249 | +
|
157 | 250 | components:
|
158 | 251 | schemas:
|
159 | 252 | ModelType1:
|
@@ -304,3 +397,112 @@ def test_nested_with_different_property(self, ModelType1, Schnauzer, WithNestedD
|
304 | 397 | {"unionProp": {"modelType": "irrelevant", "dogType": "Schnauzer", "name": "a"}},
|
305 | 398 | WithNestedDiscriminatorsDifferentProperty(union_prop=Schnauzer(model_type="irrelevant", dog_type="Schnauzer", name="a")),
|
306 | 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