diff --git a/.changeset/allow_default_values_for_properties_of_any_type.md b/.changeset/allow_default_values_for_properties_of_any_type.md new file mode 100644 index 000000000..9b1a93bf7 --- /dev/null +++ b/.changeset/allow_default_values_for_properties_of_any_type.md @@ -0,0 +1,5 @@ +--- +default: patch +--- + +# Allow default values for properties of `Any` type diff --git a/end_to_end_tests/baseline_openapi_3.0.json b/end_to_end_tests/baseline_openapi_3.0.json index 4fd2711cf..468e3532d 100644 --- a/end_to_end_tests/baseline_openapi_3.0.json +++ b/end_to_end_tests/baseline_openapi_3.0.json @@ -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": { @@ -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" @@ -1708,7 +1723,9 @@ ], "type": "object", "properties": { - "any_value": {}, + "any_value": { + "default": "default" + }, "an_enum_value": { "$ref": "#/components/schemas/AnEnum" }, @@ -2185,7 +2202,10 @@ }, "stringToEnum": { "type": "string", - "enum": ["a", "b"] + "enum": [ + "a", + "b" + ] }, "stringToDate": { "type": "string", diff --git a/end_to_end_tests/baseline_openapi_3.1.yaml b/end_to_end_tests/baseline_openapi_3.1.yaml index c100ed296..5b01d9e29 100644 --- a/end_to_end_tests/baseline_openapi_3.1.yaml +++ b/end_to_end_tests/baseline_openapi_3.1.yaml @@ -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": { @@ -1698,7 +1708,9 @@ info: ], "type": "object", "properties": { - "any_value": { }, + "any_value": { + "default": "default", + }, "an_enum_value": { "$ref": "#/components/schemas/AnEnum" }, @@ -1972,7 +1984,7 @@ info: "title": "Some Integer Array", "type": "array", "items": { - "type": ["integer", "null"] + "type": [ "integer", "null" ] } }, "some_array": { @@ -2166,7 +2178,7 @@ info: "numberToInt": { "type": "number" }, - "anyToString": {} + "anyToString": { } } }, { @@ -2179,7 +2191,7 @@ info: }, "stringToEnum": { "type": "string", - "enum": ["a", "b"] + "enum": [ "a", "b" ] }, "stringToDate": { "type": "string", @@ -2265,7 +2277,7 @@ info: }, "ModelWithNoProperties": { "type": "object", - "properties": {}, + "properties": { }, "additionalProperties": false }, "AllOfSubModel": { diff --git a/end_to_end_tests/golden-record/my_test_api_client/api/defaults/defaults_tests_defaults_post.py b/end_to_end_tests/golden-record/my_test_api_client/api/defaults/defaults_tests_defaults_post.py index a14cb2670..43d4e1340 100644 --- a/end_to_end_tests/golden-record/my_test_api_client/api/defaults/defaults_tests_defaults_post.py +++ b/end_to_end_tests/golden-record/my_test_api_client/api/defaults/defaults_tests_defaults_post.py @@ -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], @@ -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 @@ -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], @@ -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]): @@ -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, @@ -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], @@ -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]): @@ -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, @@ -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], @@ -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]): @@ -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, @@ -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], @@ -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]): @@ -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, diff --git a/end_to_end_tests/golden-record/my_test_api_client/models/a_model.py b/end_to_end_tests/golden-record/my_test_api_client/models/a_model.py index d14160bf8..054e6bfa7 100644 --- a/end_to_end_tests/golden-record/my_test_api_client/models/a_model.py +++ b/end_to_end_tests/golden-record/my_test_api_client/models/a_model.py @@ -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]): @@ -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 diff --git a/end_to_end_tests/golden-record/my_test_api_client/models/extended.py b/end_to_end_tests/golden-record/my_test_api_client/models/extended.py index 932c98a99..c3659222b 100644 --- a/end_to_end_tests/golden-record/my_test_api_client/models/extended.py +++ b/end_to_end_tests/golden-record/my_test_api_client/models/extended.py @@ -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]): @@ -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 diff --git a/openapi_python_client/parser/properties/__init__.py b/openapi_python_client/parser/properties/__init__.py index c8777f509..02a6fdafe 100644 --- a/openapi_python_client/parser/properties/__init__.py +++ b/openapi_python_client/parser/properties/__init__.py @@ -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, diff --git a/openapi_python_client/parser/properties/any.py b/openapi_python_client/parser/properties/any.py index 64c2a9725..b760a1568 100644 --- a/openapi_python_client/parser/properties/any.py +++ b/openapi_python_client/parser/properties/any.py @@ -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) diff --git a/openapi_python_client/parser/properties/const.py b/openapi_python_client/parser/properties/const.py index 605bbfe73..9da3f8f1e 100644 --- a/openapi_python_client/parser/properties/const.py +++ b/openapi_python_client/parser/properties/const.py @@ -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 @@ -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) diff --git a/pyproject.toml b/pyproject.toml index 0689285df..e2058e058 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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"]