Skip to content

Commit

Permalink
fix: remove Starlette converters from paths before adding them to the…
Browse files Browse the repository at this point in the history
… OpenAPI spec
  • Loading branch information
adriangb committed Mar 28, 2022
1 parent b22d8b5 commit 10d1553
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 3 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "xpresso"
version = "0.34.0"
version = "0.34.1"
description = "A developer centric, performant Python web framework"
authors = ["Adrian Garcia Badaracco <[email protected]>"]
readme = "README.md"
Expand Down
100 changes: 99 additions & 1 deletion tests/test_routing/test_path.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from typing import Any, Dict

import pytest

from xpresso import App, Path, Request
from xpresso import App, FromPath, Path, Request
from xpresso.testclient import TestClient


Expand Down Expand Up @@ -43,3 +45,99 @@ async def endpoint() -> None:
client = TestClient(app)
resp = client.post("/")
assert resp.status_code == 405, resp.content


@pytest.mark.parametrize("path", ["foo", "https://foo.com/bar", "foo/bar/baz/"])
def test_catchall_path(path: str) -> None:
async def endpoint(catchall: FromPath[str]) -> str:
return catchall

app = App([Path("/{catchall:path}", get=endpoint)])
client = TestClient(app)

resp = client.get(f"/{path}")
assert resp.status_code == 200, resp.content
assert resp.json() == path


def test_path_parameter_converter_is_removed_from_openapi():
async def endpoint(catchall: FromPath[str]) -> str:
return catchall

app = App([Path("/api/{catchall:path}", get=endpoint)])
client = TestClient(app)

expected_openapi: Dict[str, Any] = {
"openapi": "3.0.3",
"info": {"title": "API", "version": "0.1.0"},
"paths": {
# We MUST remove the {:converter} part from here
# since that is not part of the OpenAPI spec
"/api/{catchall}": {
"get": {
"responses": {
"200": {
"description": "OK",
"content": {
"application/json": {"schema": {"type": "string"}}
},
},
"422": {
"description": "Validation Error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/HTTPValidationError"
}
}
},
},
},
"parameters": [
{
"required": True,
"style": "simple",
"explode": False,
"schema": {"title": "Catchall", "type": "string"},
"name": "catchall",
"in": "path",
}
],
}
}
},
"components": {
"schemas": {
"ValidationError": {
"title": "ValidationError",
"required": ["loc", "msg", "type"],
"type": "object",
"properties": {
"loc": {
"title": "Location",
"type": "array",
"items": {
"oneOf": [{"type": "string"}, {"type": "integer"}]
},
},
"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"},
}
},
},
}
},
}
resp = client.get("/openapi.json")
assert resp.status_code == 200, resp.content
assert resp.json() == expected_openapi
4 changes: 3 additions & 1 deletion xpresso/openapi/_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from pydantic.schema import field_schema, get_flat_models_from_fields
from pydantic.schema import get_model_name_map as get_model_name_map_pydantic
from starlette.responses import Response
from starlette.routing import compile_path

from xpresso._utils.routing import VisitedRoute
from xpresso._utils.typing import get_args, get_origin, get_type_hints
Expand Down Expand Up @@ -355,7 +356,8 @@ def get_paths_items(
tags=operation_tags,
response_specs=operation_responses,
)
paths[visited_route.path] = models.PathItem(
path = compile_path(visited_route.path)[1]
paths[path] = models.PathItem(
description=visited_route.route.description,
summary=visited_route.route.summary,
servers=list(visited_route.route.servers) or None,
Expand Down

0 comments on commit 10d1553

Please sign in to comment.