Skip to content

Commit

Permalink
add "null" to the error code enum of non_field_errors validation …
Browse files Browse the repository at this point in the history
…errors since the error code is returned when submitting a post/patch/put with `null` as the request body (see #74 for details)
  • Loading branch information
ghazi-git committed Jun 18, 2024
1 parent ce6ef4e commit cccda0f
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 4 deletions.
1 change: 1 addition & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
- enforce support of only drf-spectacular 0.27 and newer in pyproject.toml
- ensure examples from `@extend_schema_serializer` are not ignored when adding error response examples
- show default error response examples only when the corresponding status code is allowed
- add `"null"` to the error code enum of `non_field_errors` validation errors

## [0.13.0] - 2024-02-28
### Changed
Expand Down
2 changes: 1 addition & 1 deletion drf_standardized_errors/openapi_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ def get_serializer_field_error_codes(field: serializers.Field, attr: str) -> Set
# for top-level (as opposed to nested) serializer non_field_errors,
# "required" and "null" errors are not raised
if attr == drf_settings.NON_FIELD_ERRORS_KEY:
error_codes = set(error_codes).difference(["required", "null"])
error_codes = set(error_codes).difference(["required"])

# for ManyRelatedFields, add the error codes from the child_relation
# to the parent error codes. That's because DRF raises child_relation
Expand Down
9 changes: 7 additions & 2 deletions tests/test_openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,12 +216,17 @@ def test_error_codes_for_polymorphic_serializer():
create_error_codes = schema["components"]["schemas"][
"ValidateCreateNonFieldErrorsErrorComponent"
]["properties"]["code"]["enum"]
assert set(create_error_codes) == {"invalid", "object1_code", "object2_code"}
assert set(create_error_codes) == {
"invalid",
"null",
"object1_code",
"object2_code",
}

patch_error_codes = schema["components"]["schemas"][
"ValidatePartialUpdateNonFieldErrorsErrorComponent"
]["properties"]["code"]["enum"]
assert set(patch_error_codes) == {"invalid", "object1_code", "object2_code"}
assert set(patch_error_codes) == {"invalid", "null", "object1_code", "object2_code"}


class CustomFilterSet(FilterSet):
Expand Down
2 changes: 1 addition & 1 deletion tests/test_openapi_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ def serializer():
def test_top_level_non_field_errors_error_codes(serializer):
"""required and null should NOT be listed as error codes"""
(field,) = get_serializer_fields_with_error_codes([serializer])
assert field.error_codes == {"invalid"}
assert field.error_codes == {"invalid", "null"}


@pytest.fixture
Expand Down

0 comments on commit cccda0f

Please sign in to comment.