Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4d7bfbb

Browse files
authored
Merge branch 'master' into master
2 parents e75dff0 + fbe1a80 commit 4d7bfbb

15 files changed

Lines changed: 125 additions & 12 deletions

File tree

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ The key features are:
4949
<a href="https://bit.ly/3dmXC5S" target="_blank" title="The data structure for unstructured multimodal data"><img src="https://fastapi.tiangolo.com/img/sponsors/docarray.svg"></a>
5050
<a href="https://bit.ly/3JJ7y5C" target="_blank" title="Build cross-modal and multimodal applications on the cloud"><img src="https://fastapi.tiangolo.com/img/sponsors/jina2.svg"></a>
5151
<a href="https://cryptapi.io/" target="_blank" title="CryptAPI: Your easy to use, secure and privacy oriented payment gateway."><img src="https://fastapi.tiangolo.com/img/sponsors/cryptapi.svg"></a>
52-
<a href="https://app.imgwhale.xyz/" target="_blank" title="The ultimate solution to unlimited and forever cloud storage."><img src="https://fastapi.tiangolo.com/img/sponsors/imgwhale.svg"></a>
5352
<a href="https://doist.com/careers/9B437B1615-wa-senior-backend-engineer-python" target="_blank" title="Help us migrate doist to FastAPI"><img src="https://fastapi.tiangolo.com/img/sponsors/doist.svg"></a>
5453
<a href="https://www.deta.sh/?ref=fastapi" target="_blank" title="The launchpad for all your (team's) ideas"><img src="https://fastapi.tiangolo.com/img/sponsors/deta.svg"></a>
5554
<a href="https://www.investsuite.com/jobs" target="_blank" title="Wealthtech jobs with FastAPI"><img src="https://fastapi.tiangolo.com/img/sponsors/investsuite.svg"></a>

docs/en/data/sponsors.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@ gold:
88
- url: https://cryptapi.io/
99
title: "CryptAPI: Your easy to use, secure and privacy oriented payment gateway."
1010
img: https://fastapi.tiangolo.com/img/sponsors/cryptapi.svg
11-
- url: https://app.imgwhale.xyz/
12-
title: The ultimate solution to unlimited and forever cloud storage.
13-
img: https://fastapi.tiangolo.com/img/sponsors/imgwhale.svg
1411
- url: https://doist.com/careers/9B437B1615-wa-senior-backend-engineer-python
1512
title: Help us migrate doist to FastAPI
1613
img: https://fastapi.tiangolo.com/img/sponsors/doist.svg

docs/en/docs/advanced/custom-response.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ For example, if you are squeezing performance, you can install and use <a href="
2121

2222
Import the `Response` class (sub-class) you want to use and declare it in the *path operation decorator*.
2323

24+
For large responses, returning a `Response` directly is much faster than returning a dictionary.
25+
26+
This is because by default, FastAPI will inspect every item inside and make sure it is serializable with JSON, using the same [JSON Compatible Encoder](../tutorial/encoder.md){.internal-link target=_blank} explained in the tutorial. This is what allows you to return **arbitrary objects**, for example database models.
27+
28+
But if you are certain that the content that you are returning is **serializable with JSON**, you can pass it directly to the response class and avoid the extra overhead that FastAPI would have by passing your return content through the `jsonable_encoder` before passing it to the response class.
29+
2430
```Python hl_lines="2 7"
2531
{!../../../docs_src/custom_response/tutorial001b.py!}
2632
```
@@ -244,6 +250,36 @@ You can also use the `response_class` parameter:
244250

245251
In this case, you can return the file path directly from your *path operation* function.
246252

253+
## Custom response class
254+
255+
You can create your own custom response class, inheriting from `Response` and using it.
256+
257+
For example, let's say that you want to use <a href="https://github.com/ijl/orjson" class="external-link" target="_blank">`orjson`</a>, but with some custom settings not used in the included `ORJSONResponse` class.
258+
259+
Let's say you want it to return indented and formatted JSON, so you want to use the orjson option `orjson.OPT_INDENT_2`.
260+
261+
You could create a `CustomORJSONResponse`. The main thing you have to do is create a `Response.render(content)` method that returns the content as `bytes`:
262+
263+
```Python hl_lines="9-14 17"
264+
{!../../../docs_src/custom_response/tutorial009c.py!}
265+
```
266+
267+
Now instead of returning:
268+
269+
```json
270+
{"message": "Hello World"}
271+
```
272+
273+
...this response will return:
274+
275+
```json
276+
{
277+
"message": "Hello World"
278+
}
279+
```
280+
281+
Of course, you will probably find much better ways to take advantage of this than formatting JSON. 😉
282+
247283
## Default response class
248284

249285
When creating a **FastAPI** class instance or an `APIRouter` you can specify which response class to use by default.

docs/en/docs/release-notes.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22

33
## Latest Changes
44

5+
* ♻ Internal small refactor, move `operation_id` parameter position in delete method for consistency with the code. PR [#4474](https://github.com/tiangolo/fastapi/pull/4474) by [@hiel](https://github.com/hiel).
6+
* ✨ Support Python internal description on Pydantic model's docstring. PR [#3032](https://github.com/tiangolo/fastapi/pull/3032) by [@Kludex](https://github.com/Kludex).
7+
* ✨ Update `ORJSONResponse` to support non `str` keys and serializing Numpy arrays. PR [#3892](https://github.com/tiangolo/fastapi/pull/3892) by [@baby5](https://github.com/baby5).
8+
* 🔧 Update sponsors, disable ImgWhale. PR [#5338](https://github.com/tiangolo/fastapi/pull/5338) by [@tiangolo](https://github.com/tiangolo).
9+
* 📝 Update docs for `ORJSONResponse` with details about improving performance. PR [#2615](https://github.com/tiangolo/fastapi/pull/2615) by [@falkben](https://github.com/falkben).
10+
* 📝 Add docs for creating a custom Response class. PR [#5331](https://github.com/tiangolo/fastapi/pull/5331) by [@tiangolo](https://github.com/tiangolo).
11+
* 📝 Add tip about using alias for form data fields. PR [#5329](https://github.com/tiangolo/fastapi/pull/5329) by [@tiangolo](https://github.com/tiangolo).
12+
* 🐛 Fix support for path parameters in WebSockets. PR [#3879](https://github.com/tiangolo/fastapi/pull/3879) by [@davidbrochart](https://github.com/davidbrochart).
513

614
## 0.81.0
715

docs/en/docs/tutorial/request-forms.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ For example, in one of the ways the OAuth2 specification can be used (called "pa
2727

2828
The <abbr title="specification">spec</abbr> requires the fields to be exactly named `username` and `password`, and to be sent as form fields, not JSON.
2929

30-
With `Form` you can declare the same metadata and validation as with `Body` (and `Query`, `Path`, `Cookie`).
30+
With `Form` you can declare the same configurations as with `Body` (and `Query`, `Path`, `Cookie`), including validation, examples, an alias (e.g. `user-name` instead of `username`), etc.
3131

3232
!!! info
3333
`Form` is a class that inherits directly from `Body`.

docs/en/overrides/main.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,12 @@
4040
<img class="sponsor-image" src="/img/sponsors/cryptapi-banner.svg" />
4141
</a>
4242
</div>
43-
<div class="item">
43+
<!-- <div class="item">
4444
<a title="The ultimate solution to unlimited and forever cloud storage." style="display: block; position: relative;" href="https://app.imgwhale.xyz/" target="_blank">
4545
<span class="sponsor-badge">sponsor</span>
4646
<img class="sponsor-image" src="/img/sponsors/imgwhale-banner.svg" />
4747
</a>
48-
</div>
48+
</div> -->
4949
<div class="item">
5050
<a title="Help us migrate doist to FastAPI" style="display: block; position: relative;" href="https://doist.com/careers/9B437B1615-wa-senior-backend-engineer-python" target="_blank">
5151
<span class="sponsor-badge">sponsor</span>

docs_src/custom_response/tutorial001b.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@
66

77
@app.get("/items/", response_class=ORJSONResponse)
88
async def read_items():
9-
return [{"item_id": "Foo"}]
9+
return ORJSONResponse([{"item_id": "Foo"}])
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from typing import Any
2+
3+
import orjson
4+
from fastapi import FastAPI, Response
5+
6+
app = FastAPI()
7+
8+
9+
class CustomORJSONResponse(Response):
10+
media_type = "application/json"
11+
12+
def render(self, content: Any) -> bytes:
13+
assert orjson is not None, "orjson must be installed"
14+
return orjson.dumps(content, option=orjson.OPT_INDENT_2)
15+
16+
17+
@app.get("/", response_class=CustomORJSONResponse)
18+
async def main():
19+
return {"message": "Hello World"}

fastapi/applications.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -635,10 +635,10 @@ def delete(
635635
response_description=response_description,
636636
responses=responses,
637637
deprecated=deprecated,
638+
operation_id=operation_id,
638639
response_model_include=response_model_include,
639640
response_model_exclude=response_model_exclude,
640641
response_model_by_alias=response_model_by_alias,
641-
operation_id=operation_id,
642642
response_model_exclude_unset=response_model_exclude_unset,
643643
response_model_exclude_defaults=response_model_exclude_defaults,
644644
response_model_exclude_none=response_model_exclude_none,

fastapi/responses.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,6 @@ class ORJSONResponse(JSONResponse):
3131

3232
def render(self, content: Any) -> bytes:
3333
assert orjson is not None, "orjson must be installed to use ORJSONResponse"
34-
return orjson.dumps(content)
34+
return orjson.dumps(
35+
content, option=orjson.OPT_NON_STR_KEYS | orjson.OPT_SERIALIZE_NUMPY
36+
)

0 commit comments

Comments
 (0)