Skip to content

Commit

Permalink
Don't return 400 when read-only property is provided (#1655)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
RobbeSneyders committed Mar 2, 2023
1 parent b28cf09 commit 1cb5f83
Show file tree
Hide file tree
Showing 3 changed files with 1 addition and 36 deletions.
31 changes: 0 additions & 31 deletions connexion/json_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,34 +128,6 @@ def nullable_validation_fn(validator, to_validate, instance, schema):
return nullable_validation_fn


def validate_required(validator, required, instance, schema):
if not validator.is_type(instance, "object"):
return

for prop in required:
if prop not in instance:
properties = schema.get("properties")
if properties is not None:
subschema = properties.get(prop)
if subschema is not None:
if "readOnly" in validator.VALIDATORS and subschema.get("readOnly"):
continue
if "writeOnly" in validator.VALIDATORS and subschema.get(
"writeOnly"
):
continue
if (
"x-writeOnly" in validator.VALIDATORS
and subschema.get("x-writeOnly") is True
):
continue
yield ValidationError("%r is a required property" % prop)


def validate_readOnly(validator, ro, instance, schema):
yield ValidationError("Property is read-only")


def validate_writeOnly(validator, wo, instance, schema):
yield ValidationError("Property is write-only")

Expand All @@ -168,8 +140,6 @@ def validate_writeOnly(validator, wo, instance, schema):
{
"type": NullableTypeValidator,
"enum": NullableEnumValidator,
"required": validate_required,
"readOnly": validate_readOnly,
},
)

Expand All @@ -178,7 +148,6 @@ def validate_writeOnly(validator, wo, instance, schema):
{
"type": NullableTypeValidator,
"enum": NullableEnumValidator,
"required": validate_required,
"writeOnly": validate_writeOnly,
"x-writeOnly": validate_writeOnly,
},
Expand Down
2 changes: 0 additions & 2 deletions tests/fixtures/json_validation/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ components:
type: object
required:
- name
- user_id
- password
properties:
user_id:
type: integer
Expand Down
4 changes: 1 addition & 3 deletions tests/test_json_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,6 @@ def test_readonly(json_validation_spec_dir, spec, app_class):
)
app_client = app.test_client()

headers = {"content-type": "application/json"}

res = app_client.get("/v1.0/user")
assert res.status_code == 200
assert res.json().get("user_id") == 7
Expand All @@ -77,7 +75,7 @@ def test_readonly(json_validation_spec_dir, spec, app_class):
"/v1.0/user",
json={"user_id": 9, "name": "max"},
)
assert res.status_code == 400
assert res.status_code == 200


def test_writeonly(json_validation_spec_dir, spec, app_class):
Expand Down

0 comments on commit 1cb5f83

Please sign in to comment.