Skip to content

Commit

Permalink
Path Parameters Validation (#38)
Browse files Browse the repository at this point in the history
bug/FixTests (#34)

Fix tests

Signed-off-by: Daniel Soares Martins <[email protected]>
  • Loading branch information
dan5e3s6ares authored Sep 17, 2024
1 parent d4dc43b commit 4c4cb99
Show file tree
Hide file tree
Showing 8 changed files with 15,921 additions and 118 deletions.
2 changes: 1 addition & 1 deletion a_real_settings.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"mock_api_swaggerUrl": "https://api.apis.guru/v2/specs/adyen.com/FundService/3/openapi.json",
"mock_api_from_json_file": true,
"update_on_start": true
}
94 changes: 54 additions & 40 deletions app/api.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
import logging
from contextlib import asynccontextmanager

from fastapi import FastAPI, Request
Expand All @@ -22,6 +23,8 @@
validation_exception_handler,
)

logger = logging.getLogger(__name__)


@asynccontextmanager
async def lifespan(app: FastAPI):
Expand All @@ -44,7 +47,9 @@ async def hello(request: Request):
async def catch_all(request: Request, path_name: str):

try:
from_function, path = await UrlHandler.find_matching_url(path_name)
from_function, path, path_params = await UrlHandler.find_matching_url(
path_name
)
except KeyError as e:
raise NotFoundError(
[{"msg": "Url Not Found", "loc": ["path", path_name]}]
Expand All @@ -53,62 +58,71 @@ async def catch_all(request: Request, path_name: str):
try:
full_path = from_function
from_function = from_function[request.method]
except KeyError as e:
raise MethodNotAllowed(
[{"msg": "Method not allowed", "loc": [request.method]}]
) from e

keys = list(from_function.keys())
if "queries_param" in keys:
await CheckParams.headers_query_params(
rules_dict=from_function["queries_param"],
params_dict=await CheckParams.transfrom_query_params(
request.url.query
),
position="query",
position="query-params",
)

if "PARAMETERS" in full_path:
await CheckParams.headers_query_params(
rules_dict=full_path["PARAMETERS"]["headers_param"],
params_dict=request.headers,
position="headers",
)
if "path_params" in keys:
await CheckParams.headers_query_params(
rules_dict=from_function["path_params"],
params_dict=path_params,
position="path-params",
)

if "PARAMETERS" in full_path:
await CheckParams.headers_query_params(
rules_dict=full_path["PARAMETERS"]["headers_param"],
params_dict=dict(request.headers),
position="headers",
)

if "headers_param" in keys:
await CheckParams.headers_query_params(
rules_dict=from_function["headers_param"],
params_dict=request.headers,
params_dict=dict(request.headers),
position="headers",
)

try:
payload = await request.json()
except json.decoder.JSONDecodeError:
payload = {}

try:
required = False
if from_function.get("payload", None):
required = from_function["payload"].get("required", None)
validate(
instance=payload,
schema=from_function["payload"]["schema"],
)

if required and payload == {}:
raise MethodNotAllowed(
[{"msg": "Payload required", "loc": [request.method]}]
)
except ValidationError as e:
errors = [
{
"detail": e.message,
"pointer": [e.json_path],
}
]
raise ValidationErrorException(errors) from e
except KeyError as e:
raise MethodNotAllowed(
[{"msg": "Payload not allowed", "loc": [request.method]}]
) from e
try:
payload = await request.json()
except json.decoder.JSONDecodeError:
payload = {}

try:
required = False
if from_function.get("payload", None):
required = from_function["payload"].get("required", None)
validate(
instance=payload,
schema=from_function["payload"]["schema"],
)

if required and payload == {}:
raise MethodNotAllowed(
[{"msg": "Payload required", "loc": [request.method]}]
)
except ValidationError as e:
errors = [
{
"detail": e.message,
"pointer": [e.json_path],
}
]
raise ValidationErrorException(errors) from e
except KeyError as e:
raise MethodNotAllowed(
[{"msg": "Method not allowed", "loc": [request.method]}]
[{"msg": "Payload not allowed", "loc": [request.method]}]
) from e

return await FunctionsToEndpoints.build_response(
Expand Down
Loading

0 comments on commit 4c4cb99

Please sign in to comment.