diff --git a/end_to_end_tests/golden-record/my_test_api_client/api/default/get_common_parameters.py b/end_to_end_tests/golden-record/my_test_api_client/api/default/get_common_parameters.py index 158e04b1d..96487587d 100644 --- a/end_to_end_tests/golden-record/my_test_api_client/api/default/get_common_parameters.py +++ b/end_to_end_tests/golden-record/my_test_api_client/api/default/get_common_parameters.py @@ -30,7 +30,7 @@ def _get_kwargs( } -def _build_response(*, response: httpx.Response) -> Response[None]: +def _build_response(*, response: httpx.Response) -> Response[Any]: return Response( status_code=response.status_code, content=response.content, @@ -43,7 +43,7 @@ def sync_detailed( *, client: Client, common: Union[Unset, str] = UNSET, -) -> Response[None]: +) -> Response[Any]: kwargs = _get_kwargs( client=client, common=common, @@ -60,7 +60,7 @@ async def asyncio_detailed( *, client: Client, common: Union[Unset, str] = UNSET, -) -> Response[None]: +) -> Response[Any]: kwargs = _get_kwargs( client=client, common=common, diff --git a/end_to_end_tests/golden-record/my_test_api_client/api/default/post_common_parameters.py b/end_to_end_tests/golden-record/my_test_api_client/api/default/post_common_parameters.py index bb2d0f395..3283f4830 100644 --- a/end_to_end_tests/golden-record/my_test_api_client/api/default/post_common_parameters.py +++ b/end_to_end_tests/golden-record/my_test_api_client/api/default/post_common_parameters.py @@ -30,7 +30,7 @@ def _get_kwargs( } -def _build_response(*, response: httpx.Response) -> Response[None]: +def _build_response(*, response: httpx.Response) -> Response[Any]: return Response( status_code=response.status_code, content=response.content, @@ -43,7 +43,7 @@ def sync_detailed( *, client: Client, common: Union[Unset, str] = UNSET, -) -> Response[None]: +) -> Response[Any]: kwargs = _get_kwargs( client=client, common=common, @@ -60,7 +60,7 @@ async def asyncio_detailed( *, client: Client, common: Union[Unset, str] = UNSET, -) -> Response[None]: +) -> Response[Any]: kwargs = _get_kwargs( client=client, common=common, diff --git a/end_to_end_tests/golden-record/my_test_api_client/api/parameters/get_same_name_multiple_locations_param.py b/end_to_end_tests/golden-record/my_test_api_client/api/parameters/get_same_name_multiple_locations_param.py index eeb4daff7..2013768aa 100644 --- a/end_to_end_tests/golden-record/my_test_api_client/api/parameters/get_same_name_multiple_locations_param.py +++ b/end_to_end_tests/golden-record/my_test_api_client/api/parameters/get_same_name_multiple_locations_param.py @@ -31,7 +31,7 @@ def _get_kwargs( } -def _build_response(*, response: httpx.Response) -> Response[None]: +def _build_response(*, response: httpx.Response) -> Response[Any]: return Response( status_code=response.status_code, content=response.content, @@ -45,7 +45,7 @@ def sync_detailed( client: Client, param_path: Union[Unset, str] = UNSET, param_query: Union[Unset, str] = UNSET, -) -> Response[None]: +) -> Response[Any]: kwargs = _get_kwargs( client=client, param_path=param_path, @@ -64,7 +64,7 @@ async def asyncio_detailed( client: Client, param_path: Union[Unset, str] = UNSET, param_query: Union[Unset, str] = UNSET, -) -> Response[None]: +) -> Response[Any]: kwargs = _get_kwargs( client=client, param_path=param_path, diff --git a/end_to_end_tests/golden-record/my_test_api_client/api/tests/defaults_tests_defaults_post.py b/end_to_end_tests/golden-record/my_test_api_client/api/tests/defaults_tests_defaults_post.py index 937d903a3..f22ec0153 100644 --- a/end_to_end_tests/golden-record/my_test_api_client/api/tests/defaults_tests_defaults_post.py +++ b/end_to_end_tests/golden-record/my_test_api_client/api/tests/defaults_tests_defaults_post.py @@ -131,9 +131,9 @@ def _get_kwargs( } -def _parse_response(*, response: httpx.Response) -> Optional[Union[HTTPValidationError, None]]: +def _parse_response(*, response: httpx.Response) -> Optional[Union[Any, HTTPValidationError]]: if response.status_code == 200: - response_200 = None + response_200 = response.json() return response_200 if response.status_code == 422: @@ -143,7 +143,7 @@ def _parse_response(*, response: httpx.Response) -> Optional[Union[HTTPValidatio return None -def _build_response(*, response: httpx.Response) -> Response[Union[HTTPValidationError, None]]: +def _build_response(*, response: httpx.Response) -> Response[Union[Any, HTTPValidationError]]: return Response( status_code=response.status_code, content=response.content, @@ -172,7 +172,7 @@ def sync_detailed( required_model_prop: ModelWithUnionProperty, nullable_model_prop: Union[Unset, None, ModelWithUnionProperty] = UNSET, nullable_required_model_prop: Optional[ModelWithUnionProperty], -) -> Response[Union[HTTPValidationError, None]]: +) -> Response[Union[Any, HTTPValidationError]]: kwargs = _get_kwargs( client=client, string_prop=string_prop, @@ -221,7 +221,7 @@ def sync( required_model_prop: ModelWithUnionProperty, nullable_model_prop: Union[Unset, None, ModelWithUnionProperty] = UNSET, nullable_required_model_prop: Optional[ModelWithUnionProperty], -) -> Optional[Union[HTTPValidationError, None]]: +) -> Optional[Union[Any, HTTPValidationError]]: """ """ return sync_detailed( @@ -266,7 +266,7 @@ async def asyncio_detailed( required_model_prop: ModelWithUnionProperty, nullable_model_prop: Union[Unset, None, ModelWithUnionProperty] = UNSET, nullable_required_model_prop: Optional[ModelWithUnionProperty], -) -> Response[Union[HTTPValidationError, None]]: +) -> Response[Union[Any, HTTPValidationError]]: kwargs = _get_kwargs( client=client, string_prop=string_prop, @@ -314,7 +314,7 @@ async def asyncio( required_model_prop: ModelWithUnionProperty, nullable_model_prop: Union[Unset, None, ModelWithUnionProperty] = UNSET, nullable_required_model_prop: Optional[ModelWithUnionProperty], -) -> Optional[Union[HTTPValidationError, None]]: +) -> Optional[Union[Any, HTTPValidationError]]: """ """ return ( diff --git a/end_to_end_tests/golden-record/my_test_api_client/api/tests/int_enum_tests_int_enum_post.py b/end_to_end_tests/golden-record/my_test_api_client/api/tests/int_enum_tests_int_enum_post.py index aebbccebc..d295ddaab 100644 --- a/end_to_end_tests/golden-record/my_test_api_client/api/tests/int_enum_tests_int_enum_post.py +++ b/end_to_end_tests/golden-record/my_test_api_client/api/tests/int_enum_tests_int_enum_post.py @@ -34,9 +34,9 @@ def _get_kwargs( } -def _parse_response(*, response: httpx.Response) -> Optional[Union[HTTPValidationError, None]]: +def _parse_response(*, response: httpx.Response) -> Optional[Union[Any, HTTPValidationError]]: if response.status_code == 200: - response_200 = None + response_200 = response.json() return response_200 if response.status_code == 422: @@ -46,7 +46,7 @@ def _parse_response(*, response: httpx.Response) -> Optional[Union[HTTPValidatio return None -def _build_response(*, response: httpx.Response) -> Response[Union[HTTPValidationError, None]]: +def _build_response(*, response: httpx.Response) -> Response[Union[Any, HTTPValidationError]]: return Response( status_code=response.status_code, content=response.content, @@ -59,7 +59,7 @@ def sync_detailed( *, client: Client, int_enum: AnIntEnum, -) -> Response[Union[HTTPValidationError, None]]: +) -> Response[Union[Any, HTTPValidationError]]: kwargs = _get_kwargs( client=client, int_enum=int_enum, @@ -76,7 +76,7 @@ def sync( *, client: Client, int_enum: AnIntEnum, -) -> Optional[Union[HTTPValidationError, None]]: +) -> Optional[Union[Any, HTTPValidationError]]: """ """ return sync_detailed( @@ -89,7 +89,7 @@ async def asyncio_detailed( *, client: Client, int_enum: AnIntEnum, -) -> Response[Union[HTTPValidationError, None]]: +) -> Response[Union[Any, HTTPValidationError]]: kwargs = _get_kwargs( client=client, int_enum=int_enum, @@ -105,7 +105,7 @@ async def asyncio( *, client: Client, int_enum: AnIntEnum, -) -> Optional[Union[HTTPValidationError, None]]: +) -> Optional[Union[Any, HTTPValidationError]]: """ """ return ( diff --git a/end_to_end_tests/golden-record/my_test_api_client/api/tests/json_body_tests_json_body_post.py b/end_to_end_tests/golden-record/my_test_api_client/api/tests/json_body_tests_json_body_post.py index 14c81c0d1..eba1f9615 100644 --- a/end_to_end_tests/golden-record/my_test_api_client/api/tests/json_body_tests_json_body_post.py +++ b/end_to_end_tests/golden-record/my_test_api_client/api/tests/json_body_tests_json_body_post.py @@ -29,9 +29,9 @@ def _get_kwargs( } -def _parse_response(*, response: httpx.Response) -> Optional[Union[HTTPValidationError, None]]: +def _parse_response(*, response: httpx.Response) -> Optional[Union[Any, HTTPValidationError]]: if response.status_code == 200: - response_200 = None + response_200 = response.json() return response_200 if response.status_code == 422: @@ -41,7 +41,7 @@ def _parse_response(*, response: httpx.Response) -> Optional[Union[HTTPValidatio return None -def _build_response(*, response: httpx.Response) -> Response[Union[HTTPValidationError, None]]: +def _build_response(*, response: httpx.Response) -> Response[Union[Any, HTTPValidationError]]: return Response( status_code=response.status_code, content=response.content, @@ -54,7 +54,7 @@ def sync_detailed( *, client: Client, json_body: AModel, -) -> Response[Union[HTTPValidationError, None]]: +) -> Response[Union[Any, HTTPValidationError]]: kwargs = _get_kwargs( client=client, json_body=json_body, @@ -71,7 +71,7 @@ def sync( *, client: Client, json_body: AModel, -) -> Optional[Union[HTTPValidationError, None]]: +) -> Optional[Union[Any, HTTPValidationError]]: """Try sending a JSON body""" return sync_detailed( @@ -84,7 +84,7 @@ async def asyncio_detailed( *, client: Client, json_body: AModel, -) -> Response[Union[HTTPValidationError, None]]: +) -> Response[Union[Any, HTTPValidationError]]: kwargs = _get_kwargs( client=client, json_body=json_body, @@ -100,7 +100,7 @@ async def asyncio( *, client: Client, json_body: AModel, -) -> Optional[Union[HTTPValidationError, None]]: +) -> Optional[Union[Any, HTTPValidationError]]: """Try sending a JSON body""" return ( diff --git a/end_to_end_tests/golden-record/my_test_api_client/api/tests/no_response_tests_no_response_get.py b/end_to_end_tests/golden-record/my_test_api_client/api/tests/no_response_tests_no_response_get.py index f1e02671d..9ba34b83b 100644 --- a/end_to_end_tests/golden-record/my_test_api_client/api/tests/no_response_tests_no_response_get.py +++ b/end_to_end_tests/golden-record/my_test_api_client/api/tests/no_response_tests_no_response_get.py @@ -23,7 +23,7 @@ def _get_kwargs( } -def _build_response(*, response: httpx.Response) -> Response[None]: +def _build_response(*, response: httpx.Response) -> Response[Any]: return Response( status_code=response.status_code, content=response.content, @@ -35,7 +35,7 @@ def _build_response(*, response: httpx.Response) -> Response[None]: def sync_detailed( *, client: Client, -) -> Response[None]: +) -> Response[Any]: kwargs = _get_kwargs( client=client, ) @@ -50,7 +50,7 @@ def sync_detailed( async def asyncio_detailed( *, client: Client, -) -> Response[None]: +) -> Response[Any]: kwargs = _get_kwargs( client=client, ) diff --git a/end_to_end_tests/golden-record/my_test_api_client/api/tests/optional_value_tests_optional_query_param.py b/end_to_end_tests/golden-record/my_test_api_client/api/tests/optional_value_tests_optional_query_param.py index 55e040f3c..370ba1d45 100644 --- a/end_to_end_tests/golden-record/my_test_api_client/api/tests/optional_value_tests_optional_query_param.py +++ b/end_to_end_tests/golden-record/my_test_api_client/api/tests/optional_value_tests_optional_query_param.py @@ -35,9 +35,9 @@ def _get_kwargs( } -def _parse_response(*, response: httpx.Response) -> Optional[Union[HTTPValidationError, None]]: +def _parse_response(*, response: httpx.Response) -> Optional[Union[Any, HTTPValidationError]]: if response.status_code == 200: - response_200 = None + response_200 = response.json() return response_200 if response.status_code == 422: @@ -47,7 +47,7 @@ def _parse_response(*, response: httpx.Response) -> Optional[Union[HTTPValidatio return None -def _build_response(*, response: httpx.Response) -> Response[Union[HTTPValidationError, None]]: +def _build_response(*, response: httpx.Response) -> Response[Union[Any, HTTPValidationError]]: return Response( status_code=response.status_code, content=response.content, @@ -60,7 +60,7 @@ def sync_detailed( *, client: Client, query_param: Union[Unset, List[str]] = UNSET, -) -> Response[Union[HTTPValidationError, None]]: +) -> Response[Union[Any, HTTPValidationError]]: kwargs = _get_kwargs( client=client, query_param=query_param, @@ -77,7 +77,7 @@ def sync( *, client: Client, query_param: Union[Unset, List[str]] = UNSET, -) -> Optional[Union[HTTPValidationError, None]]: +) -> Optional[Union[Any, HTTPValidationError]]: """Test optional query parameters""" return sync_detailed( @@ -90,7 +90,7 @@ async def asyncio_detailed( *, client: Client, query_param: Union[Unset, List[str]] = UNSET, -) -> Response[Union[HTTPValidationError, None]]: +) -> Response[Union[Any, HTTPValidationError]]: kwargs = _get_kwargs( client=client, query_param=query_param, @@ -106,7 +106,7 @@ async def asyncio( *, client: Client, query_param: Union[Unset, List[str]] = UNSET, -) -> Optional[Union[HTTPValidationError, None]]: +) -> Optional[Union[Any, HTTPValidationError]]: """Test optional query parameters""" return ( diff --git a/end_to_end_tests/golden-record/my_test_api_client/api/tests/post_form_data.py b/end_to_end_tests/golden-record/my_test_api_client/api/tests/post_form_data.py index 8152214c1..5dafd287e 100644 --- a/end_to_end_tests/golden-record/my_test_api_client/api/tests/post_form_data.py +++ b/end_to_end_tests/golden-record/my_test_api_client/api/tests/post_form_data.py @@ -26,7 +26,7 @@ def _get_kwargs( } -def _build_response(*, response: httpx.Response) -> Response[None]: +def _build_response(*, response: httpx.Response) -> Response[Any]: return Response( status_code=response.status_code, content=response.content, @@ -39,7 +39,7 @@ def sync_detailed( *, client: Client, form_data: AFormData, -) -> Response[None]: +) -> Response[Any]: kwargs = _get_kwargs( client=client, form_data=form_data, @@ -56,7 +56,7 @@ async def asyncio_detailed( *, client: Client, form_data: AFormData, -) -> Response[None]: +) -> Response[Any]: kwargs = _get_kwargs( client=client, form_data=form_data, diff --git a/end_to_end_tests/golden-record/my_test_api_client/api/tests/token_with_cookie_auth_token_with_cookie_get.py b/end_to_end_tests/golden-record/my_test_api_client/api/tests/token_with_cookie_auth_token_with_cookie_get.py index 1ca44278f..90cf20b07 100644 --- a/end_to_end_tests/golden-record/my_test_api_client/api/tests/token_with_cookie_auth_token_with_cookie_get.py +++ b/end_to_end_tests/golden-record/my_test_api_client/api/tests/token_with_cookie_auth_token_with_cookie_get.py @@ -26,7 +26,7 @@ def _get_kwargs( } -def _build_response(*, response: httpx.Response) -> Response[None]: +def _build_response(*, response: httpx.Response) -> Response[Any]: return Response( status_code=response.status_code, content=response.content, @@ -39,7 +39,7 @@ def sync_detailed( *, client: Client, my_token: str, -) -> Response[None]: +) -> Response[Any]: kwargs = _get_kwargs( client=client, my_token=my_token, @@ -56,7 +56,7 @@ async def asyncio_detailed( *, client: Client, my_token: str, -) -> Response[None]: +) -> Response[Any]: kwargs = _get_kwargs( client=client, my_token=my_token, diff --git a/end_to_end_tests/golden-record/my_test_api_client/api/tests/unsupported_content_tests_unsupported_content_get.py b/end_to_end_tests/golden-record/my_test_api_client/api/tests/unsupported_content_tests_unsupported_content_get.py index a1d5d5a0d..9c7776899 100644 --- a/end_to_end_tests/golden-record/my_test_api_client/api/tests/unsupported_content_tests_unsupported_content_get.py +++ b/end_to_end_tests/golden-record/my_test_api_client/api/tests/unsupported_content_tests_unsupported_content_get.py @@ -23,7 +23,7 @@ def _get_kwargs( } -def _build_response(*, response: httpx.Response) -> Response[None]: +def _build_response(*, response: httpx.Response) -> Response[Any]: return Response( status_code=response.status_code, content=response.content, @@ -35,7 +35,7 @@ def _build_response(*, response: httpx.Response) -> Response[None]: def sync_detailed( *, client: Client, -) -> Response[None]: +) -> Response[Any]: kwargs = _get_kwargs( client=client, ) @@ -50,7 +50,7 @@ def sync_detailed( async def asyncio_detailed( *, client: Client, -) -> Response[None]: +) -> Response[Any]: kwargs = _get_kwargs( client=client, ) diff --git a/end_to_end_tests/golden-record/my_test_api_client/api/tests/upload_file_tests_upload_post.py b/end_to_end_tests/golden-record/my_test_api_client/api/tests/upload_file_tests_upload_post.py index 4b2d294cb..d72aaae4d 100644 --- a/end_to_end_tests/golden-record/my_test_api_client/api/tests/upload_file_tests_upload_post.py +++ b/end_to_end_tests/golden-record/my_test_api_client/api/tests/upload_file_tests_upload_post.py @@ -33,9 +33,9 @@ def _get_kwargs( } -def _parse_response(*, response: httpx.Response) -> Optional[Union[HTTPValidationError, None]]: +def _parse_response(*, response: httpx.Response) -> Optional[Union[Any, HTTPValidationError]]: if response.status_code == 200: - response_200 = None + response_200 = response.json() return response_200 if response.status_code == 422: @@ -45,7 +45,7 @@ def _parse_response(*, response: httpx.Response) -> Optional[Union[HTTPValidatio return None -def _build_response(*, response: httpx.Response) -> Response[Union[HTTPValidationError, None]]: +def _build_response(*, response: httpx.Response) -> Response[Union[Any, HTTPValidationError]]: return Response( status_code=response.status_code, content=response.content, @@ -59,7 +59,7 @@ def sync_detailed( client: Client, multipart_data: BodyUploadFileTestsUploadPost, keep_alive: Union[Unset, bool] = UNSET, -) -> Response[Union[HTTPValidationError, None]]: +) -> Response[Union[Any, HTTPValidationError]]: kwargs = _get_kwargs( client=client, multipart_data=multipart_data, @@ -78,7 +78,7 @@ def sync( client: Client, multipart_data: BodyUploadFileTestsUploadPost, keep_alive: Union[Unset, bool] = UNSET, -) -> Optional[Union[HTTPValidationError, None]]: +) -> Optional[Union[Any, HTTPValidationError]]: """Upload a file""" return sync_detailed( @@ -93,7 +93,7 @@ async def asyncio_detailed( client: Client, multipart_data: BodyUploadFileTestsUploadPost, keep_alive: Union[Unset, bool] = UNSET, -) -> Response[Union[HTTPValidationError, None]]: +) -> Response[Union[Any, HTTPValidationError]]: kwargs = _get_kwargs( client=client, multipart_data=multipart_data, @@ -111,7 +111,7 @@ async def asyncio( client: Client, multipart_data: BodyUploadFileTestsUploadPost, keep_alive: Union[Unset, bool] = UNSET, -) -> Optional[Union[HTTPValidationError, None]]: +) -> Optional[Union[Any, HTTPValidationError]]: """Upload a file""" return ( 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 0e2c06d83..a7a68874a 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 @@ -22,13 +22,14 @@ class AModel: a_camel_date_time: Union[datetime.date, datetime.datetime] a_date: datetime.date required_not_nullable: str - one_of_models: Union[FreeFormModel, ModelWithUnionProperty] + one_of_models: Union[Any, FreeFormModel, ModelWithUnionProperty] model: ModelWithUnionProperty a_nullable_date: Optional[datetime.date] required_nullable: Optional[str] nullable_one_of_models: Union[FreeFormModel, ModelWithUnionProperty, None] nullable_model: Optional[ModelWithUnionProperty] an_allof_enum_with_overridden_default: AnAllOfEnum = AnAllOfEnum.OVERRIDDEN_DEFAULT + any_value: Union[Unset, Any] = UNSET 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 @@ -56,11 +57,16 @@ def to_dict(self) -> Dict[str, Any]: if isinstance(self.one_of_models, FreeFormModel): one_of_models = self.one_of_models.to_dict() - else: + elif isinstance(self.one_of_models, ModelWithUnionProperty): one_of_models = self.one_of_models.to_dict() + else: + one_of_models = self.one_of_models + model = self.model.to_dict() + any_value = self.any_value + an_optional_allof_enum: Union[Unset, str] = UNSET if not isinstance(self.an_optional_allof_enum, Unset): an_optional_allof_enum = self.an_optional_allof_enum.value @@ -154,6 +160,8 @@ def to_dict(self) -> Dict[str, Any]: "nullable_model": nullable_model, } ) + if any_value is not UNSET: + field_dict["any_value"] = any_value if an_optional_allof_enum is not UNSET: field_dict["an_optional_allof_enum"] = an_optional_allof_enum if nested_list_of_enums is not UNSET: @@ -205,7 +213,7 @@ def _parse_a_camel_date_time(data: object) -> Union[datetime.date, datetime.date required_not_nullable = d.pop("required_not_nullable") - def _parse_one_of_models(data: object) -> Union[FreeFormModel, ModelWithUnionProperty]: + def _parse_one_of_models(data: object) -> Union[Any, FreeFormModel, ModelWithUnionProperty]: try: if not isinstance(data, dict): raise TypeError() @@ -214,16 +222,24 @@ def _parse_one_of_models(data: object) -> Union[FreeFormModel, ModelWithUnionPro return one_of_models_type_0 except: # noqa: E722 pass - if not isinstance(data, dict): - raise TypeError() - one_of_models_type_1 = ModelWithUnionProperty.from_dict(data) + try: + if not isinstance(data, dict): + raise TypeError() + one_of_models_type_1 = ModelWithUnionProperty.from_dict(data) + + return one_of_models_type_1 + except: # noqa: E722 + pass + one_of_models_type_2 = data - return one_of_models_type_1 + return one_of_models_type_2 one_of_models = _parse_one_of_models(d.pop("one_of_models")) model = ModelWithUnionProperty.from_dict(d.pop("model")) + any_value = d.pop("any_value", UNSET) + _an_optional_allof_enum = d.pop("an_optional_allof_enum", UNSET) an_optional_allof_enum: Union[Unset, AnAllOfEnum] if isinstance(_an_optional_allof_enum, Unset): @@ -387,6 +403,7 @@ def _parse_not_required_nullable_one_of_models( required_not_nullable=required_not_nullable, one_of_models=one_of_models, model=model, + any_value=any_value, an_optional_allof_enum=an_optional_allof_enum, nested_list_of_enums=nested_list_of_enums, a_nullable_date=a_nullable_date, diff --git a/end_to_end_tests/openapi.json b/end_to_end_tests/openapi.json index 4d40f108e..784942ba2 100644 --- a/end_to_end_tests/openapi.json +++ b/end_to_end_tests/openapi.json @@ -858,6 +858,7 @@ ], "type": "object", "properties": { + "any_value": {}, "an_enum_value": { "$ref": "#/components/schemas/AnEnum" }, @@ -947,7 +948,8 @@ }, { "$ref": "#/components/schemas/ModelWithUnionProperty" - } + }, + {} ], "nullable": false }, diff --git a/openapi_python_client/parser/openapi.py b/openapi_python_client/parser/openapi.py index a1bd5489b..e92038ab6 100644 --- a/openapi_python_client/parser/openapi.py +++ b/openapi_python_client/parser/openapi.py @@ -325,7 +325,7 @@ def response_type(self) -> str: """Get the Python type of any response from this endpoint""" types = sorted({response.prop.get_type_string() for response in self.responses}) if len(types) == 0: - return "None" + return "Any" if len(types) == 1: return self.responses[0].prop.get_type_string() return f"Union[{', '.join(types)}]" diff --git a/openapi_python_client/parser/properties/__init__.py b/openapi_python_client/parser/properties/__init__.py index 243554fb6..e4800c6af 100644 --- a/openapi_python_client/parser/properties/__init__.py +++ b/openapi_python_client/parser/properties/__init__.py @@ -1,8 +1,8 @@ __all__ = [ + "AnyProperty", "Class", "EnumProperty", "ModelProperty", - "NoneProperty", "Property", "Schemas", "build_schemas", @@ -26,12 +26,12 @@ @attr.s(auto_attribs=True, frozen=True) -class NoneProperty(Property): - """A property that is always None (used for empty schemas)""" +class AnyProperty(Property): + """A property that can be any type (used for empty schemas)""" - _type_string: ClassVar[str] = "None" - _json_type_string: ClassVar[str] = "None" - template: ClassVar[Optional[str]] = "none_property.py.jinja" + _type_string: ClassVar[str] = "Any" + _json_type_string: ClassVar[str] = "Any" + template: ClassVar[Optional[str]] = "any_property.py.jinja" @attr.s(auto_attribs=True, frozen=True) @@ -499,7 +499,7 @@ def _property_from_data( data=data, name=name, schemas=schemas, required=required, parent_name=parent_name, config=config ) elif not data.type: - return NoneProperty(name=name, required=required, nullable=False, default=None), schemas + return AnyProperty(name=name, required=required, nullable=False, default=None), schemas return PropertyError(data=data, detail=f"unknown type {data.type}"), schemas diff --git a/openapi_python_client/parser/responses.py b/openapi_python_client/parser/responses.py index ffa703d6f..cbda7a140 100644 --- a/openapi_python_client/parser/responses.py +++ b/openapi_python_client/parser/responses.py @@ -7,7 +7,7 @@ from .. import Config from .. import schema as oai from .errors import ParseError, PropertyError -from .properties import NoneProperty, Property, Schemas, property_from_data +from .properties import AnyProperty, Property, Schemas, property_from_data @attr.s(auto_attribs=True, frozen=True) @@ -28,10 +28,10 @@ class Response: def empty_response(status_code: int, response_name: str) -> Response: - """Return an empty response, for when no response type is defined""" + """Return an untyped response, for when no response type is defined""" return Response( status_code=status_code, - prop=NoneProperty( + prop=AnyProperty( name=response_name, default=None, nullable=False, diff --git a/openapi_python_client/templates/endpoint_module.py.jinja b/openapi_python_client/templates/endpoint_module.py.jinja index a55ea14cd..0624394f0 100644 --- a/openapi_python_client/templates/endpoint_module.py.jinja +++ b/openapi_python_client/templates/endpoint_module.py.jinja @@ -12,7 +12,7 @@ from ...types import Response, UNSET {% from "endpoint_macros.py.jinja" import header_params, cookie_params, query_params, json_body, multipart_body, arguments, client, kwargs, parse_response %} {% set return_string = endpoint.response_type() %} -{% set parsed_responses = (endpoint.responses | length > 0) and return_string != "None" %} +{% set parsed_responses = (endpoint.responses | length > 0) and return_string != "Any" %} def _get_kwargs( {{ arguments(endpoint) | indent(4) }} diff --git a/openapi_python_client/templates/property_templates/none_property.py.jinja b/openapi_python_client/templates/property_templates/any_property.py.jinja similarity index 52% rename from openapi_python_client/templates/property_templates/none_property.py.jinja rename to openapi_python_client/templates/property_templates/any_property.py.jinja index 864802c28..18ccda75a 100644 --- a/openapi_python_client/templates/property_templates/none_property.py.jinja +++ b/openapi_python_client/templates/property_templates/any_property.py.jinja @@ -1,9 +1,7 @@ {% macro construct(property, source, initial_value="None") %} -{{ property.python_name }} = {{ initial_value }} +{{ property.python_name }} = {{ source }} {% endmacro %} -{% macro check_type_for_construct(property, source) %}{{ source }} is None{% endmacro %} - {% macro transform(property, source, destination, declare_type=True, stringify=False) %} -{{ destination }} = None +{{ destination }} = {{ source }} {% endmacro %} diff --git a/openapi_python_client/templates/property_templates/union_property.py.jinja b/openapi_python_client/templates/property_templates/union_property.py.jinja index ce988a913..859207dda 100644 --- a/openapi_python_client/templates/property_templates/union_property.py.jinja +++ b/openapi_python_client/templates/property_templates/union_property.py.jinja @@ -9,20 +9,21 @@ def _parse_{{ property.python_name }}(data: object) -> {{ property.get_type_stri return data {% endif %} {% for inner_property in property.inner_properties_with_template() %} - {% if not loop.last or property.has_properties_without_templates %} + {% import "property_templates/" + inner_property.template as inner_template %} + {% if inner_template.check_type_for_construct and (not loop.last or property.has_properties_without_templates) %} try: - {% from "property_templates/" + inner_property.template import construct, check_type_for_construct %} - if not {{ check_type_for_construct(inner_property, "data") }}: + if not {{ inner_template.check_type_for_construct(inner_property, "data") }}: raise TypeError() - {{ construct(inner_property, "data", initial_value="UNSET") | indent(8) }} + {{ inner_template.construct(inner_property, "data", initial_value="UNSET") | indent(8) }} return {{ inner_property.python_name }} except: # noqa: E722 pass - {% else %}{# Don't do try/except for the last one #} - {% from "property_templates/" + inner_property.template import construct, check_type_for_construct %} - if not {{ check_type_for_construct(inner_property, "data") }}: + {% else %}{# Don't do try/except for the last one nor any properties with no type checking #} + {% if inner_template.check_type_for_construct %} + if not {{ inner_template.check_type_for_construct(inner_property, "data") }}: raise TypeError() - {{ construct(inner_property, "data", initial_value="UNSET") | indent(4) }} + {% endif %} + {{ inner_template.construct(inner_property, "data", initial_value="UNSET") | indent(4) }} return {{ inner_property.python_name }} {% endif %} {% endfor %} @@ -34,9 +35,6 @@ def _parse_{{ property.python_name }}(data: object) -> {{ property.get_type_stri {{ property.python_name }} = _parse_{{ property.python_name }}({{ source }}) {% endmacro %} -{# For now we assume there will be no unions of unions #} -{% macro check_type_for_construct(property, source) %}True{% endmacro %} - {% macro transform(property, source, destination, declare_type=True, stringify=False) %} {% if not property.required or property.nullable %} {{ destination }}{% if declare_type %}: {{ property.get_type_string(json=True) }}{% endif %} diff --git a/tests/test_parser/test_openapi.py b/tests/test_parser/test_openapi.py index 05e8d4255..df234b8aa 100644 --- a/tests/test_parser/test_openapi.py +++ b/tests/test_parser/test_openapi.py @@ -873,7 +873,7 @@ def test_from_data_no_security(self, mocker): @pytest.mark.parametrize( "response_types, expected", - (([], "None"), (["Something"], "Something"), (["First", "Second", "Second"], "Union[First, Second]")), + (([], "Any"), (["Something"], "Something"), (["First", "Second", "Second"], "Union[First, Second]")), ) def test_response_type(self, response_types, expected): endpoint = self.make_endpoint() diff --git a/tests/test_parser/test_properties/test_init.py b/tests/test_parser/test_properties/test_init.py index 57cf3ad22..574dcf7e9 100644 --- a/tests/test_parser/test_properties/test_init.py +++ b/tests/test_parser/test_properties/test_init.py @@ -850,7 +850,7 @@ def test_property_from_data_unsupported_type(self, mocker): ) def test_property_from_data_no_valid_props_in_data(self): - from openapi_python_client.parser.properties import NoneProperty, Schemas, property_from_data + from openapi_python_client.parser.properties import AnyProperty, Schemas, property_from_data schemas = Schemas() data = oai.Schema() @@ -859,7 +859,7 @@ def test_property_from_data_no_valid_props_in_data(self): name="blah", required=True, data=data, schemas=schemas, parent_name="parent", config=MagicMock() ) - assert prop == NoneProperty(name="blah", required=True, nullable=False, default=None) + assert prop == AnyProperty(name="blah", required=True, nullable=False, default=None) assert new_schemas == schemas def test_property_from_data_validation_error(self, mocker): diff --git a/tests/test_parser/test_responses.py b/tests/test_parser/test_responses.py index 025172388..d262c8a0d 100644 --- a/tests/test_parser/test_responses.py +++ b/tests/test_parser/test_responses.py @@ -2,7 +2,7 @@ import openapi_python_client.schema as oai from openapi_python_client.parser.errors import ParseError, PropertyError -from openapi_python_client.parser.properties import NoneProperty, Schemas, StringProperty +from openapi_python_client.parser.properties import AnyProperty, Schemas, StringProperty MODULE_NAME = "openapi_python_client.parser.responses" @@ -20,7 +20,7 @@ def test_response_from_data_no_content(): assert response == Response( status_code=200, - prop=NoneProperty(name="response_200", default=None, nullable=False, required=True), + prop=AnyProperty(name="response_200", default=None, nullable=False, required=True), source="None", ) @@ -46,7 +46,7 @@ def test_response_from_data_no_content_schema(): assert response == Response( status_code=200, - prop=NoneProperty(name="response_200", default=None, nullable=False, required=True), + prop=AnyProperty(name="response_200", default=None, nullable=False, required=True), source="None", )