-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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
Web API error response for malformed json using Text.Json is misleading and is exposing internals. #40202
Comments
You can configure https://docs.microsoft.com/dotnet/api/microsoft.aspnetcore.mvc.jsonoptions.allowinputformatterexceptionmessages?view=aspnetcore-6.0#microsoft-aspnetcore-mvc-jsonoptions-allowinputformatterexceptionmessages to have the formatter return a generic error message instead. |
Thank you for this link. This is much better. I'm getting now the error:
Is there also a way to hide the variable name of the body parameter and its error?
It still states that "The createGroupDTO field is required."! This response makes no sense since it is not a field in the Json structure and just an internal name. |
@brunolins16 Can you investigate this issue? You may have some more context based on your recent experience fixing a similar issue. |
Thanks for contacting us. |
@KillerBoogie sorry for the long delay in responding. Probably you have already found a workaround for your problem :(. Anyway, quick looking into your example I don't see any
[Required(ErrorMessage = "Input required")]CreateGroupDTO createGroupDTO "createGroupDTO": [
"Input required"
], |
Hi @KillerBoogie. We have added the "Needs: Author Feedback" label to this issue, which indicates that we have an open question for you before we can take further action. This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time. |
This issue has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 4 days. It will be closed if no further activity occurs within 3 days of this comment. If it is closed, feel free to comment when you are able to provide the additional information and we will re-investigate. See our Issue Management Policies for more information. |
Is there an existing issue for this?
Describe the bug
When a malformed Json is sent to a route the default serializer Text.Json creates a misleadig error message and exposes internal information. For example a malformed Guid or DateTime produces an error like:
Additionally, the variable name of the DTO is exposed and considered a field:
Expected Behavior
The error message for a malformed value must state that type only:
"The JSON value could not be converted to "Guid" or
"The JSON value could not be converted to "DateTime"
The name of the body variable should not be exposed in the error message. Only JSON values that can not be converted should be listed in the message.
Steps To Reproduce
Define DTO:
Create method in GroupsController
Post with malformed Json body
1) Guid
response body:
2) DateTime
response body:
Exceptions (if any)
No response
.NET Version
6.0.101
Anything else?
No response
The text was updated successfully, but these errors were encountered: