Browse Source

Merge branch 'master' into fix-duplicate-special-dependency-handling

pull/12406/head
Peter Volf 7 months ago
committed by GitHub
parent
commit
c872fb7ecc
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 5
      .github/workflows/deploy-docs.yml
  2. 4
      docs/en/data/external_links.yml
  3. 8
      docs/en/docs/release-notes.md
  4. 258
      docs/pt/docs/how-to/separate-openapi-schemas.md
  5. 2
      fastapi/param_functions.py
  6. 4
      fastapi/security/oauth2.py

5
.github/workflows/deploy-docs.yml

@ -62,7 +62,10 @@ jobs:
env: env:
PROJECT_NAME: fastapitiangolo PROJECT_NAME: fastapitiangolo
BRANCH: ${{ ( github.event.workflow_run.head_repository.full_name == github.repository && github.event.workflow_run.head_branch == 'master' && 'main' ) || ( github.event.workflow_run.head_sha ) }} BRANCH: ${{ ( github.event.workflow_run.head_repository.full_name == github.repository && github.event.workflow_run.head_branch == 'master' && 'main' ) || ( github.event.workflow_run.head_sha ) }}
uses: cloudflare/wrangler-action@v3 # TODO: Use v3 when it's fixed, probably in v3.11
# https://github.com/cloudflare/wrangler-action/issues/307
uses: cloudflare/[email protected]
# uses: cloudflare/wrangler-action@v3
with: with:
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }} apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }} accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}

4
docs/en/data/external_links.yml

@ -339,6 +339,10 @@ Articles:
link: https://qiita.com/mtitg/items/47770e9a562dd150631d link: https://qiita.com/mtitg/items/47770e9a562dd150631d
title: FastAPI|DB接続してCRUDするPython製APIサーバーを構築 title: FastAPI|DB接続してCRUDするPython製APIサーバーを構築
Portuguese: Portuguese:
- author: Eduardo Mendes
author_link: https://bolha.us/@dunossauro
link: https://fastapidozero.dunossauro.com/
title: FastAPI do ZERO
- author: Jessica Temporal - author: Jessica Temporal
author_link: https://jtemporal.com/socials author_link: https://jtemporal.com/socials
link: https://jtemporal.com/dicas-para-migrar-de-flask-para-fastapi-e-vice-versa/ link: https://jtemporal.com/dicas-para-migrar-de-flask-para-fastapi-e-vice-versa/

8
docs/en/docs/release-notes.md

@ -9,10 +9,17 @@ hide:
### Docs ### Docs
* 📝 Add External Link: FastAPI do Zero. PR [#12533](https://github.com/fastapi/fastapi/pull/12533) by [@rennerocha](https://github.com/rennerocha).
* 📝 Fix minor typos. PR [#12516](https://github.com/fastapi/fastapi/pull/12516) by [@kkirsche](https://github.com/kkirsche).
* 🌐 Fix rendering issue in translations. PR [#12509](https://github.com/fastapi/fastapi/pull/12509) by [@alejsdev](https://github.com/alejsdev). * 🌐 Fix rendering issue in translations. PR [#12509](https://github.com/fastapi/fastapi/pull/12509) by [@alejsdev](https://github.com/alejsdev).
### Translations ### Translations
* 🌐 Add Portuguese translation for `docs/pt/docs/how-to/separate-openapi-schemas.md`. PR [#12518](https://github.com/fastapi/fastapi/pull/12518) by [@ilacftemp](https://github.com/ilacftemp).
* 🌐 Update Traditional Chinese translation for `docs/zh-hant/docs/deployment/index.md`. PR [#12521](https://github.com/fastapi/fastapi/pull/12521) by [@codingjenny](https://github.com/codingjenny).
* 🌐 Update Traditional Chinese translation for `docs/zh-hant/docs/deployment/cloud.md`. PR [#12522](https://github.com/fastapi/fastapi/pull/12522) by [@codingjenny](https://github.com/codingjenny).
* 🌐 Update Traditional Chinese translation for `docs/zh-hant/docs/how-to/index.md`. PR [#12523](https://github.com/fastapi/fastapi/pull/12523) by [@codingjenny](https://github.com/codingjenny).
* 🌐 Update Traditional Chinese translation for `docs/zh-hant/docs/tutorial/index.md`. PR [#12524](https://github.com/fastapi/fastapi/pull/12524) by [@codingjenny](https://github.com/codingjenny).
* 🌐 Add Traditional Chinese translation for `docs/zh-hant/docs/how-to/index.md`. PR [#12468](https://github.com/fastapi/fastapi/pull/12468) by [@codingjenny](https://github.com/codingjenny). * 🌐 Add Traditional Chinese translation for `docs/zh-hant/docs/how-to/index.md`. PR [#12468](https://github.com/fastapi/fastapi/pull/12468) by [@codingjenny](https://github.com/codingjenny).
* 🌐 Add Traditional Chinese translation for `docs/zh-hant/docs/tutorial/index.md`. PR [#12466](https://github.com/fastapi/fastapi/pull/12466) by [@codingjenny](https://github.com/codingjenny). * 🌐 Add Traditional Chinese translation for `docs/zh-hant/docs/tutorial/index.md`. PR [#12466](https://github.com/fastapi/fastapi/pull/12466) by [@codingjenny](https://github.com/codingjenny).
* 🌐 Add Portuguese translation for `docs/pt/docs/tutorial/header-param-models.md`. PR [#12437](https://github.com/fastapi/fastapi/pull/12437) by [@Joao-Pedro-P-Holanda](https://github.com/Joao-Pedro-P-Holanda). * 🌐 Add Portuguese translation for `docs/pt/docs/tutorial/header-param-models.md`. PR [#12437](https://github.com/fastapi/fastapi/pull/12437) by [@Joao-Pedro-P-Holanda](https://github.com/Joao-Pedro-P-Holanda).
@ -22,6 +29,7 @@ hide:
### Internal ### Internal
* 👷 Update GitHub Action to deploy docs previews to handle missing deploy comments. PR [#12527](https://github.com/fastapi/fastapi/pull/12527) by [@tiangolo](https://github.com/tiangolo).
* ⬆ [pre-commit.ci] pre-commit autoupdate. PR [#12505](https://github.com/fastapi/fastapi/pull/12505) by [@pre-commit-ci[bot]](https://github.com/apps/pre-commit-ci). * ⬆ [pre-commit.ci] pre-commit autoupdate. PR [#12505](https://github.com/fastapi/fastapi/pull/12505) by [@pre-commit-ci[bot]](https://github.com/apps/pre-commit-ci).
## 0.115.3 ## 0.115.3

258
docs/pt/docs/how-to/separate-openapi-schemas.md

@ -0,0 +1,258 @@
# Esquemas OpenAPI Separados para Entrada e Saída ou Não
Ao usar **Pydantic v2**, o OpenAPI gerado é um pouco mais exato e **correto** do que antes. 😎
Inclusive, em alguns casos, ele terá até **dois JSON Schemas** no OpenAPI para o mesmo modelo Pydantic, para entrada e saída, dependendo se eles possuem **valores padrão**.
Vamos ver como isso funciona e como alterar se for necessário.
## Modelos Pydantic para Entrada e Saída
Digamos que você tenha um modelo Pydantic com valores padrão, como este:
//// tab | Python 3.10+
```Python hl_lines="7"
{!> ../../docs_src/separate_openapi_schemas/tutorial001_py310.py[ln:1-7]!}
# Code below omitted 👇
```
<details>
<summary>👀 Visualização completa do arquivo</summary>
```Python
{!> ../../docs_src/separate_openapi_schemas/tutorial001_py310.py!}
```
</details>
////
//// tab | Python 3.9+
```Python hl_lines="9"
{!> ../../docs_src/separate_openapi_schemas/tutorial001_py39.py[ln:1-9]!}
# Code below omitted 👇
```
<details>
<summary>👀 Visualização completa do arquivo</summary>
```Python
{!> ../../docs_src/separate_openapi_schemas/tutorial001_py39.py!}
```
</details>
////
//// tab | Python 3.8+
```Python hl_lines="9"
{!> ../../docs_src/separate_openapi_schemas/tutorial001.py[ln:1-9]!}
# Code below omitted 👇
```
<details>
<summary>👀 Visualização completa do arquivo</summary>
```Python
{!> ../../docs_src/separate_openapi_schemas/tutorial001.py!}
```
</details>
////
### Modelo para Entrada
Se você usar esse modelo como entrada, como aqui:
//// tab | Python 3.10+
```Python hl_lines="14"
{!> ../../docs_src/separate_openapi_schemas/tutorial001_py310.py[ln:1-15]!}
# Code below omitted 👇
```
<details>
<summary>👀 Visualização completa do arquivo</summary>
```Python
{!> ../../docs_src/separate_openapi_schemas/tutorial001_py310.py!}
```
</details>
////
//// tab | Python 3.9+
```Python hl_lines="16"
{!> ../../docs_src/separate_openapi_schemas/tutorial001_py39.py[ln:1-17]!}
# Code below omitted 👇
```
<details>
<summary>👀 Visualização completa do arquivo</summary>
```Python
{!> ../../docs_src/separate_openapi_schemas/tutorial001_py39.py!}
```
</details>
////
//// tab | Python 3.8+
```Python hl_lines="16"
{!> ../../docs_src/separate_openapi_schemas/tutorial001.py[ln:1-17]!}
# Code below omitted 👇
```
<details>
<summary>👀 Visualização completa do arquivo</summary>
```Python
{!> ../../docs_src/separate_openapi_schemas/tutorial001.py!}
```
</details>
////
... então o campo `description` não será obrigatório. Porque ele tem um valor padrão de `None`.
### Modelo de Entrada na Documentação
Você pode confirmar que na documentação, o campo `description` não tem um **asterisco vermelho**, não é marcado como obrigatório:
<div class="screenshot">
<img src="/img/tutorial/separate-openapi-schemas/image01.png">
</div>
### Modelo para Saída
Mas se você usar o mesmo modelo como saída, como aqui:
//// tab | Python 3.10+
```Python hl_lines="19"
{!> ../../docs_src/separate_openapi_schemas/tutorial001_py310.py!}
```
////
//// tab | Python 3.9+
```Python hl_lines="21"
{!> ../../docs_src/separate_openapi_schemas/tutorial001_py39.py!}
```
////
//// tab | Python 3.8+
```Python hl_lines="21"
{!> ../../docs_src/separate_openapi_schemas/tutorial001.py!}
```
////
... então, como `description` tem um valor padrão, se você **não retornar nada** para esse campo, ele ainda terá o **valor padrão**.
### Modelo para Dados de Resposta de Saída
Se você interagir com a documentação e verificar a resposta, mesmo que o código não tenha adicionado nada em um dos campos `description`, a resposta JSON contém o valor padrão (`null`):
<div class="screenshot">
<img src="/img/tutorial/separate-openapi-schemas/image02.png">
</div>
Isso significa que ele **sempre terá um valor**, só que às vezes o valor pode ser `None` (ou `null` em termos de JSON).
Isso quer dizer que, os clientes que usam sua API não precisam verificar se o valor existe ou não, eles podem **assumir que o campo sempre estará lá**, mas que em alguns casos terá o valor padrão de `None`.
A maneira de descrever isso no OpenAPI é marcar esse campo como **obrigatório**, porque ele sempre estará lá.
Por causa disso, o JSON Schema para um modelo pode ser diferente dependendo se ele é usado para **entrada ou saída**:
* para **entrada**, o `description` **não será obrigatório**
* para **saída**, ele será **obrigatório** (e possivelmente `None`, ou em termos de JSON, `null`)
### Modelo para Saída na Documentação
Você pode verificar o modelo de saída na documentação também, ambos `name` e `description` são marcados como **obrigatórios** com um **asterisco vermelho**:
<div class="screenshot">
<img src="/img/tutorial/separate-openapi-schemas/image03.png">
</div>
### Modelo para Entrada e Saída na Documentação
E se você verificar todos os Schemas disponíveis (JSON Schemas) no OpenAPI, verá que há dois, um `Item-Input` e um `Item-Output`.
Para `Item-Input`, `description` **não é obrigatório**, não tem um asterisco vermelho.
Mas para `Item-Output`, `description` **é obrigatório**, tem um asterisco vermelho.
<div class="screenshot">
<img src="/img/tutorial/separate-openapi-schemas/image04.png">
</div>
Com esse recurso do **Pydantic v2**, sua documentação da API fica mais **precisa**, e se você tiver clientes e SDKs gerados automaticamente, eles serão mais precisos também, proporcionando uma melhor **experiência para desenvolvedores** e consistência. 🎉
## Não Separe Schemas
Agora, há alguns casos em que você pode querer ter o **mesmo esquema para entrada e saída**.
Provavelmente, o principal caso de uso para isso é se você já tem algum código de cliente/SDK gerado automaticamente e não quer atualizar todo o código de cliente/SDK gerado ainda, você provavelmente vai querer fazer isso em algum momento, mas talvez não agora.
Nesse caso, você pode desativar esse recurso no **FastAPI**, com o parâmetro `separate_input_output_schemas=False`.
/// info | Informação
O suporte para `separate_input_output_schemas` foi adicionado no FastAPI `0.102.0`. 🤓
///
//// tab | Python 3.10+
```Python hl_lines="10"
{!> ../../docs_src/separate_openapi_schemas/tutorial002_py310.py!}
```
////
//// tab | Python 3.9+
```Python hl_lines="12"
{!> ../../docs_src/separate_openapi_schemas/tutorial002_py39.py!}
```
////
//// tab | Python 3.8+
```Python hl_lines="12"
{!> ../../docs_src/separate_openapi_schemas/tutorial002.py!}
```
////
### Mesmo Esquema para Modelos de Entrada e Saída na Documentação
E agora haverá um único esquema para entrada e saída para o modelo, apenas `Item`, e `description` **não será obrigatório**:
<div class="screenshot">
<img src="/img/tutorial/separate-openapi-schemas/image05.png">
</div>
Esse é o mesmo comportamento do Pydantic v1. 🤓

2
fastapi/param_functions.py

@ -2298,7 +2298,7 @@ def Security( # noqa: N802
dependency. dependency.
The term "scope" comes from the OAuth2 specification, it seems to be The term "scope" comes from the OAuth2 specification, it seems to be
intentionaly vague and interpretable. It normally refers to permissions, intentionally vague and interpretable. It normally refers to permissions,
in cases to roles. in cases to roles.
These scopes are integrated with OpenAPI (and the API docs at `/docs`). These scopes are integrated with OpenAPI (and the API docs at `/docs`).

4
fastapi/security/oauth2.py

@ -52,7 +52,7 @@ class OAuth2PasswordRequestForm:
``` ```
Note that for OAuth2 the scope `items:read` is a single scope in an opaque string. Note that for OAuth2 the scope `items:read` is a single scope in an opaque string.
You could have custom internal logic to separate it by colon caracters (`:`) or You could have custom internal logic to separate it by colon characters (`:`) or
similar, and get the two parts `items` and `read`. Many applications do that to similar, and get the two parts `items` and `read`. Many applications do that to
group and organize permissions, you could do it as well in your application, just group and organize permissions, you could do it as well in your application, just
know that that it is application specific, it's not part of the specification. know that that it is application specific, it's not part of the specification.
@ -194,7 +194,7 @@ class OAuth2PasswordRequestFormStrict(OAuth2PasswordRequestForm):
``` ```
Note that for OAuth2 the scope `items:read` is a single scope in an opaque string. Note that for OAuth2 the scope `items:read` is a single scope in an opaque string.
You could have custom internal logic to separate it by colon caracters (`:`) or You could have custom internal logic to separate it by colon characters (`:`) or
similar, and get the two parts `items` and `read`. Many applications do that to similar, and get the two parts `items` and `read`. Many applications do that to
group and organize permissions, you could do it as well in your application, just group and organize permissions, you could do it as well in your application, just
know that that it is application specific, it's not part of the specification. know that that it is application specific, it's not part of the specification.

Loading…
Cancel
Save