-
Notifications
You must be signed in to change notification settings - Fork 10.4k
Fix null property name handling logic in SystemTextJsonValidationMetadataProvider #47775
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
Conversation
Fixing what appears to be a negation bug causing null names to be passed to the underlying `JsonNamingPolicy`.
src/Mvc/Mvc.Core/src/ModelBinding/Metadata/SystemTextJsonValidationMetadataProvider.cs
Outdated
Show resolved
Hide resolved
…ationMetadataProvider.cs
@@ -55,7 +55,7 @@ public void CreateValidationMetadata(ValidationMetadataProviderContext context) | |||
|
|||
var propertyName = ReadPropertyNameFrom(context.Attributes); | |||
|
|||
if (string.IsNullOrEmpty(propertyName)) | |||
if (!string.IsNullOrEmpty(propertyName)) |
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 believe the existing code is correct. "If propertyName isn't set, read it from the context.Key.Name
".
However, the bug here appears to be the null suppression on line 60:
_jsonNamingPolicy.ConvertName(context.Key.Name!)
If context.Key.Name
can also be null
, then I think we have a problem.
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.
Good call, I misinterpreted the code as running the naming policy against propertyName
. In that case I'll close the PR and transfer https://github.com/dotnet/runtime/issues/84830 to aspnetcore so that you folks can make the appropriate fix.
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 ended up updating the PR so that it includes a null check for context.Key.Name
. That should emulate the semantics of JsonNamingPolicy.CamelCase
for other naming policies.
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.
Can we add a test for this in https://github.com/dotnet/aspnetcore/blob/main/src/Mvc/Mvc.Core/test/ModelBinding/Metadata/SystemTextJsonValidationMetadataProviderTest.cs?
I think it should work if we use the following key in the test cases:
var key = ModelMetadataIdentity.ForType(typeof(SampleTestClass));
src/Mvc/Mvc.Core/src/ModelBinding/Metadata/SystemTextJsonValidationMetadataProvider.cs
Outdated
Show resolved
Hide resolved
…ationMetadataProvider.cs Co-authored-by: Safia Abdalla <safia@microsoft.com>
@eerhardt I added the test I recommended in #47775 (review). Can you review both changes? Also, I observed something interesting which is that the bug only applies to scenarios that use the I was curious as to why this happens but I suppose it makes sense because you don't need to do any seperator-based concatenation for other policies? |
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.
Thanks!
This is a (now) known inconsistency between the policies. See the conversation at dotnet/runtime#85002 (comment). |
Fixing what appears to be a negation bug causing null names to be passed to the underlying
JsonNamingPolicy
.cf. dotnet/runtime#85002 (comment)
Fix #47835