Skip to content
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

fixes for responses #457

Merged
merged 3 commits into from
Dec 18, 2024
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
5 changes: 5 additions & 0 deletions docs/en/docs/release-notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ hide:

- Allow passing extensions as string.

### Fixed

- OpenAPI responses.
- Enum definitions.

## 3.6.0

### Added
Expand Down
4 changes: 2 additions & 2 deletions docs/en/docs/responses.md
Original file line number Diff line number Diff line change
Expand Up @@ -215,13 +215,13 @@ The wrappers, like Lilya, also accept the classic parameters such as `headers` a
## Response status codes

You need to be mindful when it comes to return a specific status code when using
[JSON](#json), [OrJSON](#orjson) and [UJSON](#ujson) wrappers.
[JSON](#json), [ORJSON](#orjson) and [UJSON](#ujson) wrappers.

Esmerald allows you to pass the status codes via [handler](./routing/handlers.md) and directly via
return of that same response but the if the handler has a `status_code` declared, the returned
`status_code` **takes precedence**.

Let us use an example to be more clear. This example is applied to `JSON`, `UJSON` and `OrJSON`.
Let us use an example to be more clear. This example is applied to `JSON`, `UJSON` and `ORJSON`.

### Status code without declaring in the handler

Expand Down
12 changes: 6 additions & 6 deletions esmerald/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def __repr__(self) -> str:
return str(self)


class HttpMethod(StrEnum, Enum):
class HttpMethod(StrEnum):
GET = "GET"
POST = "POST"
PUT = "PUT"
Expand All @@ -20,7 +20,7 @@ class HttpMethod(StrEnum, Enum):
TRACE = "TRACE"


class MediaType(StrEnum, Enum):
class MediaType(StrEnum):
JSON = "application/json"
HTML = "text/html"
TEXT = "text/plain"
Expand All @@ -30,23 +30,23 @@ class MediaType(StrEnum, Enum):
OCTET = "application/octet-stream"


class OpenAPIMediaType(StrEnum, Enum):
class OpenAPIMediaType(StrEnum):
OPENAPI_YAML = "application/vnd.oai.openapi"
OPENAPI_JSON = "application/vnd.oai.openapi+json"


class EncodingType(StrEnum, Enum):
class EncodingType(StrEnum):
JSON = "application/json"
MULTI_PART = "multipart/form-data"
URL_ENCODED = "application/x-www-form-urlencoded"


class ScopeType(StrEnum, Enum):
class ScopeType(StrEnum):
HTTP = "http"
WEBSOCKET = "websocket"


class ParamType(StrEnum, Enum):
class ParamType(StrEnum):
PATH = "path"
QUERY = "query"
COOKIE = "cookie"
Expand Down
12 changes: 7 additions & 5 deletions esmerald/openapi/responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@


def create_internal_response(
handler: Union["HTTPHandler", Any]
handler: Union["HTTPHandler", Any],
) -> InternalResponse: # pragma: no cover
signature = Signature.from_callable(cast("AnyCallable", handler.fn))
default_descriptions: Dict[Any, str] = {
Expand Down Expand Up @@ -43,20 +43,22 @@ def create_internal_response(
}:
if signature.return_annotation is Template:
internal_response.return_annotation = str
internal_response.media_type = MediaType.HTML
internal_response.media_type = (
handler.content_media_type or handler.media_type or MediaType.HTML
)
elif get_origin(signature.return_annotation) is EsmeraldResponse:
internal_response.return_annotation = get_args(signature.return_annotation)[0] or Any
internal_response.media_type = handler.content_media_type
else:
internal_response.media_type = MediaType.JSON
internal_response.media_type = handler.content_media_type or MediaType.JSON

internal_response.encoding = handler.content_encoding

elif signature.return_annotation is Redirect:
internal_response.media_type = MediaType.JSON
elif signature.return_annotation in (File, Stream):
internal_response.media_type = handler.content_media_type
internal_response.encoding = handler.content_encoding or MediaType.OCTET
internal_response.media_type = handler.content_media_type or MediaType.OCTET
internal_response.encoding = handler.content_encoding
else:
internal_response.media_type = handler.content_media_type
internal_response.encoding = handler.content_encoding
Expand Down
Loading