NewtonsoftJsonInputFormatter uses incorrect ModelStateDictionary keys for members containing single quotes #39069
Labels
area-mvc
Includes: MVC, Actions and Controllers, Localization, CORS, most templates
bug
This issue describes a behavior which is not expected - a bug.
feature-mvc-formatting
Milestone
Given JSON content like
"[{\"It's a key\": 1234556}]"
which is deserialized asIDictionary<string, short>
(the value is too big for a short making this an error),NewtonsoftJsonInputFormatter
adds an entry to theModelStateDictionary
with the key"['It\\'s a key'].It's a key"
instead of['It\\'s a key']
as expected.This is caused by logic in
NewtonsoftJsonInputFormatter
'sErrorHandler
that appends theErrorContext.Member
to theErrorContext.Path
when thePath
doesn't already end withMember
in order to better report missing required properties.aspnetcore/src/Mvc/Mvc.NewtonsoftJson/src/NewtonsoftJsonInputFormatter.cs
Lines 238 to 263 in 28ecab6
ErrorContext.Path
escapes the'
, butErrorContext.Member
doesn't meaning the!path.EndsWith()
checks don't cover this scenario. We could scanErrorContext.Member
for any'
characters and manually escape it before doing the!path.EndsWith()
checks, but that feels like playing whac-a-mole. I'm guessing there are even more edge cases the!path.EndsWith()
checks don't cover.I really want to get rid of the
addMember
logic altogether and have Json.NET either give us the path we want to begin with or expose anErrorContext.ErrorType
so we only do theaddMember
logic for missing required properties where we know the member is never in the path. The latter option was proposed a while back but never implemennted in Json.NET. See JamesNK/Newtonsoft.Json#1903See #39058 (comment) for more context about this issue.
The text was updated successfully, but these errors were encountered: