Skip to content

Commit

Permalink
Fix for JSON array parsing in multipart/form-data (#1869) (#1872)
Browse files Browse the repository at this point in the history
Fixes #1869 .



Changes proposed in this pull request:

- Prioritise parsing JSON over splitting array

---------

Co-authored-by: Alex Fechner <alex.fechner@stryker.com>
  • Loading branch information
alfechner and Alex Fechner authored Feb 13, 2024
1 parent 5d7eb0b commit 5de6dcc
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 3 deletions.
6 changes: 3 additions & 3 deletions connexion/uri_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,10 @@ def resolve_form(self, form_data):
form_data[k] = self._resolve_param_duplicates(
form_data[k], encoding, "form"
)
if defn and defn["type"] == "array":
form_data[k] = self._split(form_data[k], encoding, "form")
elif "contentType" in encoding and all_json([encoding.get("contentType")]):
if "contentType" in encoding and all_json([encoding.get("contentType")]):
form_data[k] = json.loads(form_data[k])
elif defn and defn["type"] == "array":
form_data[k] = self._split(form_data[k], encoding, "form")
form_data[k] = coerce_type(defn, form_data[k], "requestBody", k)
return form_data

Expand Down
9 changes: 9 additions & 0 deletions tests/fakeapi/hello/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,15 @@ def post_multipart_form(body):
return x


def post_multipart_form_array(body):
result = []
for x in body["x"]:
x["name"] += "-reply"
x["age"] += 10
result.append(x)
return result


def apikey_info(apikey, required_scopes=None):
if apikey == "mykey":
return {"sub": "admin"}
Expand Down
28 changes: 28 additions & 0 deletions tests/fixtures/json_validation/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,31 @@ paths:
application/json:
schema:
$ref: "#/components/schemas/X"


/multipart_form_json_array:
post:
operationId: fakeapi.hello.post_multipart_form_array
requestBody:
required: true
content:
multipart/form-data:
schema:
type: object
properties:
x:
type: array
items:
$ref: "#/components/schemas/X"
encoding:
x:
contentType: "application/json"
responses:
200:
description: Modified Echo
content:
application/json:
schema:
type: array
items:
$ref: "#/components/schemas/X"
24 changes: 24 additions & 0 deletions tests/test_json_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,27 @@ def test_multipart_form_json(json_validation_spec_dir, spec, app_class):
assert res.status_code == 200
assert res.json()["name"] == "joe-reply"
assert res.json()["age"] == 30


@pytest.mark.parametrize("spec", ["openapi.yaml"])
def test_multipart_form_json_array(json_validation_spec_dir, spec, app_class):
app = build_app_from_fixture(
json_validation_spec_dir,
app_class=app_class,
spec_file=spec,
validate_responses=True,
)
app_client = app.test_client()

res = app_client.post(
"/v1.0/multipart_form_json_array",
files={"file": b""}, # Force multipart/form-data content-type
data={
"x": json.dumps([{"name": "joe", "age": 20}, {"name": "alena", "age": 28}])
},
)
assert res.status_code == 200
assert res.json()[0]["name"] == "joe-reply"
assert res.json()[0]["age"] == 30
assert res.json()[1]["name"] == "alena-reply"
assert res.json()[1]["age"] == 38

0 comments on commit 5de6dcc

Please sign in to comment.