-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
get_error_detail: use error_list to get code(s) #5785
Conversation
None of the current test is failing. Where should I add a new one? |
I guess so, tho I'm not sure what the failing case is? |
The problem is with |
btw: using/calling |
@blueyed I merged #5787. Do you want to update the test here.
Sorry. (I always struggle to follow) Is there an extra behaviour you're looking for here?
This seems good to me: additional info captured; I can't see a BC issue. Was there anything you wanted to add? |
No, I've just mentioned that in this case it is ok.
No, it's ready - but should be squash-merged. |
This change throws an exception on Django 1.11 if you raise a Django ValidationError with a dict as a message.
Before this the dict was transformed into a list, removing the dict key. |
ed6c97f
to
c42df55
Compare
I've added some more tests and fixed the code for them. @alexgmin |
code=expected_code | ||
) | ||
] | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not what you would expect really, but tests the current behavior.
Django itself loses the code and key when wrapping dict_errors into a list with ValidationError it seems.
No exceptions anymore. However, this code def validate(self, attrs):
raise DjangoValidationError({'email': 'That email is already being used.'}) Before this pull request resulted in {
"non_field_errors": [
"That email is already being used."
]
} now it's {
"email": [
"That email is already being used."
]
} So this fixes another problem that I had when transforming DjangoValidationErrors to RestFrameworkValidation errors, which was solved with this: try:
...
except DjangoValidationError as e:
# Django Rest Framework transforms Django Validation errors to lists,
# removing the key indicating the field.
if hasattr(e, 'error_dict'):
raise ValidationError(e.message_dict)
else:
raise ValidationError(detail=as_serializer_error(e)) |
rest_framework/fields.py
Outdated
detail = ErrorDetail(error.message % (error.params or ()), | ||
code=error.code if error.code else code) | ||
else: | ||
detail = _get_error_detail_dict(error, code) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This code/line is not executed. Need to look into this, likely also wrapping it in a list, not sure if that's a valid use case though.
@blueyed Do the test cases in this pull request fail without the code changes? |
@tomchristie
|
2c81d66
to
b2aa754
Compare
b2aa754
to
d9ee7d6
Compare
Updated and rebased. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've removed myself from the reviewer list. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alright, finally took a look. One minor concern - I'm not sure if the tests as-is are a good candidate for parametrization. Keeping track of the various conditions (eg, in test_validationerror_code_with_dict
) makes the test logic a little harder to follow. I typically prefer the expected result to be explicit instead of partially computed.
The tests might be a little more terse/easier to write if they focused on the get_error_detail
instead of having to write/test a full serializer class. Possibly write several parameterized unit tests for get_error_detail
, then a couple of integration tests for the entire serializer validation/error handling workflow.
Either way, PR looks good as-is. Test changes are just a thought/suggestion.
@rpkilby I think that light/clear tests are important and will look into simplifying them, but |
Lets take this as-is now. It finishes the run of changes to the error structures, and it'll be good to have it. Any work on the tests later/separately is obviously appreciated. Thanks @blueyed! |
This is necessary to get code(s) for validation errors.
Via e.g.
password_validation.validate_password
andget_error_detail
used indjango-rest-framework/rest_framework/serializers.py
Line 471 in 75cf06e
TODO: