mirror of
https://github.com/LukeHagar/connexion.git
synced 2025-12-06 04:19:26 +00:00
Fixes #942 No longer return 400 if a read-only property is provided, as discussed in the issue. We still raise an error if write-only properties are included in the response and response validation is enabled. I also changed how read-/write-only works in combination with `required`. Previously, `required` would not be overwritten by read-/write-only. Now we just follow the spec to the letter: - required and read-only: must be included but must be ignored by the server - required and write-only: impossible to achieve, but I also don't see how this combination could make sense - read-only: may be included but must be ignored by server - write-only: must not be included by server
131 lines
3.7 KiB
Python
131 lines
3.7 KiB
Python
import json
|
|
import pathlib
|
|
|
|
import pytest
|
|
from connexion import App
|
|
from connexion.json_schema import Draft4RequestValidator
|
|
from connexion.spec import Specification
|
|
from connexion.validators import JSONRequestBodyValidator
|
|
from jsonschema.validators import _utils, extend
|
|
|
|
from conftest import build_app_from_fixture
|
|
|
|
|
|
def test_validator_map(json_validation_spec_dir, spec):
|
|
def validate_type(validator, types, instance, schema):
|
|
types = _utils.ensure_list(types)
|
|
errors = Draft4RequestValidator.VALIDATORS["type"](
|
|
validator, types, instance, schema
|
|
)
|
|
yield from errors
|
|
|
|
if "string" in types and "minLength" not in schema:
|
|
errors = Draft4RequestValidator.VALIDATORS["minLength"](
|
|
validator, 1, instance, schema
|
|
)
|
|
yield from errors
|
|
|
|
MinLengthRequestValidator = extend(Draft4RequestValidator, {"type": validate_type})
|
|
|
|
class MyJSONBodyValidator(JSONRequestBodyValidator):
|
|
@property
|
|
def _validator(self):
|
|
return MinLengthRequestValidator(self._schema)
|
|
|
|
validator_map = {"body": {"application/json": MyJSONBodyValidator}}
|
|
|
|
app = App(__name__, specification_dir=json_validation_spec_dir)
|
|
app.add_api(spec, validate_responses=True, validator_map=validator_map)
|
|
app_client = app.test_client()
|
|
|
|
res = app_client.post(
|
|
"/v1.0/minlength",
|
|
json={"foo": "bar"},
|
|
)
|
|
assert res.status_code == 200
|
|
|
|
res = app_client.post(
|
|
"/v1.0/minlength",
|
|
json={"foo": ""},
|
|
)
|
|
assert res.status_code == 400
|
|
|
|
|
|
def test_readonly(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.get("/v1.0/user")
|
|
assert res.status_code == 200
|
|
assert res.json().get("user_id") == 7
|
|
|
|
res = app_client.post(
|
|
"/v1.0/user",
|
|
json={"name": "max", "password": "1234"},
|
|
)
|
|
assert res.status_code == 200
|
|
assert res.json().get("user_id") == 8
|
|
|
|
res = app_client.post(
|
|
"/v1.0/user",
|
|
json={"user_id": 9, "name": "max"},
|
|
)
|
|
assert res.status_code == 200
|
|
|
|
|
|
def test_writeonly(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/user",
|
|
json={"name": "max", "password": "1234"},
|
|
)
|
|
assert res.status_code == 200
|
|
assert "password" not in res.json()
|
|
|
|
res = app_client.get("/v1.0/user")
|
|
assert res.status_code == 200
|
|
assert "password" not in res.json()
|
|
|
|
res = app_client.get("/v1.0/user_with_password")
|
|
assert res.status_code == 500
|
|
assert res.json()["detail"].startswith(
|
|
"Response body does not conform to specification"
|
|
)
|
|
|
|
|
|
def test_nullable_default(json_validation_spec_dir, spec):
|
|
spec_path = pathlib.Path(json_validation_spec_dir) / spec
|
|
Specification.load(spec_path)
|
|
|
|
|
|
@pytest.mark.parametrize("spec", ["openapi.yaml"])
|
|
def test_multipart_form_json(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",
|
|
files={"file": b""}, # Force multipart/form-data content-type
|
|
data={"x": json.dumps({"name": "joe", "age": 20})},
|
|
)
|
|
assert res.status_code == 200
|
|
assert res.json()["name"] == "joe-reply"
|
|
assert res.json()["age"] == 30
|