-
Notifications
You must be signed in to change notification settings - Fork 1k
Fix case sensitivity #285
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
Fix case sensitivity #285
Conversation
86d9970
to
0a64979
Compare
Codecov Report
@@ Coverage Diff @@
## master #285 +/- ##
==========================================
+ Coverage 81.38% 81.46% +0.08%
==========================================
Files 41 41
Lines 4979 4980 +1
==========================================
+ Hits 4052 4057 +5
Misses 807 807
+ Partials 120 116 -4
Continue to review full report at Codecov.
|
if !ctx.caseSensitive() { | ||
for k, binding := range bindings { | ||
if _, found := fields[strings.ToLower(k)]; !found { | ||
fields[strings.ToLower(k)] = binding.Decoder.(*structFieldDecoder) |
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.
Do the toLower calls on lines 521 and 527 need conditionalizing as well?
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.
And if so, is there a test case that could exercise that path?
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.
Do the toLower calls on lines 521 and 527 need conditionalizing as well?
I don't think so. In fact, I don't think we even need to check for the nil condition there.
Lines 516 to 519 in 7cceb6c
fieldDecoder = decoder.fields[field] | |
if fieldDecoder == nil { | |
fieldDecoder = decoder.fields[strings.ToLower(field)] | |
} |
Here decoder
is a generalStructDecoder
, which is created here:
Line 485 in 7cceb6c
return &generalStructDecoder{typ, fields, false} |
The fields
in this struct comes from:
Lines 31 to 34 in 7cceb6c
fields := map[string]*structFieldDecoder{} | |
for k, binding := range bindings { | |
fields[k] = binding.Decoder.(*structFieldDecoder) | |
} |
The *structFieldDecoder
is a *StructDescriptor
which is created from:
Line 14 in 7cceb6c
structDescriptor := describeStruct(ctx, typ) |
Line 324 in 7cceb6c
func describeStruct(ctx *ctx, typ reflect2.Type) *StructDescriptor { |
which creates from:
Lines 388 to 391 in 7cceb6c
func createStructDescriptor(ctx *ctx, typ reflect2.Type, bindings []*Binding, embeddedBindings []*Binding) *StructDescriptor { | |
structDescriptor := &StructDescriptor{ | |
Type: typ, | |
Fields: bindings, |
From the above snippet ^, *StructDescriptor
can never be nil.
Also, if the decoder is supposed to be case sensitive, the conversion to lower case is taken care of while constructing the struct decoder:
Lines 35 to 39 in 7cceb6c
for k, binding := range bindings { | |
if _, found := fields[strings.ToLower(k)]; !found { | |
fields[strings.ToLower(k)] = binding.Decoder.(*structFieldDecoder) | |
} | |
} |
so the toLower()
which is used below in lines 521 and 527 is not called and not needed.
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.
If this makes sense, I'll remove the lines. But I want to first double check if this is indeed correct.
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.
Also, if the decoder is supposed to be case sensitive, the conversion to lower case is take care of while constructing the struct decoder:
That's my understanding as well. I agree that we should remove the lines.
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.
That's my understanding as well. I agree that we should remove the lines.
Removed. (tests were failing, investigating why)
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.
Wait, if the decoder is case insensitive, we need to keep the lines, because decoder.fields[field]
might return empty, and we can only find the decoder via decoder.fields[strings.ToLower(field)]
.
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.
Yup, added back the lines along with a new test case.
And if so, is there a test case that could exercise that path?
@liggitt the case insensitive ones are the ones that exercise the path.
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.
Yup, added back the lines along with a new test case.
+1
0fd1038
to
9837bb9
Compare
@nikhita I found the root cause of not ignoring I think in the test cases, we need to add a case where a struct has more than 11 fields, to exercise the Btw, we might have accidentally improved the performance of jsoniter. Before our fixes, as long as a struct has camelCase field names, the decoder will always be the Lines 368 to 372 in 7cceb6c
|
9837bb9
to
3830516
Compare
@caesarxuchao updated.
🎉 |
lgtm |
We should add an equivalent case-sensitivity test to k8s, so that any future changes in JSON parsing catch it. |
LGTM By the power vested in me, I hereby merge your fix. |
Fix case sensitivity
Fixes kubernetes/kubernetes#64612 (comment)
Follow-up for #282
Also see https://github.com/json-iterator/go/pull/282/files#r194283181