Skip to content

Commit

Permalink
Circumvent double validation by pydantic + FastAPI when generating AP…
Browse files Browse the repository at this point in the history
…I response

- Fix type hint: FastAPI suggests using
  • Loading branch information
ml-evs committed Jul 21, 2023
1 parent d7cab19 commit e62e108
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 8 deletions.
6 changes: 4 additions & 2 deletions optimade/server/routers/references.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import Any

from fastapi import APIRouter, Depends, Request

from optimade.models import (
Expand Down Expand Up @@ -30,7 +32,7 @@
)
def get_references(
request: Request, params: EntryListingQueryParams = Depends()
) -> ReferenceResponseMany:
) -> Any:
return get_entries(
collection=references_coll,
response=ReferenceResponseMany,
Expand All @@ -48,7 +50,7 @@ def get_references(
)
def get_single_reference(
request: Request, entry_id: str, params: SingleEntryQueryParams = Depends()
) -> ReferenceResponseOne:
) -> Any:
return get_single_entry(
collection=references_coll,
entry_id=entry_id,
Expand Down
6 changes: 4 additions & 2 deletions optimade/server/routers/structures.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import Any

from fastapi import APIRouter, Depends, Request

from optimade.models import (
Expand Down Expand Up @@ -30,7 +32,7 @@
)
def get_structures(
request: Request, params: EntryListingQueryParams = Depends()
) -> StructureResponseMany:
) -> Any:
return get_entries(
collection=structures_coll,
response=StructureResponseMany,
Expand All @@ -48,7 +50,7 @@ def get_structures(
)
def get_single_structure(
request: Request, entry_id: str, params: SingleEntryQueryParams = Depends()
) -> StructureResponseOne:
) -> Any:
return get_single_entry(
collection=structures_coll,
entry_id=entry_id,
Expand Down
8 changes: 4 additions & 4 deletions optimade/server/routers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ def get_entries(
response: Type[EntryResponseMany],
request: Request,
params: EntryListingQueryParams,
) -> EntryResponseMany:
) -> Dict:
"""Generalized /{entry} endpoint getter"""
from optimade.server.routers import ENTRY_COLLECTIONS

Expand Down Expand Up @@ -285,7 +285,7 @@ def get_entries(
if results is not None and (fields or include_fields):
results = handle_response_fields(results, fields, include_fields) # type: ignore[assignment]

return response(
return dict(
links=links,
data=results if results else [],
meta=meta_values(
Expand All @@ -307,7 +307,7 @@ def get_single_entry(
response: Type[EntryResponseOne],
request: Request,
params: SingleEntryQueryParams,
) -> EntryResponseOne:
) -> Dict:
from optimade.server.routers import ENTRY_COLLECTIONS

params.check_params(request.query_params)
Expand Down Expand Up @@ -338,7 +338,7 @@ def get_single_entry(
if results is not None and (fields or include_fields):
results = handle_response_fields(results, fields, include_fields)[0] # type: ignore[assignment]

return response(
return dict(
links=links,
data=results if results else None,
meta=meta_values(
Expand Down

0 comments on commit e62e108

Please sign in to comment.