Skip to content

Commit 50374be

Browse files
juspencedbanty
authored andcommitted
fix: Allow None in enum properties
Needed for some OpenAPI schemas generated by django-rest-framework / drf-spectacular.
1 parent 2f82310 commit 50374be

File tree

3 files changed

+7
-6
lines changed

3 files changed

+7
-6
lines changed

openapi_python_client/parser/properties/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ def build_enum_property(
296296
name: str,
297297
required: bool,
298298
schemas: Schemas,
299-
enum: Union[List[str], List[int]],
299+
enum: Union[List[Optional[str]], List[Optional[int]]],
300300
parent_name: Optional[str],
301301
config: Config,
302302
) -> Tuple[Union[EnumProperty, PropertyError], Schemas]:

openapi_python_client/parser/properties/enum_property.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from .property import Property
99
from .schemas import Class
1010

11-
ValueType = Union[str, int]
11+
ValueType = Union[str, int, None]
1212

1313

1414
@attr.s(auto_attribs=True, frozen=True)
@@ -41,8 +41,8 @@ def get_imports(self, *, prefix: str) -> Set[str]:
4141
return imports
4242

4343
@staticmethod
44-
def values_from_list(values: Union[List[str], List[int]]) -> Dict[str, ValueType]:
45-
"""Convert a list of values into dict of {name: value}"""
44+
def values_from_list(values: Union[List[Optional[str]], List[Optional[int]]]) -> Dict[str, ValueType]:
45+
"""Convert a list of values into dict of {name: value}, where value can sometimes be None"""
4646
output: Dict[str, ValueType] = {}
4747

4848
for i, value in enumerate(values):
@@ -60,5 +60,6 @@ def values_from_list(values: Union[List[str], List[int]]) -> Dict[str, ValueType
6060
if key in output:
6161
raise ValueError(f"Duplicate key {key} in Enum")
6262
sanitized_key = utils.snake_case(key).upper()
63-
output[sanitized_key] = utils.remove_string_escapes(value)
63+
output[sanitized_key] = utils.remove_string_escapes(value) if isinstance(value, str) else value
64+
# If value is the string "null", this becomes Python's None instead of a str, so must special-case it
6465
return output

openapi_python_client/schema/openapi_schema_pydantic/schema.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class Schema(BaseModel):
3535
maxProperties: Optional[int] = Field(default=None, ge=0)
3636
minProperties: Optional[int] = Field(default=None, ge=0)
3737
required: Optional[List[str]] = Field(default=None, min_items=1)
38-
enum: Union[None, List[StrictInt], List[StrictStr]] = Field(default=None, min_items=1)
38+
enum: Union[None, List[Optional[StrictInt]], List[Optional[StrictStr]]] = Field(default=None, min_items=1)
3939
type: Optional[DataType] = Field(default=None)
4040
allOf: Optional[List[Union[Reference, "Schema"]]] = None
4141
oneOf: List[Union[Reference, "Schema"]] = []

0 commit comments

Comments
 (0)