From 7e85d50b944485c1b5b9b228832d50b258afa465 Mon Sep 17 00:00:00 2001 From: Dylan Anthony Date: Sat, 25 Sep 2021 21:20:34 -0600 Subject: [PATCH 1/2] fix: Don't crash when a null is in an enum (fixes #500) --- openapi_python_client/schema/openapi_schema_pydantic/schema.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/openapi_python_client/schema/openapi_schema_pydantic/schema.py b/openapi_python_client/schema/openapi_schema_pydantic/schema.py index d1bf5a43e..f4027cc32 100644 --- a/openapi_python_client/schema/openapi_schema_pydantic/schema.py +++ b/openapi_python_client/schema/openapi_schema_pydantic/schema.py @@ -35,7 +35,7 @@ class Schema(BaseModel): maxProperties: Optional[int] = Field(default=None, ge=0) minProperties: Optional[int] = Field(default=None, ge=0) required: Optional[List[str]] = Field(default=None, min_items=1) - enum: Optional[List[Any]] = Field(default=None, min_items=1) + enum: Optional[List[Union[str, int]]] = Field(default=None, min_items=1) type: Optional[DataType] = Field(default=None) allOf: Optional[List[Union[Reference, "Schema"]]] = None oneOf: List[Union[Reference, "Schema"]] = [] From 8779620f862f24e24ea6593b8ec3d3f1e929fb6c Mon Sep 17 00:00:00 2001 From: Dylan Anthony Date: Sat, 25 Sep 2021 21:36:11 -0600 Subject: [PATCH 2/2] fix: Don't allow mixed types in enums. --- openapi_python_client/parser/properties/__init__.py | 2 +- openapi_python_client/parser/properties/enum_property.py | 5 +++-- .../schema/openapi_schema_pydantic/schema.py | 4 ++-- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/openapi_python_client/parser/properties/__init__.py b/openapi_python_client/parser/properties/__init__.py index 22b799f04..ac9c0f4d5 100644 --- a/openapi_python_client/parser/properties/__init__.py +++ b/openapi_python_client/parser/properties/__init__.py @@ -296,7 +296,7 @@ def build_enum_property( name: str, required: bool, schemas: Schemas, - enum: List[Union[str, int]], + enum: Union[List[str], List[int]], parent_name: Optional[str], config: Config, ) -> Tuple[Union[EnumProperty, PropertyError], Schemas]: diff --git a/openapi_python_client/parser/properties/enum_property.py b/openapi_python_client/parser/properties/enum_property.py index ef8f144e0..8cb2f0327 100644 --- a/openapi_python_client/parser/properties/enum_property.py +++ b/openapi_python_client/parser/properties/enum_property.py @@ -1,6 +1,6 @@ __all__ = ["EnumProperty"] -from typing import Any, ClassVar, Dict, List, Optional, Set, Type, Union +from typing import Any, ClassVar, Dict, List, Optional, Set, Type, Union, cast import attr @@ -41,11 +41,12 @@ def get_imports(self, *, prefix: str) -> Set[str]: return imports @staticmethod - def values_from_list(values: List[ValueType]) -> Dict[str, ValueType]: + def values_from_list(values: Union[List[str], List[int]]) -> Dict[str, ValueType]: """Convert a list of values into dict of {name: value}""" output: Dict[str, ValueType] = {} for i, value in enumerate(values): + value = cast(Union[str, int], value) if isinstance(value, int): if value < 0: output[f"VALUE_NEGATIVE_{-value}"] = value diff --git a/openapi_python_client/schema/openapi_schema_pydantic/schema.py b/openapi_python_client/schema/openapi_schema_pydantic/schema.py index f4027cc32..bf8dcd2d0 100644 --- a/openapi_python_client/schema/openapi_schema_pydantic/schema.py +++ b/openapi_python_client/schema/openapi_schema_pydantic/schema.py @@ -1,6 +1,6 @@ from typing import Any, Dict, List, Optional, Union -from pydantic import BaseModel, Field +from pydantic import BaseModel, Field, StrictInt, StrictStr from ..data_type import DataType from .discriminator import Discriminator @@ -35,7 +35,7 @@ class Schema(BaseModel): maxProperties: Optional[int] = Field(default=None, ge=0) minProperties: Optional[int] = Field(default=None, ge=0) required: Optional[List[str]] = Field(default=None, min_items=1) - enum: Optional[List[Union[str, int]]] = Field(default=None, min_items=1) + enum: Union[None, List[StrictInt], List[StrictStr]] = Field(default=None, min_items=1) type: Optional[DataType] = Field(default=None) allOf: Optional[List[Union[Reference, "Schema"]]] = None oneOf: List[Union[Reference, "Schema"]] = []