Skip to content

Commit 7ba5370

Browse files
committed
Changed behavior from using the term x_type_0 to x_type_y if x is a Schema that defines a Title property string y.
This provides an easier interface for understanding what users are importing, as well as less reliance on the ordering of the schema.
1 parent 9600088 commit 7ba5370

File tree

4 files changed

+59
-4
lines changed

4 files changed

+59
-4
lines changed

openapi_python_client/parser/properties/union.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
from attr import define, evolve
77

8+
from openapi_python_client.schema.data_type import DataType
9+
810
from ... import Config
911
from ... import schema as oai
1012
from ...utils import PythonIdentifier
@@ -55,8 +57,14 @@ def build(
5557
type_list_data.append(data.model_copy(update={"type": _type, "default": None}))
5658

5759
for i, sub_prop_data in enumerate(chain(data.anyOf, data.oneOf, type_list_data)):
60+
# If a schema has a title property, we can use that to carry forward a descriptive instead of "type_0"
61+
subscript = i
62+
is_oneOf = i >= len(data.anyOf) and i < (len(data.anyOf) + len(data.oneOf))
63+
if isinstance(sub_prop_data, oai.Schema) and sub_prop_data.title is not None and is_oneOf:
64+
subscript = sub_prop_data.title
65+
5866
sub_prop, schemas = property_from_data(
59-
name=f"{name}_type_{i}",
67+
name=f"{name}_type_{subscript}",
6068
required=True,
6169
data=sub_prop_data,
6270
schemas=schemas,

tests/conftest.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
import pytest
55

6-
from openapi_python_client import Config, MetaType
6+
from openapi_python_client import Config, MetaType, utils
77
from openapi_python_client import schema as oai
88
from openapi_python_client.config import ConfigFile
99
from openapi_python_client.parser.properties import (
@@ -267,5 +267,5 @@ def _common_kwargs(kwargs: Dict[str, Any]) -> Dict[str, Any]:
267267
**kwargs,
268268
}
269269
if not kwargs.get("python_name"):
270-
kwargs["python_name"] = kwargs["name"]
270+
kwargs["python_name"] = utils.PythonIdentifier(value=kwargs["name"], prefix="")
271271
return kwargs

tests/test_parser/test_properties/test_init.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ def test_property_from_data_str_enum_with_null(
423423
# None / null is removed from enum, and property is now nullable
424424
assert isinstance(prop, UnionProperty), "Enums with None should be converted to UnionProperties"
425425
enum_prop = enum_property_factory(
426-
name="my_enum_type_1",
426+
name="my_enum_type_AnEnum",
427427
required=required,
428428
values={"A": "A", "B": "B", "C": "C"},
429429
class_info=Class(name="ParentAnEnum", module_name="parent_an_enum"),

tests/test_parser/test_properties/test_union.py

+47
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,50 @@ def test_not_required_in_path(config):
8181

8282
err = prop.validate_location(ParameterLocation.PATH)
8383
assert isinstance(err, ParseError)
84+
85+
86+
def test_union_oneOf_descriptive_type_name(union_property_factory, date_time_property_factory, string_property_factory, config):
87+
from openapi_python_client.parser.properties import Schemas, property_from_data
88+
89+
nested_schema_variant_A = oai.Schema(type=DataType.STRING, title="A")
90+
nested_schema_variant_B = oai.Schema(type=DataType.STRING, title="B")
91+
nested_schema_variant_2 = oai.Schema(type=DataType.STRING)
92+
nested_schema_variant_C = oai.Schema(type=DataType.STRING, title="C")
93+
nested_schema_variant_4 = oai.Schema(type=DataType.STRING)
94+
95+
name = "union_prop"
96+
required = True
97+
data = oai.Schema(
98+
anyOf=[
99+
# AnyOf retains the old naming convention
100+
nested_schema_variant_C,
101+
nested_schema_variant_4,
102+
],
103+
oneOf=[
104+
# OneOf fields that define their own titles will have those titles as their Type names
105+
nested_schema_variant_A,
106+
nested_schema_variant_B,
107+
nested_schema_variant_2,
108+
oai.Schema(type=DataType.STRING, schema_format="date-time"),
109+
],
110+
)
111+
expected = union_property_factory(
112+
name=name,
113+
required=required,
114+
inner_properties=[
115+
string_property_factory(name=f"{name}_type_0"),
116+
string_property_factory(name=f"{name}_type_1"),
117+
string_property_factory(name=f"{name}_type_A"),
118+
string_property_factory(name=f"{name}_type_B"),
119+
string_property_factory(name=f"{name}_type_4"),
120+
date_time_property_factory(name=f"{name}_type_5"),
121+
],
122+
)
123+
124+
p, s = property_from_data(
125+
name=name, required=required, data=data, schemas=Schemas(), parent_name="parent", config=config
126+
)
127+
128+
assert p == expected
129+
assert s == Schemas()
130+

0 commit comments

Comments
 (0)