mirror of
https://github.com/LukeHagar/connexion.git
synced 2025-12-06 04:19:26 +00:00
Fixes #1869 . Changes proposed in this pull request: - Prioritise parsing JSON over splitting array --------- Co-authored-by: Alex Fechner <alex.fechner@stryker.com>
This commit is contained in:
@@ -160,10 +160,10 @@ class OpenAPIURIParser(AbstractURIParser):
|
||||
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
|
||||
|
||||
|
||||
@@ -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"}
|
||||
|
||||
28
tests/fixtures/json_validation/openapi.yaml
vendored
28
tests/fixtures/json_validation/openapi.yaml
vendored
@@ -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"
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user