Skip to content

Commit

Permalink
Improve test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
dbanty committed Sep 7, 2024
1 parent 19fb418 commit 886d0e7
Show file tree
Hide file tree
Showing 10 changed files with 70 additions and 22 deletions.
5 changes: 5 additions & 0 deletions .changeset/allow_default_values_for_properties_of_any_type.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
default: patch
---

# Allow default values for properties of `Any` type
32 changes: 26 additions & 6 deletions end_to_end_tests/baseline_openapi_3.0.json
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,16 @@
"name": "float_prop",
"in": "query"
},
{
"required": true,
"schema": {
"title": "Float with int default",
"type": "number",
"default": 3
},
"name": "float_with_int",
"in": "query"
},
{
"required": true,
"schema": {
Expand Down Expand Up @@ -1674,15 +1684,20 @@
"an_required_field"
]
},
"Aliased":{
"Aliased": {
"allOf": [
{"$ref": "#/components/schemas/AModel"}
{
"$ref": "#/components/schemas/AModel"
}
]
},
"Extended": {
"allOf": [
{"$ref": "#/components/schemas/Aliased"},
{"type": "object",
{
"$ref": "#/components/schemas/Aliased"
},
{
"type": "object",
"properties": {
"fromExtended": {
"type": "string"
Expand All @@ -1708,7 +1723,9 @@
],
"type": "object",
"properties": {
"any_value": {},
"any_value": {
"default": "default"
},
"an_enum_value": {
"$ref": "#/components/schemas/AnEnum"
},
Expand Down Expand Up @@ -2185,7 +2202,10 @@
},
"stringToEnum": {
"type": "string",
"enum": ["a", "b"]
"enum": [
"a",
"b"
]
},
"stringToDate": {
"type": "string",
Expand Down
22 changes: 17 additions & 5 deletions end_to_end_tests/baseline_openapi_3.1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,16 @@ info:
"name": "float_prop",
"in": "query"
},
{
"required": true,
"schema": {
"title": "Float with int default",
"type": "number",
"default": 3
},
"name": "float_with_int",
"in": "query"
},
{
"required": true,
"schema": {
Expand Down Expand Up @@ -1698,7 +1708,9 @@ info:
],
"type": "object",
"properties": {
"any_value": { },
"any_value": {
"default": "default",
},
"an_enum_value": {
"$ref": "#/components/schemas/AnEnum"
},
Expand Down Expand Up @@ -1972,7 +1984,7 @@ info:
"title": "Some Integer Array",
"type": "array",
"items": {
"type": ["integer", "null"]
"type": [ "integer", "null" ]
}
},
"some_array": {
Expand Down Expand Up @@ -2166,7 +2178,7 @@ info:
"numberToInt": {
"type": "number"
},
"anyToString": {}
"anyToString": { }
}
},
{
Expand All @@ -2179,7 +2191,7 @@ info:
},
"stringToEnum": {
"type": "string",
"enum": ["a", "b"]
"enum": [ "a", "b" ]
},
"stringToDate": {
"type": "string",
Expand Down Expand Up @@ -2265,7 +2277,7 @@ info:
},
"ModelWithNoProperties": {
"type": "object",
"properties": {},
"properties": { },
"additionalProperties": false
},
"AllOfSubModel": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def _get_kwargs(
string_with_num: str = "1",
date_prop: datetime.date = isoparse("1010-10-10").date(),
float_prop: float = 3.14,
float_with_int: float = 3.0,
int_prop: int = 7,
boolean_prop: bool = False,
list_prop: List[AnEnum],
Expand All @@ -39,6 +40,8 @@ def _get_kwargs(

params["float_prop"] = float_prop

params["float_with_int"] = float_with_int

params["int_prop"] = int_prop

params["boolean_prop"] = boolean_prop
Expand Down Expand Up @@ -117,6 +120,7 @@ def sync_detailed(
string_with_num: str = "1",
date_prop: datetime.date = isoparse("1010-10-10").date(),
float_prop: float = 3.14,
float_with_int: float = 3.0,
int_prop: int = 7,
boolean_prop: bool = False,
list_prop: List[AnEnum],
Expand All @@ -133,6 +137,7 @@ def sync_detailed(
string_with_num (str): Default: '1'.
date_prop (datetime.date): Default: isoparse('1010-10-10').date().
float_prop (float): Default: 3.14.
float_with_int (float): Default: 3.0.
int_prop (int): Default: 7.
boolean_prop (bool): Default: False.
list_prop (List[AnEnum]):
Expand All @@ -155,6 +160,7 @@ def sync_detailed(
string_with_num=string_with_num,
date_prop=date_prop,
float_prop=float_prop,
float_with_int=float_with_int,
int_prop=int_prop,
boolean_prop=boolean_prop,
list_prop=list_prop,
Expand All @@ -179,6 +185,7 @@ def sync(
string_with_num: str = "1",
date_prop: datetime.date = isoparse("1010-10-10").date(),
float_prop: float = 3.14,
float_with_int: float = 3.0,
int_prop: int = 7,
boolean_prop: bool = False,
list_prop: List[AnEnum],
Expand All @@ -195,6 +202,7 @@ def sync(
string_with_num (str): Default: '1'.
date_prop (datetime.date): Default: isoparse('1010-10-10').date().
float_prop (float): Default: 3.14.
float_with_int (float): Default: 3.0.
int_prop (int): Default: 7.
boolean_prop (bool): Default: False.
list_prop (List[AnEnum]):
Expand All @@ -218,6 +226,7 @@ def sync(
string_with_num=string_with_num,
date_prop=date_prop,
float_prop=float_prop,
float_with_int=float_with_int,
int_prop=int_prop,
boolean_prop=boolean_prop,
list_prop=list_prop,
Expand All @@ -236,6 +245,7 @@ async def asyncio_detailed(
string_with_num: str = "1",
date_prop: datetime.date = isoparse("1010-10-10").date(),
float_prop: float = 3.14,
float_with_int: float = 3.0,
int_prop: int = 7,
boolean_prop: bool = False,
list_prop: List[AnEnum],
Expand All @@ -252,6 +262,7 @@ async def asyncio_detailed(
string_with_num (str): Default: '1'.
date_prop (datetime.date): Default: isoparse('1010-10-10').date().
float_prop (float): Default: 3.14.
float_with_int (float): Default: 3.0.
int_prop (int): Default: 7.
boolean_prop (bool): Default: False.
list_prop (List[AnEnum]):
Expand All @@ -274,6 +285,7 @@ async def asyncio_detailed(
string_with_num=string_with_num,
date_prop=date_prop,
float_prop=float_prop,
float_with_int=float_with_int,
int_prop=int_prop,
boolean_prop=boolean_prop,
list_prop=list_prop,
Expand All @@ -296,6 +308,7 @@ async def asyncio(
string_with_num: str = "1",
date_prop: datetime.date = isoparse("1010-10-10").date(),
float_prop: float = 3.14,
float_with_int: float = 3.0,
int_prop: int = 7,
boolean_prop: bool = False,
list_prop: List[AnEnum],
Expand All @@ -312,6 +325,7 @@ async def asyncio(
string_with_num (str): Default: '1'.
date_prop (datetime.date): Default: isoparse('1010-10-10').date().
float_prop (float): Default: 3.14.
float_with_int (float): Default: 3.0.
int_prop (int): Default: 7.
boolean_prop (bool): Default: False.
list_prop (List[AnEnum]):
Expand All @@ -336,6 +350,7 @@ async def asyncio(
string_with_num=string_with_num,
date_prop=date_prop,
float_prop=float_prop,
float_with_int=float_with_int,
int_prop=int_prop,
boolean_prop=boolean_prop,
list_prop=list_prop,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class AModel:
nullable_one_of_models (Union['FreeFormModel', 'ModelWithUnionProperty', None]):
model (ModelWithUnionProperty):
nullable_model (Union['ModelWithUnionProperty', None]):
any_value (Union[Unset, Any]):
any_value (Union[Unset, Any]): Default: 'default'.
an_optional_allof_enum (Union[Unset, AnAllOfEnum]):
nested_list_of_enums (Union[Unset, List[List[DifferentEnum]]]):
a_not_required_date (Union[Unset, datetime.date]):
Expand All @@ -58,7 +58,7 @@ class AModel:
model: "ModelWithUnionProperty"
nullable_model: Union["ModelWithUnionProperty", None]
an_allof_enum_with_overridden_default: AnAllOfEnum = AnAllOfEnum.OVERRIDDEN_DEFAULT
any_value: Union[Unset, Any] = UNSET
any_value: Union[Unset, Any] = "default"
an_optional_allof_enum: Union[Unset, AnAllOfEnum] = UNSET
nested_list_of_enums: Union[Unset, List[List[DifferentEnum]]] = UNSET
a_not_required_date: Union[Unset, datetime.date] = UNSET
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class Extended:
nullable_one_of_models (Union['FreeFormModel', 'ModelWithUnionProperty', None]):
model (ModelWithUnionProperty):
nullable_model (Union['ModelWithUnionProperty', None]):
any_value (Union[Unset, Any]):
any_value (Union[Unset, Any]): Default: 'default'.
an_optional_allof_enum (Union[Unset, AnAllOfEnum]):
nested_list_of_enums (Union[Unset, List[List[DifferentEnum]]]):
a_not_required_date (Union[Unset, datetime.date]):
Expand All @@ -59,7 +59,7 @@ class Extended:
model: "ModelWithUnionProperty"
nullable_model: Union["ModelWithUnionProperty", None]
an_allof_enum_with_overridden_default: AnAllOfEnum = AnAllOfEnum.OVERRIDDEN_DEFAULT
any_value: Union[Unset, Any] = UNSET
any_value: Union[Unset, Any] = "default"
an_optional_allof_enum: Union[Unset, AnAllOfEnum] = UNSET
nested_list_of_enums: Union[Unset, List[List[DifferentEnum]]] = UNSET
a_not_required_date: Union[Unset, datetime.date] = UNSET
Expand Down
2 changes: 1 addition & 1 deletion openapi_python_client/parser/properties/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ def property_from_data( # noqa: PLR0911, PLR0912
AnyProperty.build(
name=name,
required=required,
default=None,
default=data.default,
python_name=utils.PythonIdentifier(value=name, prefix=config.field_prefix),
description=data.description,
example=data.example,
Expand Down
2 changes: 1 addition & 1 deletion openapi_python_client/parser/properties/any.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def build(
def convert_value(cls, value: Any) -> Value | None:
from .string import StringProperty

if value is None or isinstance(value, Value):
if value is None:
return value
if isinstance(value, str):
return StringProperty.convert_value(value)
Expand Down
4 changes: 0 additions & 4 deletions openapi_python_client/parser/properties/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,6 @@ def build(
return prop

def convert_value(self, value: Any) -> Value | None | PropertyError:
if isinstance(value, Value):
return value
value = self._convert_value(value)
if value is None:
return value
Expand All @@ -87,8 +85,6 @@ def _convert_value(value: Any) -> Value: ... # pragma: no cover
def _convert_value(value: Any) -> Value | None:
if value is None or isinstance(value, Value):
return value
if isinstance(value, Value):
return value # pragma: no cover
if isinstance(value, str):
return StringProperty.convert_value(value)
return Value(python_code=str(value), raw_value=value)
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ ignore = ["E501", "PLR0913"]
"tests/*" = ["PLR2004"]

[tool.coverage.run]
omit = ["openapi_python_client/templates/*"]
omit = ["openapi_python_client/__main__.py", "openapi_python_client/templates/*"]

[tool.mypy]
plugins = ["pydantic.mypy"]
Expand Down

0 comments on commit 886d0e7

Please sign in to comment.