Maillol / aiohttp-pydantic

Aiohttp View that validates request body and query sting regarding the annotations declared in the View method
MIT License
67 stars 21 forks source link

Prevent internal server error when receiving a JSON request body with non-object top-level structure #9

Closed drderuiter closed 3 years ago

drderuiter commented 3 years ago

When the BodyGetter class has to deal with a JSON that does not have an object as the top-level structure, it crashes. To replicate this behavior, simply send a POST request with request body ["foo"] in the demo, or run the test scenario added in this PR without the changes to the BodyGetter.

Seeing as the request body is always parsed to a Pydantic model, and never to a nested structure like List[<MyPydanticModel>], this change does not alter any behavior except for returning a 400 whenever a 500 would have been returned otherwise.

WDYT?

Awesome package, by the way!

codecov-io commented 3 years ago

Codecov Report

Merging #9 (13041af) into main (c6b979d) will increase coverage by 0.03%. The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main       #9      +/-   ##
==========================================
+ Coverage   92.64%   92.67%   +0.03%     
==========================================
  Files          11       11              
  Lines         666      669       +3     
==========================================
+ Hits          617      620       +3     
  Misses         49       49              
Impacted Files Coverage Δ
aiohttp_pydantic/__init__.py 100.00% <100.00%> (ø)
aiohttp_pydantic/injectors.py 94.80% <100.00%> (+0.21%) :arrow_up:

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update c6b979d...13041af. Read the comment docs.

Maillol commented 3 years ago

Thank you for your contribution.

I would like that aiohttp-pydantic can accept a JSON that has a list as the top-level structure.

We can define a list as model using pydantic:

class Pets(BaseModel):
    __root__: List[str]

https://pydantic-docs.helpmanual.io/usage/models/

Maillol commented 3 years ago

Thank you!