Skip to content

fix: Don't crash when a null is in an enum (fixes #500). Thanks @juspence! #501

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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]:
Expand Down
5 changes: 3 additions & 2 deletions openapi_python_client/parser/properties/enum_property.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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: 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"]] = []
Expand Down