mirror of
https://github.com/fastapi/fastapi.git
synced 2025-12-29 01:00:51 -05:00
Compare commits
28 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
fad35ef43f | ||
|
|
4d57c13055 | ||
|
|
496de1816a | ||
|
|
2cf04ee30d | ||
|
|
ec00f5a90f | ||
|
|
8b46d8821b | ||
|
|
17fcbbe910 | ||
|
|
dcfb8b9dda | ||
|
|
1fc586c3a5 | ||
|
|
bb88a0f94a | ||
|
|
9d1a384f4f | ||
|
|
c144f9fbd3 | ||
|
|
22ccca21fc | ||
|
|
d2a703d5cc | ||
|
|
35aa12b9bd | ||
|
|
c01b5dd96f | ||
|
|
6a657f360d | ||
|
|
7132a69046 | ||
|
|
db7feb5a3e | ||
|
|
448ea5ec82 | ||
|
|
b618e0f9d4 | ||
|
|
ccf50ca477 | ||
|
|
a0ef245067 | ||
|
|
78c94c3f56 | ||
|
|
4b0301b280 | ||
|
|
436932aef5 | ||
|
|
3ea6a4a0b1 | ||
|
|
96dd32718b |
2
.github/workflows/build-docs.yml
vendored
2
.github/workflows/build-docs.yml
vendored
@@ -118,7 +118,7 @@ jobs:
|
||||
path: docs/${{ matrix.lang }}/.cache
|
||||
- name: Build Docs
|
||||
run: python ./scripts/docs.py build-lang ${{ matrix.lang }}
|
||||
- uses: actions/upload-artifact@v4
|
||||
- uses: actions/upload-artifact@v5
|
||||
with:
|
||||
name: docs-site-${{ matrix.lang }}
|
||||
path: ./site/**
|
||||
|
||||
2
.github/workflows/deploy-docs.yml
vendored
2
.github/workflows/deploy-docs.yml
vendored
@@ -49,7 +49,7 @@ jobs:
|
||||
run: |
|
||||
rm -rf ./site
|
||||
mkdir ./site
|
||||
- uses: actions/download-artifact@v5
|
||||
- uses: actions/download-artifact@v6
|
||||
with:
|
||||
path: ./site/
|
||||
pattern: docs-site-*
|
||||
|
||||
2
.github/workflows/smokeshow.yml
vendored
2
.github/workflows/smokeshow.yml
vendored
@@ -34,7 +34,7 @@ jobs:
|
||||
requirements**.txt
|
||||
pyproject.toml
|
||||
- run: uv pip install -r requirements-github-actions.txt
|
||||
- uses: actions/download-artifact@v5
|
||||
- uses: actions/download-artifact@v6
|
||||
with:
|
||||
name: coverage-html
|
||||
path: htmlcov
|
||||
|
||||
6
.github/workflows/test.yml
vendored
6
.github/workflows/test.yml
vendored
@@ -97,7 +97,7 @@ jobs:
|
||||
COVERAGE_FILE: coverage/.coverage.${{ runner.os }}-py${{ matrix.python-version }}
|
||||
CONTEXT: ${{ runner.os }}-py${{ matrix.python-version }}
|
||||
- name: Store coverage files
|
||||
uses: actions/upload-artifact@v4
|
||||
uses: actions/upload-artifact@v5
|
||||
with:
|
||||
name: coverage-${{ matrix.python-version }}-${{ matrix.pydantic-version }}
|
||||
path: coverage
|
||||
@@ -126,7 +126,7 @@ jobs:
|
||||
- name: Install Dependencies
|
||||
run: uv pip install -r requirements-tests.txt
|
||||
- name: Get coverage files
|
||||
uses: actions/download-artifact@v5
|
||||
uses: actions/download-artifact@v6
|
||||
with:
|
||||
pattern: coverage-*
|
||||
path: coverage
|
||||
@@ -136,7 +136,7 @@ jobs:
|
||||
- run: coverage report
|
||||
- run: coverage html --title "Coverage for ${{ github.sha }}"
|
||||
- name: Store coverage HTML
|
||||
uses: actions/upload-artifact@v4
|
||||
uses: actions/upload-artifact@v5
|
||||
with:
|
||||
name: coverage-html
|
||||
path: htmlcov
|
||||
|
||||
@@ -14,7 +14,7 @@ repos:
|
||||
- id: end-of-file-fixer
|
||||
- id: trailing-whitespace
|
||||
- repo: https://github.com/astral-sh/ruff-pre-commit
|
||||
rev: v0.14.1
|
||||
rev: v0.14.2
|
||||
hooks:
|
||||
- id: ruff
|
||||
args:
|
||||
|
||||
@@ -55,6 +55,7 @@ The key features are:
|
||||
<a href="https://www.coderabbit.ai/?utm_source=fastapi&utm_medium=badge&utm_campaign=fastapi" target="_blank" title="Cut Code Review Time & Bugs in Half with CodeRabbit"><img src="https://fastapi.tiangolo.com/img/sponsors/coderabbit.png"></a>
|
||||
<a href="https://subtotal.com/?utm_source=fastapi&utm_medium=sponsorship&utm_campaign=open-source" target="_blank" title="The Gold Standard in Retail Account Linking"><img src="https://fastapi.tiangolo.com/img/sponsors/subtotal.svg"></a>
|
||||
<a href="https://docs.railway.com/guides/fastapi?utm_medium=integration&utm_source=docs&utm_campaign=fastapi" target="_blank" title="Deploy enterprise applications at startup speed"><img src="https://fastapi.tiangolo.com/img/sponsors/railway.png"></a>
|
||||
<a href="https://serpapi.com/?utm_source=fastapi_website" target="_blank" title="SerpApi: Web Search API"><img src="https://fastapi.tiangolo.com/img/sponsors/serpapi.png"></a>
|
||||
<a href="https://databento.com/?utm_source=fastapi&utm_medium=sponsor&utm_content=display" target="_blank" title="Pay as you go for market data"><img src="https://fastapi.tiangolo.com/img/sponsors/databento.svg"></a>
|
||||
<a href="https://speakeasy.com/editor?utm_source=fastapi+repo&utm_medium=github+sponsorship" target="_blank" title="SDKs for your API | Speakeasy"><img src="https://fastapi.tiangolo.com/img/sponsors/speakeasy.png"></a>
|
||||
<a href="https://www.svix.com/" target="_blank" title="Svix - Webhooks as a service"><img src="https://fastapi.tiangolo.com/img/sponsors/svix.svg"></a>
|
||||
|
||||
@@ -26,6 +26,9 @@ gold:
|
||||
- url: https://docs.railway.com/guides/fastapi?utm_medium=integration&utm_source=docs&utm_campaign=fastapi
|
||||
title: Deploy enterprise applications at startup speed
|
||||
img: https://fastapi.tiangolo.com/img/sponsors/railway.png
|
||||
- url: https://serpapi.com/?utm_source=fastapi_website
|
||||
title: "SerpApi: Web Search API"
|
||||
img: https://fastapi.tiangolo.com/img/sponsors/serpapi.png
|
||||
silver:
|
||||
- url: https://databento.com/?utm_source=fastapi&utm_medium=sponsor&utm_content=display
|
||||
title: Pay as you go for market data
|
||||
|
||||
BIN
docs/en/docs/img/sponsors/serpapi-banner.png
Normal file
BIN
docs/en/docs/img/sponsors/serpapi-banner.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 6.3 KiB |
BIN
docs/en/docs/img/sponsors/serpapi.png
Normal file
BIN
docs/en/docs/img/sponsors/serpapi.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 10 KiB |
@@ -7,6 +7,47 @@ hide:
|
||||
|
||||
## Latest Changes
|
||||
|
||||
## 0.120.4
|
||||
|
||||
### Fixes
|
||||
|
||||
* 🐛 Fix security schemes in OpenAPI when added at the top level app. PR [#14266](https://github.com/fastapi/fastapi/pull/14266) by [@YuriiMotov](https://github.com/YuriiMotov).
|
||||
|
||||
## 0.120.3
|
||||
|
||||
### Refactors
|
||||
|
||||
* ♻️ Reduce internal cyclic recursion in dependencies, from 2 functions calling each other to 1 calling itself. PR [#14256](https://github.com/fastapi/fastapi/pull/14256) by [@tiangolo](https://github.com/tiangolo).
|
||||
* ♻️ Refactor internals of dependencies, simplify code and remove `get_param_sub_dependant`. PR [#14255](https://github.com/fastapi/fastapi/pull/14255) by [@tiangolo](https://github.com/tiangolo).
|
||||
* ♻️ Refactor internals of dependencies, simplify using dataclasses. PR [#14254](https://github.com/fastapi/fastapi/pull/14254) by [@tiangolo](https://github.com/tiangolo).
|
||||
|
||||
### Docs
|
||||
|
||||
* 📝 Update note for untranslated pages. PR [#14257](https://github.com/fastapi/fastapi/pull/14257) by [@YuriiMotov](https://github.com/YuriiMotov).
|
||||
|
||||
## 0.120.2
|
||||
|
||||
### Fixes
|
||||
|
||||
* 🐛 Fix separation of schemas with nested models introduced in 0.119.0. PR [#14246](https://github.com/fastapi/fastapi/pull/14246) by [@tiangolo](https://github.com/tiangolo).
|
||||
|
||||
### Internal
|
||||
|
||||
* 🔧 Add sponsor: SerpApi. PR [#14248](https://github.com/fastapi/fastapi/pull/14248) by [@tiangolo](https://github.com/tiangolo).
|
||||
* ⬆ Bump actions/download-artifact from 5 to 6. PR [#14236](https://github.com/fastapi/fastapi/pull/14236) by [@dependabot[bot]](https://github.com/apps/dependabot).
|
||||
* ⬆ [pre-commit.ci] pre-commit autoupdate. PR [#14237](https://github.com/fastapi/fastapi/pull/14237) by [@pre-commit-ci[bot]](https://github.com/apps/pre-commit-ci).
|
||||
* ⬆ Bump actions/upload-artifact from 4 to 5. PR [#14235](https://github.com/fastapi/fastapi/pull/14235) by [@dependabot[bot]](https://github.com/apps/dependabot).
|
||||
|
||||
## 0.120.1
|
||||
|
||||
### Upgrades
|
||||
|
||||
* ⬆️ Bump Starlette to <`0.50.0`. PR [#14234](https://github.com/fastapi/fastapi/pull/14234) by [@YuriiMotov](https://github.com/YuriiMotov).
|
||||
|
||||
### Internal
|
||||
|
||||
* 🔧 Add `license` and `license-files` to `pyproject.toml`, remove `License` from `classifiers`. PR [#14230](https://github.com/fastapi/fastapi/pull/14230) by [@YuriiMotov](https://github.com/YuriiMotov).
|
||||
|
||||
## 0.120.0
|
||||
|
||||
There are no major nor breaking changes in this release. ☕️
|
||||
|
||||
@@ -80,6 +80,12 @@
|
||||
<img class="sponsor-image" src="/img/sponsors/railway-banner.png" />
|
||||
</a>
|
||||
</div>
|
||||
<div class="item">
|
||||
<a title="SerpApi: Web Search API" style="display: block; position: relative;" href="https://serpapi.com/?utm_source=fastapi_website" target="_blank">
|
||||
<span class="sponsor-badge">sponsor</span>
|
||||
<img class="sponsor-image" src="/img/sponsors/serpapi-banner.png" />
|
||||
</a>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{% endblock %}
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
/// warning
|
||||
|
||||
The current page still doesn't have a translation for this language.
|
||||
This page hasn’t been translated into your language yet. 🌍
|
||||
|
||||
But you can help translating it: [Contributing](https://fastapi.tiangolo.com/contributing/){.internal-link target=_blank}.
|
||||
We’re currently switching to an automated translation system 🤖, which will help keep all translations complete and up to date.
|
||||
|
||||
Learn more: [Contributing – Translations](https://fastapi.tiangolo.com/contributing/#translations){.internal-link target=_blank}
|
||||
|
||||
///
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
"""FastAPI framework, high performance, easy to learn, fast to code, ready for production"""
|
||||
|
||||
__version__ = "0.120.0"
|
||||
__version__ = "0.120.4"
|
||||
|
||||
from starlette import status as status
|
||||
|
||||
|
||||
@@ -207,11 +207,31 @@ def get_definitions(
|
||||
override_mode: Union[Literal["validation"], None] = (
|
||||
None if separate_input_output_schemas else "validation"
|
||||
)
|
||||
flat_models = get_flat_models_from_fields(fields, known_models=set())
|
||||
flat_model_fields = [
|
||||
ModelField(field_info=FieldInfo(annotation=model), name=model.__name__)
|
||||
for model in flat_models
|
||||
validation_fields = [field for field in fields if field.mode == "validation"]
|
||||
serialization_fields = [field for field in fields if field.mode == "serialization"]
|
||||
flat_validation_models = get_flat_models_from_fields(
|
||||
validation_fields, known_models=set()
|
||||
)
|
||||
flat_serialization_models = get_flat_models_from_fields(
|
||||
serialization_fields, known_models=set()
|
||||
)
|
||||
flat_validation_model_fields = [
|
||||
ModelField(
|
||||
field_info=FieldInfo(annotation=model),
|
||||
name=model.__name__,
|
||||
mode="validation",
|
||||
)
|
||||
for model in flat_validation_models
|
||||
]
|
||||
flat_serialization_model_fields = [
|
||||
ModelField(
|
||||
field_info=FieldInfo(annotation=model),
|
||||
name=model.__name__,
|
||||
mode="serialization",
|
||||
)
|
||||
for model in flat_serialization_models
|
||||
]
|
||||
flat_model_fields = flat_validation_model_fields + flat_serialization_model_fields
|
||||
input_types = {f.type_ for f in fields}
|
||||
unique_flat_model_fields = {
|
||||
f for f in flat_model_fields if f.type_ not in input_types
|
||||
|
||||
@@ -125,60 +125,16 @@ def ensure_multipart_is_installed() -> None:
|
||||
raise RuntimeError(multipart_not_installed_error) from None
|
||||
|
||||
|
||||
def get_param_sub_dependant(
|
||||
*,
|
||||
param_name: str,
|
||||
depends: params.Depends,
|
||||
path: str,
|
||||
security_scopes: Optional[List[str]] = None,
|
||||
) -> Dependant:
|
||||
assert depends.dependency
|
||||
return get_sub_dependant(
|
||||
depends=depends,
|
||||
dependency=depends.dependency,
|
||||
path=path,
|
||||
name=param_name,
|
||||
security_scopes=security_scopes,
|
||||
)
|
||||
|
||||
|
||||
def get_parameterless_sub_dependant(*, depends: params.Depends, path: str) -> Dependant:
|
||||
assert callable(depends.dependency), (
|
||||
"A parameter-less dependency must have a callable dependency"
|
||||
)
|
||||
return get_sub_dependant(depends=depends, dependency=depends.dependency, path=path)
|
||||
|
||||
|
||||
def get_sub_dependant(
|
||||
*,
|
||||
depends: params.Depends,
|
||||
dependency: Callable[..., Any],
|
||||
path: str,
|
||||
name: Optional[str] = None,
|
||||
security_scopes: Optional[List[str]] = None,
|
||||
) -> Dependant:
|
||||
security_requirement = None
|
||||
security_scopes = security_scopes or []
|
||||
if isinstance(depends, params.Security):
|
||||
dependency_scopes = depends.scopes
|
||||
security_scopes.extend(dependency_scopes)
|
||||
if isinstance(dependency, SecurityBase):
|
||||
use_scopes: List[str] = []
|
||||
if isinstance(dependency, (OAuth2, OpenIdConnect)):
|
||||
use_scopes = security_scopes
|
||||
security_requirement = SecurityRequirement(
|
||||
security_scheme=dependency, scopes=use_scopes
|
||||
)
|
||||
sub_dependant = get_dependant(
|
||||
path=path,
|
||||
call=dependency,
|
||||
name=name,
|
||||
security_scopes=security_scopes,
|
||||
use_cache=depends.use_cache,
|
||||
use_security_scopes: List[str] = []
|
||||
if isinstance(depends, params.Security) and depends.scopes:
|
||||
use_security_scopes.extend(depends.scopes)
|
||||
return get_dependant(
|
||||
path=path, call=depends.dependency, security_scopes=use_security_scopes
|
||||
)
|
||||
if security_requirement:
|
||||
sub_dependant.security_requirements.append(security_requirement)
|
||||
return sub_dependant
|
||||
|
||||
|
||||
CacheKey = Tuple[Optional[Callable[..., Any]], Tuple[str, ...]]
|
||||
@@ -282,9 +238,6 @@ def get_dependant(
|
||||
security_scopes: Optional[List[str]] = None,
|
||||
use_cache: bool = True,
|
||||
) -> Dependant:
|
||||
path_param_names = get_path_param_names(path)
|
||||
endpoint_signature = get_typed_signature(call)
|
||||
signature_params = endpoint_signature.parameters
|
||||
dependant = Dependant(
|
||||
call=call,
|
||||
name=name,
|
||||
@@ -292,6 +245,17 @@ def get_dependant(
|
||||
security_scopes=security_scopes,
|
||||
use_cache=use_cache,
|
||||
)
|
||||
path_param_names = get_path_param_names(path)
|
||||
endpoint_signature = get_typed_signature(call)
|
||||
signature_params = endpoint_signature.parameters
|
||||
if isinstance(call, SecurityBase):
|
||||
use_scopes: List[str] = []
|
||||
if isinstance(call, (OAuth2, OpenIdConnect)):
|
||||
use_scopes = security_scopes
|
||||
security_requirement = SecurityRequirement(
|
||||
security_scheme=call, scopes=use_scopes
|
||||
)
|
||||
dependant.security_requirements.append(security_requirement)
|
||||
for param_name, param in signature_params.items():
|
||||
is_path_param = param_name in path_param_names
|
||||
param_details = analyze_param(
|
||||
@@ -301,11 +265,17 @@ def get_dependant(
|
||||
is_path_param=is_path_param,
|
||||
)
|
||||
if param_details.depends is not None:
|
||||
sub_dependant = get_param_sub_dependant(
|
||||
param_name=param_name,
|
||||
depends=param_details.depends,
|
||||
assert param_details.depends.dependency
|
||||
use_security_scopes = security_scopes or []
|
||||
if isinstance(param_details.depends, params.Security):
|
||||
if param_details.depends.scopes:
|
||||
use_security_scopes.extend(param_details.depends.scopes)
|
||||
sub_dependant = get_dependant(
|
||||
path=path,
|
||||
security_scopes=security_scopes,
|
||||
call=param_details.depends.dependency,
|
||||
name=param_name,
|
||||
security_scopes=use_security_scopes,
|
||||
use_cache=param_details.depends.use_cache,
|
||||
)
|
||||
dependant.dependencies.append(sub_dependant)
|
||||
continue
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import warnings
|
||||
from dataclasses import dataclass
|
||||
from enum import Enum
|
||||
from typing import Any, Callable, Dict, List, Optional, Sequence, Union
|
||||
|
||||
@@ -761,26 +762,12 @@ class File(Form): # type: ignore[misc]
|
||||
)
|
||||
|
||||
|
||||
@dataclass
|
||||
class Depends:
|
||||
def __init__(
|
||||
self, dependency: Optional[Callable[..., Any]] = None, *, use_cache: bool = True
|
||||
):
|
||||
self.dependency = dependency
|
||||
self.use_cache = use_cache
|
||||
|
||||
def __repr__(self) -> str:
|
||||
attr = getattr(self.dependency, "__name__", type(self.dependency).__name__)
|
||||
cache = "" if self.use_cache else ", use_cache=False"
|
||||
return f"{self.__class__.__name__}({attr}{cache})"
|
||||
dependency: Optional[Callable[..., Any]] = None
|
||||
use_cache: bool = True
|
||||
|
||||
|
||||
@dataclass
|
||||
class Security(Depends):
|
||||
def __init__(
|
||||
self,
|
||||
dependency: Optional[Callable[..., Any]] = None,
|
||||
*,
|
||||
scopes: Optional[Sequence[str]] = None,
|
||||
use_cache: bool = True,
|
||||
):
|
||||
super().__init__(dependency=dependency, use_cache=use_cache)
|
||||
self.scopes = scopes or []
|
||||
scopes: Optional[Sequence[str]] = None
|
||||
|
||||
@@ -7,6 +7,8 @@ name = "fastapi"
|
||||
dynamic = ["version"]
|
||||
description = "FastAPI framework, high performance, easy to learn, fast to code, ready for production"
|
||||
readme = "README.md"
|
||||
license = "MIT"
|
||||
license-files = ["LICENSE"]
|
||||
requires-python = ">=3.8"
|
||||
authors = [
|
||||
{ name = "Sebastián Ramírez", email = "tiangolo@gmail.com" },
|
||||
@@ -31,7 +33,6 @@ classifiers = [
|
||||
"Framework :: Pydantic :: 1",
|
||||
"Framework :: Pydantic :: 2",
|
||||
"Intended Audience :: Developers",
|
||||
"License :: OSI Approved :: MIT License",
|
||||
"Programming Language :: Python :: 3 :: Only",
|
||||
"Programming Language :: Python :: 3.8",
|
||||
"Programming Language :: Python :: 3.9",
|
||||
@@ -44,7 +45,7 @@ classifiers = [
|
||||
"Topic :: Internet :: WWW/HTTP",
|
||||
]
|
||||
dependencies = [
|
||||
"starlette>=0.40.0,<0.49.0",
|
||||
"starlette>=0.40.0,<0.50.0",
|
||||
"pydantic>=1.7.4,!=1.8,!=1.8.1,!=2.0.0,!=2.0.1,!=2.1.0,<3.0.0",
|
||||
"typing-extensions>=4.8.0",
|
||||
"annotated-doc>=0.0.2",
|
||||
|
||||
78
tests/test_dependency_paramless.py
Normal file
78
tests/test_dependency_paramless.py
Normal file
@@ -0,0 +1,78 @@
|
||||
from typing import Union
|
||||
|
||||
from fastapi import FastAPI, HTTPException, Security
|
||||
from fastapi.security import (
|
||||
OAuth2PasswordBearer,
|
||||
SecurityScopes,
|
||||
)
|
||||
from fastapi.testclient import TestClient
|
||||
from typing_extensions import Annotated
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")
|
||||
|
||||
|
||||
def process_auth(
|
||||
credentials: Annotated[Union[str, None], Security(oauth2_scheme)],
|
||||
security_scopes: SecurityScopes,
|
||||
):
|
||||
# This is an incorrect way of using it, this is not checking if the scopes are
|
||||
# provided by the token, only if the endpoint is requesting them, but the test
|
||||
# here is just to check if FastAPI is indeed registering and passing the scopes
|
||||
# correctly when using Security with parameterless dependencies.
|
||||
if "a" not in security_scopes.scopes or "b" not in security_scopes.scopes:
|
||||
raise HTTPException(detail="a or b not in scopes", status_code=401)
|
||||
return {"token": credentials, "scopes": security_scopes.scopes}
|
||||
|
||||
|
||||
@app.get("/get-credentials")
|
||||
def get_credentials(
|
||||
credentials: Annotated[dict, Security(process_auth, scopes=["a", "b"])],
|
||||
):
|
||||
return credentials
|
||||
|
||||
|
||||
@app.get(
|
||||
"/parameterless-with-scopes",
|
||||
dependencies=[Security(process_auth, scopes=["a", "b"])],
|
||||
)
|
||||
def get_parameterless_with_scopes():
|
||||
return {"status": "ok"}
|
||||
|
||||
|
||||
@app.get(
|
||||
"/parameterless-without-scopes",
|
||||
dependencies=[Security(process_auth)],
|
||||
)
|
||||
def get_parameterless_without_scopes():
|
||||
return {"status": "ok"}
|
||||
|
||||
|
||||
client = TestClient(app)
|
||||
|
||||
|
||||
def test_get_credentials():
|
||||
response = client.get("/get-credentials", headers={"authorization": "Bearer token"})
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == {"token": "token", "scopes": ["a", "b"]}
|
||||
|
||||
|
||||
def test_parameterless_with_scopes():
|
||||
response = client.get(
|
||||
"/parameterless-with-scopes", headers={"authorization": "Bearer token"}
|
||||
)
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == {"status": "ok"}
|
||||
|
||||
|
||||
def test_parameterless_without_scopes():
|
||||
response = client.get(
|
||||
"/parameterless-without-scopes", headers={"authorization": "Bearer token"}
|
||||
)
|
||||
assert response.status_code == 401, response.text
|
||||
assert response.json() == {"detail": "a or b not in scopes"}
|
||||
|
||||
|
||||
def test_call_get_parameterless_without_scopes_for_coverage():
|
||||
assert get_parameterless_without_scopes() == {"status": "ok"}
|
||||
203
tests/test_no_schema_split.py
Normal file
203
tests/test_no_schema_split.py
Normal file
@@ -0,0 +1,203 @@
|
||||
# Test with parts from, and to verify the report in:
|
||||
# https://github.com/fastapi/fastapi/discussions/14177
|
||||
# Made an issue in:
|
||||
# https://github.com/fastapi/fastapi/issues/14247
|
||||
from enum import Enum
|
||||
from typing import List
|
||||
|
||||
from fastapi import FastAPI
|
||||
from fastapi.testclient import TestClient
|
||||
from inline_snapshot import snapshot
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
from tests.utils import pydantic_snapshot
|
||||
|
||||
|
||||
class MessageEventType(str, Enum):
|
||||
alpha = "alpha"
|
||||
beta = "beta"
|
||||
|
||||
|
||||
class MessageEvent(BaseModel):
|
||||
event_type: MessageEventType = Field(default=MessageEventType.alpha)
|
||||
output: str
|
||||
|
||||
|
||||
class MessageOutput(BaseModel):
|
||||
body: str = ""
|
||||
events: List[MessageEvent] = []
|
||||
|
||||
|
||||
class Message(BaseModel):
|
||||
input: str
|
||||
output: MessageOutput
|
||||
|
||||
|
||||
app = FastAPI(title="Minimal FastAPI App", version="1.0.0")
|
||||
|
||||
|
||||
@app.post("/messages", response_model=Message)
|
||||
async def create_message(input_message: str) -> Message:
|
||||
return Message(
|
||||
input=input_message,
|
||||
output=MessageOutput(body=f"Processed: {input_message}"),
|
||||
)
|
||||
|
||||
|
||||
client = TestClient(app)
|
||||
|
||||
|
||||
def test_create_message():
|
||||
response = client.post("/messages", params={"input_message": "Hello"})
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == {
|
||||
"input": "Hello",
|
||||
"output": {"body": "Processed: Hello", "events": []},
|
||||
}
|
||||
|
||||
|
||||
def test_openapi_schema():
|
||||
response = client.get("/openapi.json")
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == snapshot(
|
||||
{
|
||||
"openapi": "3.1.0",
|
||||
"info": {"title": "Minimal FastAPI App", "version": "1.0.0"},
|
||||
"paths": {
|
||||
"/messages": {
|
||||
"post": {
|
||||
"summary": "Create Message",
|
||||
"operationId": "create_message_messages_post",
|
||||
"parameters": [
|
||||
{
|
||||
"name": "input_message",
|
||||
"in": "query",
|
||||
"required": True,
|
||||
"schema": {"type": "string", "title": "Input Message"},
|
||||
}
|
||||
],
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "Successful Response",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/Message"
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
"422": {
|
||||
"description": "Validation Error",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/HTTPValidationError"
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
},
|
||||
"components": {
|
||||
"schemas": {
|
||||
"HTTPValidationError": {
|
||||
"properties": {
|
||||
"detail": {
|
||||
"items": {
|
||||
"$ref": "#/components/schemas/ValidationError"
|
||||
},
|
||||
"type": "array",
|
||||
"title": "Detail",
|
||||
}
|
||||
},
|
||||
"type": "object",
|
||||
"title": "HTTPValidationError",
|
||||
},
|
||||
"Message": {
|
||||
"properties": {
|
||||
"input": {"type": "string", "title": "Input"},
|
||||
"output": {"$ref": "#/components/schemas/MessageOutput"},
|
||||
},
|
||||
"type": "object",
|
||||
"required": ["input", "output"],
|
||||
"title": "Message",
|
||||
},
|
||||
"MessageEvent": {
|
||||
"properties": {
|
||||
"event_type": pydantic_snapshot(
|
||||
v2=snapshot(
|
||||
{
|
||||
"$ref": "#/components/schemas/MessageEventType",
|
||||
"default": "alpha",
|
||||
}
|
||||
),
|
||||
v1=snapshot(
|
||||
{
|
||||
"allOf": [
|
||||
{
|
||||
"$ref": "#/components/schemas/MessageEventType"
|
||||
}
|
||||
],
|
||||
"default": "alpha",
|
||||
}
|
||||
),
|
||||
),
|
||||
"output": {"type": "string", "title": "Output"},
|
||||
},
|
||||
"type": "object",
|
||||
"required": ["output"],
|
||||
"title": "MessageEvent",
|
||||
},
|
||||
"MessageEventType": pydantic_snapshot(
|
||||
v2=snapshot(
|
||||
{
|
||||
"type": "string",
|
||||
"enum": ["alpha", "beta"],
|
||||
"title": "MessageEventType",
|
||||
}
|
||||
),
|
||||
v1=snapshot(
|
||||
{
|
||||
"type": "string",
|
||||
"enum": ["alpha", "beta"],
|
||||
"title": "MessageEventType",
|
||||
"description": "An enumeration.",
|
||||
}
|
||||
),
|
||||
),
|
||||
"MessageOutput": {
|
||||
"properties": {
|
||||
"body": {"type": "string", "title": "Body", "default": ""},
|
||||
"events": {
|
||||
"items": {"$ref": "#/components/schemas/MessageEvent"},
|
||||
"type": "array",
|
||||
"title": "Events",
|
||||
"default": [],
|
||||
},
|
||||
},
|
||||
"type": "object",
|
||||
"title": "MessageOutput",
|
||||
},
|
||||
"ValidationError": {
|
||||
"properties": {
|
||||
"loc": {
|
||||
"items": {
|
||||
"anyOf": [{"type": "string"}, {"type": "integer"}]
|
||||
},
|
||||
"type": "array",
|
||||
"title": "Location",
|
||||
},
|
||||
"msg": {"type": "string", "title": "Message"},
|
||||
"type": {"type": "string", "title": "Error Type"},
|
||||
},
|
||||
"type": "object",
|
||||
"required": ["loc", "msg", "type"],
|
||||
"title": "ValidationError",
|
||||
},
|
||||
}
|
||||
},
|
||||
}
|
||||
)
|
||||
@@ -1,7 +1,7 @@
|
||||
from typing import Any, List
|
||||
|
||||
from dirty_equals import IsOneOf
|
||||
from fastapi.params import Body, Cookie, Depends, Header, Param, Path, Query
|
||||
from fastapi.params import Body, Cookie, Header, Param, Path, Query
|
||||
|
||||
test_data: List[Any] = ["teststr", None, ..., 1, []]
|
||||
|
||||
@@ -141,12 +141,3 @@ def test_body_repr_number():
|
||||
|
||||
def test_body_repr_list():
|
||||
assert repr(Body([])) == "Body([])"
|
||||
|
||||
|
||||
def test_depends_repr():
|
||||
assert repr(Depends()) == "Depends(NoneType)"
|
||||
assert repr(Depends(get_user)) == "Depends(get_user)"
|
||||
assert repr(Depends(use_cache=False)) == "Depends(NoneType, use_cache=False)"
|
||||
assert (
|
||||
repr(Depends(get_user, use_cache=False)) == "Depends(get_user, use_cache=False)"
|
||||
)
|
||||
|
||||
@@ -1028,17 +1028,6 @@ def test_openapi_schema():
|
||||
"type": "object",
|
||||
"title": "HTTPValidationError",
|
||||
},
|
||||
"SubItem-Output": {
|
||||
"properties": {
|
||||
"new_sub_name": {
|
||||
"type": "string",
|
||||
"title": "New Sub Name",
|
||||
}
|
||||
},
|
||||
"type": "object",
|
||||
"required": ["new_sub_name"],
|
||||
"title": "SubItem",
|
||||
},
|
||||
"ValidationError": {
|
||||
"properties": {
|
||||
"loc": {
|
||||
@@ -1113,11 +1102,11 @@ def test_openapi_schema():
|
||||
"title": "New Description",
|
||||
},
|
||||
"new_sub": {
|
||||
"$ref": "#/components/schemas/SubItem-Output"
|
||||
"$ref": "#/components/schemas/tests__test_pydantic_v1_v2_multifile__modelsv2__SubItem"
|
||||
},
|
||||
"new_multi": {
|
||||
"items": {
|
||||
"$ref": "#/components/schemas/SubItem-Output"
|
||||
"$ref": "#/components/schemas/tests__test_pydantic_v1_v2_multifile__modelsv2__SubItem"
|
||||
},
|
||||
"type": "array",
|
||||
"title": "New Multi",
|
||||
|
||||
60
tests/test_top_level_security_scheme_in_openapi.py
Normal file
60
tests/test_top_level_security_scheme_in_openapi.py
Normal file
@@ -0,0 +1,60 @@
|
||||
# Test security scheme at the top level, including OpenAPI
|
||||
# Ref: https://github.com/fastapi/fastapi/discussions/14263
|
||||
# Ref: https://github.com/fastapi/fastapi/issues/14271
|
||||
from fastapi import Depends, FastAPI
|
||||
from fastapi.security import HTTPBearer
|
||||
from fastapi.testclient import TestClient
|
||||
from inline_snapshot import snapshot
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
bearer_scheme = HTTPBearer()
|
||||
|
||||
|
||||
@app.get("/", dependencies=[Depends(bearer_scheme)])
|
||||
async def get_root():
|
||||
return {"message": "Hello, World!"}
|
||||
|
||||
|
||||
client = TestClient(app)
|
||||
|
||||
|
||||
def test_get_root():
|
||||
response = client.get("/", headers={"Authorization": "Bearer token"})
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == {"message": "Hello, World!"}
|
||||
|
||||
|
||||
def test_get_root_no_token():
|
||||
response = client.get("/")
|
||||
assert response.status_code == 403, response.text
|
||||
assert response.json() == {"detail": "Not authenticated"}
|
||||
|
||||
|
||||
def test_openapi_schema():
|
||||
response = client.get("/openapi.json")
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == snapshot(
|
||||
{
|
||||
"openapi": "3.1.0",
|
||||
"info": {"title": "FastAPI", "version": "0.1.0"},
|
||||
"paths": {
|
||||
"/": {
|
||||
"get": {
|
||||
"summary": "Get Root",
|
||||
"operationId": "get_root__get",
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "Successful Response",
|
||||
"content": {"application/json": {"schema": {}}},
|
||||
}
|
||||
},
|
||||
"security": [{"HTTPBearer": []}],
|
||||
}
|
||||
}
|
||||
},
|
||||
"components": {
|
||||
"securitySchemes": {"HTTPBearer": {"type": "http", "scheme": "bearer"}}
|
||||
},
|
||||
}
|
||||
)
|
||||
Reference in New Issue
Block a user