FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

fix: treat array without items as list of Any per OpenAPI 3.1 (#1435) by rifkir23 · Pull Request #1484 · openapi-generators/openapi-python-client · GitHub

Repository navigation

Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .md  (1) .py  (2) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
11 changes: 11 additions & 0 deletions .changeset/array_without_items_is_list_of_any.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
default: patch
---

# Treat `type: array` without `items` as a list of Any

An array schema with neither `items` nor `prefixItems` is valid in JSON Schema
2020-12 (OpenAPI 3.1) and means "array of any type". Previously the generator
failed such schemas with a `PropertyError` ("type array must have items or
prefixItems defined"), silently skipping the property or `anyOf` branch. It now
generates a `list[Any]`, equivalent to `items: {}`.
17 changes: 7 additions & 10 deletions openapi_python_client/parser/properties/list_property.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -58,20 +58,17 @@ def build(
"""
from . import property_from_data # noqa: PLC0415

if data.items is None and not data.prefixItems:
return (
PropertyError(
data=data,
detail="type array must have items or prefixItems defined",
),
schemas,
)

items = data.prefixItems or []
if data.items:
items.append(data.items)

if len(items) == 1:
if not items:
# An array with neither `items` nor `prefixItems` is valid in JSON
# Schema 2020-12 (OpenAPI 3.1) and means "array of any type". Treat
# it as equivalent to `items: {}` and generate a list of Any rather
# than failing. See issue #1435.
inner_schema: oai.Schema | oai.Reference = oai.Schema()
elif len(items) == 1:
inner_schema = items[0]
else:
inner_schema = oai.Schema(anyOf=items)
Expand Down
22 changes: 22 additions & 0 deletions tests/test_parser/test_properties/test_init.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import openapi_python_client.schema as oai
from openapi_python_client.parser.errors import PropertyError
from openapi_python_client.parser.properties import (
AnyProperty,
Class,
ListProperty,
ReferencePath,
Schemas,
_propogate_removal,
Expand Down Expand Up @@ -323,3 +325,23 @@ def test_propogate_removal_ref_path_already_removed(self):
assert schemas.classes_by_name == {class_name: None}
assert schemas.classes_by_reference == {ref_path: None}
assert not error.detail


class TestArrayWithoutItems:
def test_array_without_items_becomes_list_of_any(self, config):
"""Issue #1435: `{"type": "array"}` with no `items`/`prefixItems` is
valid in OpenAPI 3.1 (JSON Schema 2020-12) and means "array of any". It
must generate a list of Any instead of failing with a PropertyError."""
data = oai.Schema(type="array", description="anything")
prop, _ = property_from_data(
name=UntrustedString("metrics"),
required=True,
data=data,
schemas=Schemas(),
parent_name="parent",
config=config,
)

assert isinstance(prop, ListProperty)
assert isinstance(prop.inner_property, AnyProperty)
assert prop.get_base_type_string().as_unembedded_code() == "list[Any]"

Back | FazBrowse Home | New Git URL