Skip to content

feat: Allow enums in headers #667

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 3 commits into from
Aug 27, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import httpx

from ...client import Client
from ...models.get_location_header_types_int_enum_header import GetLocationHeaderTypesIntEnumHeader
from ...models.get_location_header_types_string_enum_header import GetLocationHeaderTypesStringEnumHeader
from ...types import UNSET, Response, Unset


Expand All @@ -13,6 +15,8 @@ def _get_kwargs(
string_header: Union[Unset, str] = UNSET,
number_header: Union[Unset, float] = UNSET,
integer_header: Union[Unset, int] = UNSET,
int_enum_header: Union[Unset, GetLocationHeaderTypesIntEnumHeader] = UNSET,
string_enum_header: Union[Unset, GetLocationHeaderTypesStringEnumHeader] = UNSET,
) -> Dict[str, Any]:
url = "{}/location/header/types".format(client.base_url)

Expand All @@ -31,6 +35,12 @@ def _get_kwargs(
if not isinstance(integer_header, Unset):
headers["Integer-Header"] = str(integer_header)

if not isinstance(int_enum_header, Unset):
headers["Int-Enum-Header"] = str(int_enum_header)

if not isinstance(string_enum_header, Unset):
headers["String-Enum-Header"] = str(string_enum_header)

return {
"method": "get",
"url": url,
Expand All @@ -56,13 +66,17 @@ def sync_detailed(
string_header: Union[Unset, str] = UNSET,
number_header: Union[Unset, float] = UNSET,
integer_header: Union[Unset, int] = UNSET,
int_enum_header: Union[Unset, GetLocationHeaderTypesIntEnumHeader] = UNSET,
string_enum_header: Union[Unset, GetLocationHeaderTypesStringEnumHeader] = UNSET,
) -> Response[Any]:
"""
Args:
boolean_header (Union[Unset, bool]):
string_header (Union[Unset, str]):
number_header (Union[Unset, float]):
integer_header (Union[Unset, int]):
int_enum_header (Union[Unset, GetLocationHeaderTypesIntEnumHeader]):
string_enum_header (Union[Unset, GetLocationHeaderTypesStringEnumHeader]):

Returns:
Response[Any]
Expand All @@ -74,6 +88,8 @@ def sync_detailed(
string_header=string_header,
number_header=number_header,
integer_header=integer_header,
int_enum_header=int_enum_header,
string_enum_header=string_enum_header,
)

response = httpx.request(
Expand All @@ -91,13 +107,17 @@ async def asyncio_detailed(
string_header: Union[Unset, str] = UNSET,
number_header: Union[Unset, float] = UNSET,
integer_header: Union[Unset, int] = UNSET,
int_enum_header: Union[Unset, GetLocationHeaderTypesIntEnumHeader] = UNSET,
string_enum_header: Union[Unset, GetLocationHeaderTypesStringEnumHeader] = UNSET,
) -> Response[Any]:
"""
Args:
boolean_header (Union[Unset, bool]):
string_header (Union[Unset, str]):
number_header (Union[Unset, float]):
integer_header (Union[Unset, int]):
int_enum_header (Union[Unset, GetLocationHeaderTypesIntEnumHeader]):
string_enum_header (Union[Unset, GetLocationHeaderTypesStringEnumHeader]):

Returns:
Response[Any]
Expand All @@ -109,6 +129,8 @@ async def asyncio_detailed(
string_header=string_header,
number_header=number_header,
integer_header=integer_header,
int_enum_header=int_enum_header,
string_enum_header=string_enum_header,
)

async with httpx.AsyncClient(verify=client.verify_ssl) as _client:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
from .body_upload_file_tests_upload_post_some_optional_object import BodyUploadFileTestsUploadPostSomeOptionalObject
from .different_enum import DifferentEnum
from .free_form_model import FreeFormModel
from .get_location_header_types_int_enum_header import GetLocationHeaderTypesIntEnumHeader
from .get_location_header_types_string_enum_header import GetLocationHeaderTypesStringEnumHeader
from .http_validation_error import HTTPValidationError
from .import_ import Import
from .model_from_all_of import ModelFromAllOf
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from enum import IntEnum


class GetLocationHeaderTypesIntEnumHeader(IntEnum):
VALUE_1 = 1
VALUE_2 = 2
VALUE_3 = 3

def __str__(self) -> str:
return str(self.value)
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from enum import Enum


class GetLocationHeaderTypesStringEnumHeader(str, Enum):
ONE = "one"
TWO = "two"
THREE = "three"

def __str__(self) -> str:
return str(self.value)
26 changes: 26 additions & 0 deletions end_to_end_tests/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -1049,6 +1049,32 @@
},
"name": "Integer-Header",
"in": "header"
},
{
"in": "header",
"name": "Int-Enum-Header",
"required": false,
"schema": {
"type": "integer",
"enum": [
1,
2,
3
]
}
},
{
"in": "header",
"name": "String-Enum-Header",
"required": false,
"schema": {
"type": "string",
"enum": [
"one",
"two",
"three"
]
}
}
],
"responses": {
Expand Down
8 changes: 8 additions & 0 deletions openapi_python_client/parser/properties/enum_property.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import attr

from ... import schema as oai
from ... import utils
from .property import Property
from .schemas import Class
Expand All @@ -22,6 +23,13 @@ class EnumProperty(Property):

template: ClassVar[str] = "enum_property.py.jinja"

_allowed_locations: ClassVar[Set[oai.ParameterLocation]] = {
oai.ParameterLocation.QUERY,
oai.ParameterLocation.PATH,
oai.ParameterLocation.COOKIE,
oai.ParameterLocation.HEADER,
}

def get_base_type_string(self) -> str:
return self.class_info.name

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,7 @@ if not isinstance({{ source }}, Unset):
{% endif %}
{% endif %}
{% endmacro %}

{% macro transform_header(property, source, destination) %}
{{ destination }} = str({{ source }})
{% endmacro %}