|
| 1 | +import datetime |
| 2 | +from typing import Any, ForwardRef, List, Union |
| 3 | +import uuid |
| 4 | +import pytest |
| 5 | +from end_to_end_tests.end_to_end_test_helpers import ( |
| 6 | + assert_model_decode_encode, |
| 7 | + assert_model_property_type_hint, |
| 8 | + with_generated_client_fixture, |
| 9 | + with_generated_code_imports, |
| 10 | +) |
| 11 | + |
| 12 | + |
| 13 | +@with_generated_client_fixture( |
| 14 | +""" |
| 15 | +components: |
| 16 | + schemas: |
| 17 | + SimpleObject: |
| 18 | + type: object |
| 19 | + properties: |
| 20 | + name: {"type": "string"} |
| 21 | + ModelWithArrayOfAny: |
| 22 | + properties: |
| 23 | + arrayProp: |
| 24 | + type: array |
| 25 | + items: {} |
| 26 | + ModelWithArrayOfInts: |
| 27 | + properties: |
| 28 | + arrayProp: |
| 29 | + type: array |
| 30 | + items: {"type": "integer"} |
| 31 | + ModelWithArrayOfObjects: |
| 32 | + properties: |
| 33 | + arrayProp: |
| 34 | + type: array |
| 35 | + items: {"$ref": "#/components/schemas/SimpleObject"} |
| 36 | +""") |
| 37 | +@with_generated_code_imports( |
| 38 | + ".models.ModelWithArrayOfAny", |
| 39 | + ".models.ModelWithArrayOfInts", |
| 40 | + ".models.ModelWithArrayOfObjects", |
| 41 | + ".models.SimpleObject", |
| 42 | + ".types.Unset", |
| 43 | +) |
| 44 | +class TestArraySchemas: |
| 45 | + def test_array_of_any(self, ModelWithArrayOfAny): |
| 46 | + assert_model_decode_encode( |
| 47 | + ModelWithArrayOfAny, |
| 48 | + {"arrayProp": ["a", 1]}, |
| 49 | + ModelWithArrayOfAny(array_prop=["a", 1]), |
| 50 | + ) |
| 51 | + |
| 52 | + def test_array_of_int(self, ModelWithArrayOfInts): |
| 53 | + assert_model_decode_encode( |
| 54 | + ModelWithArrayOfInts, |
| 55 | + {"arrayProp": [1, 2]}, |
| 56 | + ModelWithArrayOfInts(array_prop=[1, 2]), |
| 57 | + ) |
| 58 | + # Note, currently arrays of simple types are not validated, so the following assertion would fail: |
| 59 | + # with pytest.raises(TypeError): |
| 60 | + # ModelWithArrayOfInt.from_dict({"arrayProp": [1, "a"]}) |
| 61 | + |
| 62 | + def test_array_of_object(self, ModelWithArrayOfObjects, SimpleObject): |
| 63 | + assert_model_decode_encode( |
| 64 | + ModelWithArrayOfObjects, |
| 65 | + {"arrayProp": [{"name": "a"}, {"name": "b"}]}, |
| 66 | + ModelWithArrayOfObjects(array_prop=[SimpleObject(name="a"), SimpleObject(name="b")]), |
| 67 | + ) |
| 68 | + |
| 69 | + def test_type_hints(self, ModelWithArrayOfAny, ModelWithArrayOfInts, ModelWithArrayOfObjects, Unset): |
| 70 | + assert_model_property_type_hint(ModelWithArrayOfAny, "array_prop", Union[List[Any], Unset]) |
| 71 | + assert_model_property_type_hint(ModelWithArrayOfInts, "array_prop", Union[List[int], Unset]) |
| 72 | + assert_model_property_type_hint(ModelWithArrayOfObjects, "array_prop", Union[List[ForwardRef("SimpleObject")], Unset]) |
| 73 | + |
| 74 | + |
| 75 | +@with_generated_client_fixture( |
| 76 | +""" |
| 77 | +components: |
| 78 | + schemas: |
| 79 | + SimpleObject: |
| 80 | + type: object |
| 81 | + properties: |
| 82 | + name: {"type": "string"} |
| 83 | + ModelWithSinglePrefixItem: |
| 84 | + type: object |
| 85 | + properties: |
| 86 | + arrayProp: |
| 87 | + type: array |
| 88 | + prefixItems: |
| 89 | + - type: string |
| 90 | + ModelWithPrefixItems: |
| 91 | + type: object |
| 92 | + properties: |
| 93 | + arrayProp: |
| 94 | + type: array |
| 95 | + prefixItems: |
| 96 | + - $ref: "#/components/schemas/SimpleObject" |
| 97 | + - type: string |
| 98 | + ModelWithMixedItems: |
| 99 | + type: object |
| 100 | + properties: |
| 101 | + arrayProp: |
| 102 | + type: array |
| 103 | + prefixItems: |
| 104 | + - $ref: "#/components/schemas/SimpleObject" |
| 105 | + items: |
| 106 | + type: string |
| 107 | +""") |
| 108 | +@with_generated_code_imports( |
| 109 | + ".models.ModelWithSinglePrefixItem", |
| 110 | + ".models.ModelWithPrefixItems", |
| 111 | + ".models.ModelWithMixedItems", |
| 112 | + ".models.SimpleObject", |
| 113 | + ".types.Unset", |
| 114 | +) |
| 115 | +class TestArraysWithPrefixItems: |
| 116 | + def test_single_prefix_item(self, ModelWithSinglePrefixItem): |
| 117 | + assert_model_decode_encode( |
| 118 | + ModelWithSinglePrefixItem, |
| 119 | + {"arrayProp": ["a"]}, |
| 120 | + ModelWithSinglePrefixItem(array_prop=["a"]), |
| 121 | + ) |
| 122 | + |
| 123 | + def test_prefix_items(self, ModelWithPrefixItems, SimpleObject): |
| 124 | + assert_model_decode_encode( |
| 125 | + ModelWithPrefixItems, |
| 126 | + {"arrayProp": [{"name": "a"}, "b"]}, |
| 127 | + ModelWithPrefixItems(array_prop=[SimpleObject(name="a"), "b"]), |
| 128 | + ) |
| 129 | + |
| 130 | + def test_prefix_items_and_regular_items(self, ModelWithMixedItems, SimpleObject): |
| 131 | + assert_model_decode_encode( |
| 132 | + ModelWithMixedItems, |
| 133 | + {"arrayProp": [{"name": "a"}, "b"]}, |
| 134 | + ModelWithMixedItems(array_prop=[SimpleObject(name="a"), "b"]), |
| 135 | + ) |
| 136 | + |
| 137 | + def test_type_hints(self, ModelWithSinglePrefixItem, ModelWithPrefixItems, ModelWithMixedItems, Unset): |
| 138 | + assert_model_property_type_hint(ModelWithSinglePrefixItem, "array_prop", Union[List[str], Unset]) |
| 139 | + assert_model_property_type_hint( |
| 140 | + ModelWithPrefixItems, |
| 141 | + "array_prop", |
| 142 | + Union[List[Union[ForwardRef("SimpleObject"), str]], Unset], |
| 143 | + ) |
| 144 | + assert_model_property_type_hint( |
| 145 | + ModelWithMixedItems, |
| 146 | + "array_prop", |
| 147 | + Union[List[Union[ForwardRef("SimpleObject"), str]], Unset], |
| 148 | + ) |
| 149 | + # Note, this test is asserting the current behavior which, due to limitations of the implementation |
| 150 | + # (see: https://github.com/openapi-generators/openapi-python-client/pull/1130), is not really doing |
| 151 | + # tuple type validation-- the ordering of prefixItems is ignored, and instead all of the types are |
| 152 | + # simply treated as a union. |
0 commit comments