Closed LonelyVikingMichael closed 1 year ago
It seems that Litestar changed the way that multipart data is parsed.
from litestar._multipart import parse_multipart_form
from starlite.multipart import parse_multipart_form as starlite_parse_multipart_form
boundary = b"501688d00016004543513fb9e9784080"
body = (
b'--501688d00016004543513fb9e9784080\r\nContent-Disposition: form-data; name="code"\r\n\r\n'
b'123\r\n--501688d00016004543513fb9e9784080\r\nContent-Disposition: form-data; name="line_items"'
b'\r\n\r\n[{"cost": 5, "description": "Not free food"}]\r\n--501688d00016004543513fb9e9784080\r\n'
b'Content-Disposition: form-data; name="file"; filename="name.jpg"\r\nContent-Type: image/jpeg\r\n\r\n'
b"todayisgood\r\n--501688d00016004543513fb9e9784080--\r\n"
)
litestar_result = parse_multipart_form(body=body, boundary=boundary)
starlite_result = starlite_parse_multipart_form(body=body, boundary=boundary)
print("starlite result: ", starlite_result)
print("litestar result: ", litestar_result)
assert litestar_result == starlite_result
Results:
# starlite result:
{'code': 123, 'line_items': [{'cost': 5, 'description': 'Not free food'}], 'file': name.jpg - image/jpeg}
# litestar result:
{'code': '123', 'line_items': '[{"cost": 5, "description": "Not free food"}]', 'file': name.jpg - image/jpeg}
This was intentionally changed in the PR you linked, @LonelyVikingMichael. You can see a more detailed discussion regarding this here.
This was intentionally changed in the PR you linked, @LonelyVikingMichael. You can see a more detailed discussion regarding this here.
Ah, thanks for the context. I am the person who was relying on the bug as a feature then
@LonelyVikingMichael Have you tried explicitly declaring this as nested JSON in your Pydantic model with the Json
type?
@LonelyVikingMichael Have you tried explicitly declaring this as nested JSON in your Pydantic model with the
Json
type?
I've never used that type, you might be on to something. I've discussed internally with my work team and we've decided to take a different approach with mixed data types on HTTP POST.
I'll give this a shot if I ever get around to porting some of our other Starlite applications. Thank you all, happy to close this issue
Thanks @LonelyVikingMichael!
I'll close this for now as the recommended approach for mixed (i.e. "nested" data) with multipart is explicitly declaring it.
Description
Preface: I am trying to replicate functionality that was working in Starlite 1.51.x
It was previously possible to define nested models including
UploadFile
fields for consumption in a single request. Litestar raises a validation error on request.URL to code causing the issue
No response
MCVE
Steps to reproduce
Screenshots
No response
Logs
Litestar Version
2.3.2
Platform