Skip to content

Commit cccda0f

Browse files
committed
add "null" to the error code enum of non_field_errors validation errors since the error code is returned when submitting a post/patch/put with null as the request body (see #74 for details)
1 parent ce6ef4e commit cccda0f

File tree

4 files changed

+10
-4
lines changed

4 files changed

+10
-4
lines changed

docs/changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
1212
- enforce support of only drf-spectacular 0.27 and newer in pyproject.toml
1313
- ensure examples from `@extend_schema_serializer` are not ignored when adding error response examples
1414
- show default error response examples only when the corresponding status code is allowed
15+
- add `"null"` to the error code enum of `non_field_errors` validation errors
1516

1617
## [0.13.0] - 2024-02-28
1718
### Changed

drf_standardized_errors/openapi_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ def get_serializer_field_error_codes(field: serializers.Field, attr: str) -> Set
205205
# for top-level (as opposed to nested) serializer non_field_errors,
206206
# "required" and "null" errors are not raised
207207
if attr == drf_settings.NON_FIELD_ERRORS_KEY:
208-
error_codes = set(error_codes).difference(["required", "null"])
208+
error_codes = set(error_codes).difference(["required"])
209209

210210
# for ManyRelatedFields, add the error codes from the child_relation
211211
# to the parent error codes. That's because DRF raises child_relation

tests/test_openapi.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,12 +216,17 @@ def test_error_codes_for_polymorphic_serializer():
216216
create_error_codes = schema["components"]["schemas"][
217217
"ValidateCreateNonFieldErrorsErrorComponent"
218218
]["properties"]["code"]["enum"]
219-
assert set(create_error_codes) == {"invalid", "object1_code", "object2_code"}
219+
assert set(create_error_codes) == {
220+
"invalid",
221+
"null",
222+
"object1_code",
223+
"object2_code",
224+
}
220225

221226
patch_error_codes = schema["components"]["schemas"][
222227
"ValidatePartialUpdateNonFieldErrorsErrorComponent"
223228
]["properties"]["code"]["enum"]
224-
assert set(patch_error_codes) == {"invalid", "object1_code", "object2_code"}
229+
assert set(patch_error_codes) == {"invalid", "null", "object1_code", "object2_code"}
225230

226231

227232
class CustomFilterSet(FilterSet):

tests/test_openapi_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ def serializer():
298298
def test_top_level_non_field_errors_error_codes(serializer):
299299
"""required and null should NOT be listed as error codes"""
300300
(field,) = get_serializer_fields_with_error_codes([serializer])
301-
assert field.error_codes == {"invalid"}
301+
assert field.error_codes == {"invalid", "null"}
302302

303303

304304
@pytest.fixture

0 commit comments

Comments
 (0)