Skip to content

Commit

Permalink
✨ Non-blocking errors on schema generation
Browse files Browse the repository at this point in the history
  • Loading branch information
perdy authored and migduroli committed Sep 3, 2024
1 parent 05f9bac commit bd4f6c1
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
23 changes: 15 additions & 8 deletions flama/schemas/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import yaml

from flama import routing, schemas, types
from flama.schemas import Schema, openapi
from flama.schemas import Schema, exceptions, openapi
from flama.schemas.data_structures import Parameter
from flama.url import RegexPath

Expand All @@ -21,11 +21,11 @@
class EndpointInfo:
path: str
method: str
func: t.Callable
query_parameters: t.Dict[str, Parameter]
path_parameters: t.Dict[str, Parameter]
body_parameter: t.Optional[Parameter]
response_parameter: Parameter
func: t.Callable = dataclasses.field(repr=False)
query_parameters: t.Dict[str, Parameter] = dataclasses.field(repr=False)
path_parameters: t.Dict[str, Parameter] = dataclasses.field(repr=False)
body_parameter: t.Optional[Parameter] = dataclasses.field(repr=False)
response_parameter: Parameter = dataclasses.field(repr=False)


@dataclasses.dataclass(frozen=True)
Expand Down Expand Up @@ -495,8 +495,15 @@ def get_api_schema(self, routes: t.List[routing.BaseRoute]) -> t.Dict[str, t.Any
endpoints_info = self.get_endpoints(routes)

for path, endpoints in endpoints_info.items():
operations = {e.method: self.get_operation_schema(e) for e in endpoints}
self.spec.add_path(path, openapi.Path(**operations)) # type: ignore[arg-type]
operations = {}
for endpoint in endpoints:
try:
operations[endpoint.method] = self.get_operation_schema(endpoint)
except Exception:
logger.error("Cannot generate schema for endpoint %s", endpoint)

if operations:
self.spec.add_path(path, openapi.Path(**operations)) # type: ignore[arg-type]

for schema in self.schemas.used(self.spec).values():
self.spec.add_schema(schema.name, openapi.Schema(schema.json_schema(self.schemas.names)))
Expand Down
13 changes: 13 additions & 0 deletions tests/schemas/test_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -1022,6 +1022,13 @@ async def custom_body():
"""
return {}

@app.route("/wrong-schema/", methods=["POST"])
async def wrong():
"""
Wrong schema.
"""
return {}

router = Router()
router.add_route("/custom-component/", endpoint=get, methods=["GET"])
app.mount("/mount", router)
Expand Down Expand Up @@ -1330,6 +1337,12 @@ def test_components_schemas(self, app, schemas):
},
id="custom_body",
),
pytest.param(
"/wrong-schema/",
"post",
{},
id="wrong_schema",
),
],
)
def test_schema(self, app, schemas, path, verb, expected_schema):
Expand Down

0 comments on commit bd4f6c1

Please sign in to comment.