Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for JSON array parsing in multipart/form-data (#1869) #1872

Merged
merged 2 commits into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading