Skip to content

Commit

Permalink
fix req + args parse in starlette (#203)
Browse files Browse the repository at this point in the history
* fix req + args in starlette

* test new starlette route register method

* release 0.7.4
  • Loading branch information
kemingy authored Feb 17, 2022
1 parent 8aea58c commit 3c191c5
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 21 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

setup(
name="spectree",
version="0.7.3",
version="0.7.4",
license="Apache-2.0",
author="Keming Yang",
author_email="[email protected]",
Expand Down
10 changes: 6 additions & 4 deletions spectree/plugins/starlette_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,14 @@ async def validate(
*args,
**kwargs,
):
from starlette.requests import Request
from starlette.responses import JSONResponse

# NOTE: If func is a `HTTPEndpoint`, it should have '.' in its ``__qualname__``
# This is not elegant. But it seems `inspect` doesn't work here.
instance = args[0] if "." in func.__qualname__ else None
request = args[1] if "." in func.__qualname__ else args[0]
if isinstance(args[0], Request):
instance, request = None, args[0]
else:
instance, request = args[:2]

response = None
req_validation_error = resp_validation_error = json_decode_error = None

Expand Down
32 changes: 16 additions & 16 deletions tests/test_plugin_starlette.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,17 +74,6 @@ async def user_score_annotated(request, query: Query, json: JSON, cookies: Cooki
return JSONResponse({"name": json.name, "score": score})


@api.validate(
query=Query,
path_parameter_descriptions={
"name": "The name that uniquely identifies the user.",
"non-existent-param": "description",
},
)
def user_address(request):
return None


app = Starlette(
routes=[
Route("/ping", Ping),
Expand All @@ -95,11 +84,6 @@ def user_address(request):
"/user",
routes=[
Route("/{name}", user_score, methods=["POST"]),
Route(
"/{name}/address/{address_id}",
user_address,
methods=["GET"],
),
],
),
Mount(
Expand All @@ -113,6 +97,22 @@ def user_address(request):
Mount("/static", app=StaticFiles(directory="docs"), name="static"),
]
)


def inner_register_func():
@app.route("/api/user/{name}/address/{address_id}")
@api.validate(
query=Query,
path_parameter_descriptions={
"name": "The name that uniquely identifies the user.",
"non-existent-param": "description",
},
)
def user_address(request):
return None


inner_register_func()
api.register(app)


Expand Down

0 comments on commit 3c191c5

Please sign in to comment.