Skip to content

Commit

Permalink
Changed behavior from using the term x_type_0 to x_type_y if x is…
Browse files Browse the repository at this point in the history
… 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.
  • Loading branch information
wallagib committed Feb 14, 2024
1 parent 9600088 commit 7ba5370
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 4 deletions.
10 changes: 9 additions & 1 deletion openapi_python_client/parser/properties/union.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

from attr import define, evolve

from openapi_python_client.schema.data_type import DataType

from ... import Config
from ... import schema as oai
from ...utils import PythonIdentifier
Expand Down Expand Up @@ -55,8 +57,14 @@ def build(
type_list_data.append(data.model_copy(update={"type": _type, "default": None}))

for i, sub_prop_data in enumerate(chain(data.anyOf, data.oneOf, type_list_data)):
# If a schema has a title property, we can use that to carry forward a descriptive instead of "type_0"
subscript = i
is_oneOf = i >= len(data.anyOf) and i < (len(data.anyOf) + len(data.oneOf))
if isinstance(sub_prop_data, oai.Schema) and sub_prop_data.title is not None and is_oneOf:
subscript = sub_prop_data.title

sub_prop, schemas = property_from_data(
name=f"{name}_type_{i}",
name=f"{name}_type_{subscript}",
required=True,
data=sub_prop_data,
schemas=schemas,
Expand Down
4 changes: 2 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import pytest

from openapi_python_client import Config, MetaType
from openapi_python_client import Config, MetaType, utils
from openapi_python_client import schema as oai
from openapi_python_client.config import ConfigFile
from openapi_python_client.parser.properties import (
Expand Down Expand Up @@ -267,5 +267,5 @@ def _common_kwargs(kwargs: Dict[str, Any]) -> Dict[str, Any]:
**kwargs,
}
if not kwargs.get("python_name"):
kwargs["python_name"] = kwargs["name"]
kwargs["python_name"] = utils.PythonIdentifier(value=kwargs["name"], prefix="")
return kwargs
2 changes: 1 addition & 1 deletion tests/test_parser/test_properties/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ def test_property_from_data_str_enum_with_null(
# None / null is removed from enum, and property is now nullable
assert isinstance(prop, UnionProperty), "Enums with None should be converted to UnionProperties"
enum_prop = enum_property_factory(
name="my_enum_type_1",
name="my_enum_type_AnEnum",
required=required,
values={"A": "A", "B": "B", "C": "C"},
class_info=Class(name="ParentAnEnum", module_name="parent_an_enum"),
Expand Down
47 changes: 47 additions & 0 deletions tests/test_parser/test_properties/test_union.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,50 @@ def test_not_required_in_path(config):

err = prop.validate_location(ParameterLocation.PATH)
assert isinstance(err, ParseError)


def test_union_oneOf_descriptive_type_name(union_property_factory, date_time_property_factory, string_property_factory, config):
from openapi_python_client.parser.properties import Schemas, property_from_data

nested_schema_variant_A = oai.Schema(type=DataType.STRING, title="A")
nested_schema_variant_B = oai.Schema(type=DataType.STRING, title="B")
nested_schema_variant_2 = oai.Schema(type=DataType.STRING)
nested_schema_variant_C = oai.Schema(type=DataType.STRING, title="C")
nested_schema_variant_4 = oai.Schema(type=DataType.STRING)

name = "union_prop"
required = True
data = oai.Schema(
anyOf=[
# AnyOf retains the old naming convention
nested_schema_variant_C,
nested_schema_variant_4,
],
oneOf=[
# OneOf fields that define their own titles will have those titles as their Type names
nested_schema_variant_A,
nested_schema_variant_B,
nested_schema_variant_2,
oai.Schema(type=DataType.STRING, schema_format="date-time"),
],
)
expected = union_property_factory(
name=name,
required=required,
inner_properties=[
string_property_factory(name=f"{name}_type_0"),
string_property_factory(name=f"{name}_type_1"),
string_property_factory(name=f"{name}_type_A"),
string_property_factory(name=f"{name}_type_B"),
string_property_factory(name=f"{name}_type_4"),
date_time_property_factory(name=f"{name}_type_5"),
],
)

p, s = property_from_data(
name=name, required=required, data=data, schemas=Schemas(), parent_name="parent", config=config
)

assert p == expected
assert s == Schemas()

0 comments on commit 7ba5370

Please sign in to comment.