Skip to content

Nullable array models generate failing code #928

@kgutwin

Description

@kgutwin

Describe the bug
When an array is marked as nullable (in OpenAPI 3.0 or 3.1) the generated code fails type checking with the message:

error: Incompatible types in assignment (expression has type "tuple[None, bytes, str]", variable has type "list[float] | Unset | None")  [assignment]

From the end-to-end test suite, making some_array nullable (part of Body_upload_file_tests_upload_post) results in this change:

@@ -165,10 +172,17 @@ class BodyUploadFileTestsUploadPost:
             else (None, str(self.some_number).encode(), "text/plain")
         )
 
-        some_array: Union[Unset, Tuple[None, bytes, str]] = UNSET
-        if not isinstance(self.some_array, Unset):
-            _temp_some_array = self.some_array
-            some_array = (None, json.dumps(_temp_some_array).encode(), "application/json")
+        some_array: Union[List[float], None, Unset]
+        if isinstance(self.some_array, Unset):
+            some_array = UNSET
+        elif isinstance(self.some_array, list):
+            some_array = UNSET
+            if not isinstance(self.some_array, Unset):
+                _temp_some_array = self.some_array
+                some_array = (None, json.dumps(_temp_some_array).encode(), "application/json")
+
+        else:
+            some_array = self.some_array
 
         some_optional_object: Union[Unset, Tuple[None, bytes, str]] = UNSET

OpenAPI Spec File
The following patch applied the end-to-end test suite reproduces the problem:

diff --git a/end_to_end_tests/baseline_openapi_3.0.json b/end_to_end_tests/baseline_openapi_3.0.json
index d21d1d5..25adeaa 100644
--- a/end_to_end_tests/baseline_openapi_3.0.json
+++ b/end_to_end_tests/baseline_openapi_3.0.json
@@ -1778,6 +1778,7 @@
           },
           "some_array": {
             "title": "Some Array",
+            "nullable": true,
             "type": "array",
             "items": {
               "type": "number"
diff --git a/end_to_end_tests/baseline_openapi_3.1.yaml b/end_to_end_tests/baseline_openapi_3.1.yaml
index 03270af..4e33e68 100644
--- a/end_to_end_tests/baseline_openapi_3.1.yaml
+++ b/end_to_end_tests/baseline_openapi_3.1.yaml
@@ -1794,7 +1794,7 @@ info:
         },
         "some_array": {
           "title": "Some Array",
-          "type": "array",
+          "type": [ "array", "null" ],
           "items": {
             "type": "number"
           }

Desktop (please complete the following information):

  • openapi-python-client version 0.17.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions