mirror of
https://github.com/rjNemo/fastapi
synced 2026-06-10 20:46:51 +00:00
🐛 Fix path in path parameters (#272)
This commit is contained in:
parent
58f135ba2f
commit
703ade7967
4 changed files with 102 additions and 2 deletions
8
docs/src/path_params/tutorial004.py
Normal file
8
docs/src/path_params/tutorial004.py
Normal file
|
|
@ -0,0 +1,8 @@
|
||||||
|
from fastapi import FastAPI
|
||||||
|
|
||||||
|
app = FastAPI()
|
||||||
|
|
||||||
|
|
||||||
|
@app.get("/files/{file_path:path}")
|
||||||
|
async def read_user_me(file_path: str):
|
||||||
|
return {"file_path": file_path}
|
||||||
|
|
@ -255,10 +255,11 @@ class APIRoute(routing.Route):
|
||||||
assert inspect.isfunction(endpoint) or inspect.ismethod(
|
assert inspect.isfunction(endpoint) or inspect.ismethod(
|
||||||
endpoint
|
endpoint
|
||||||
), f"An endpoint must be a function or method"
|
), f"An endpoint must be a function or method"
|
||||||
self.dependant = get_dependant(path=path, call=self.endpoint)
|
self.dependant = get_dependant(path=self.path_format, call=self.endpoint)
|
||||||
for depends in self.dependencies[::-1]:
|
for depends in self.dependencies[::-1]:
|
||||||
self.dependant.dependencies.insert(
|
self.dependant.dependencies.insert(
|
||||||
0, get_parameterless_sub_dependant(depends=depends, path=path)
|
0,
|
||||||
|
get_parameterless_sub_dependant(depends=depends, path=self.path_format),
|
||||||
)
|
)
|
||||||
self.body_field = get_body_field(dependant=self.dependant, name=self.name)
|
self.body_field = get_body_field(dependant=self.dependant, name=self.name)
|
||||||
self.app = request_response(
|
self.app = request_response(
|
||||||
|
|
|
||||||
0
tests/test_tutorial/test_path_params/__init__.py
Normal file
0
tests/test_tutorial/test_path_params/__init__.py
Normal file
91
tests/test_tutorial/test_path_params/test_tutorial004.py
Normal file
91
tests/test_tutorial/test_path_params/test_tutorial004.py
Normal file
|
|
@ -0,0 +1,91 @@
|
||||||
|
from starlette.testclient import TestClient
|
||||||
|
|
||||||
|
from path_params.tutorial004 import app
|
||||||
|
|
||||||
|
client = TestClient(app)
|
||||||
|
|
||||||
|
openapi_schema = {
|
||||||
|
"openapi": "3.0.2",
|
||||||
|
"info": {"title": "Fast API", "version": "0.1.0"},
|
||||||
|
"paths": {
|
||||||
|
"/files/{file_path}": {
|
||||||
|
"get": {
|
||||||
|
"responses": {
|
||||||
|
"200": {
|
||||||
|
"description": "Successful Response",
|
||||||
|
"content": {"application/json": {"schema": {}}},
|
||||||
|
},
|
||||||
|
"422": {
|
||||||
|
"description": "Validation Error",
|
||||||
|
"content": {
|
||||||
|
"application/json": {
|
||||||
|
"schema": {
|
||||||
|
"$ref": "#/components/schemas/HTTPValidationError"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"summary": "Read User Me",
|
||||||
|
"operationId": "read_user_me_files__file_path__get",
|
||||||
|
"parameters": [
|
||||||
|
{
|
||||||
|
"required": True,
|
||||||
|
"schema": {"title": "File_Path", "type": "string"},
|
||||||
|
"name": "file_path",
|
||||||
|
"in": "path",
|
||||||
|
}
|
||||||
|
],
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"components": {
|
||||||
|
"schemas": {
|
||||||
|
"ValidationError": {
|
||||||
|
"title": "ValidationError",
|
||||||
|
"required": ["loc", "msg", "type"],
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"loc": {
|
||||||
|
"title": "Location",
|
||||||
|
"type": "array",
|
||||||
|
"items": {"type": "string"},
|
||||||
|
},
|
||||||
|
"msg": {"title": "Message", "type": "string"},
|
||||||
|
"type": {"title": "Error Type", "type": "string"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"HTTPValidationError": {
|
||||||
|
"title": "HTTPValidationError",
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"detail": {
|
||||||
|
"title": "Detail",
|
||||||
|
"type": "array",
|
||||||
|
"items": {"$ref": "#/components/schemas/ValidationError"},
|
||||||
|
}
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def test_openapi():
|
||||||
|
response = client.get("/openapi.json")
|
||||||
|
assert response.status_code == 200
|
||||||
|
assert response.json() == openapi_schema
|
||||||
|
|
||||||
|
|
||||||
|
def test_file_path():
|
||||||
|
response = client.get("/files/home/johndoe/myfile.txt")
|
||||||
|
print(response.content)
|
||||||
|
assert response.status_code == 200
|
||||||
|
assert response.json() == {"file_path": "home/johndoe/myfile.txt"}
|
||||||
|
|
||||||
|
|
||||||
|
def test_root_file_path():
|
||||||
|
response = client.get("/files//home/johndoe/myfile.txt")
|
||||||
|
print(response.content)
|
||||||
|
assert response.status_code == 200
|
||||||
|
assert response.json() == {"file_path": "/home/johndoe/myfile.txt"}
|
||||||
Loading…
Reference in a new issue