Skip to content

Commit 30182e1

Browse files
fix: JSON deserialization setter case priority (#1831)
* fix: JSON deserialization setter case priority Fixes the problem of JSON deserialization ignoring the case of setters. Now case-sensitive setter method matches are considered first. Fixes: #1830. * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md --------- Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent 84216c5 commit 30182e1

File tree

2 files changed

+52
-13
lines changed

2 files changed

+52
-13
lines changed

google-http-client/src/main/java/com/google/api/client/util/FieldInfo.java

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -134,12 +134,20 @@ public static FieldInfo of(Field field) {
134134
}
135135

136136
/** Creates list of setter methods for a field only in declaring class. */
137-
private Method[] settersMethodForField(Field field) {
137+
private Method[] settersMethodForField(final Field field) {
138138
List<Method> methods = new ArrayList<>();
139+
String fieldSetter = "set" + Ascii.toUpperCase(field.getName().substring(0, 1));
140+
if (field.getName().length() > 1) {
141+
fieldSetter += field.getName().substring(1);
142+
}
139143
for (Method method : field.getDeclaringClass().getDeclaredMethods()) {
140-
if (Ascii.toLowerCase(method.getName()).equals("set" + Ascii.toLowerCase(field.getName()))
141-
&& method.getParameterTypes().length == 1) {
142-
methods.add(method);
144+
if (method.getParameterTypes().length == 1) {
145+
// add case-sensitive matches first in the list
146+
if (method.getName().equals(fieldSetter)) {
147+
methods.add(0, method);
148+
} else if (Ascii.toLowerCase(method.getName()).equals(Ascii.toLowerCase(fieldSetter))) {
149+
methods.add(method);
150+
}
143151
}
144152
}
145153
return methods.toArray(new Method[0]);
@@ -216,15 +224,13 @@ public Object getValue(Object obj) {
216224
* value.
217225
*/
218226
public void setValue(Object obj, Object value) {
219-
if (setters.length > 0) {
220-
for (Method method : setters) {
221-
if (value == null || method.getParameterTypes()[0].isAssignableFrom(value.getClass())) {
222-
try {
223-
method.invoke(obj, value);
224-
return;
225-
} catch (IllegalAccessException | InvocationTargetException e) {
226-
// try to set field directly
227-
}
227+
for (Method method : setters) {
228+
if (value == null || method.getParameterTypes()[0].isAssignableFrom(value.getClass())) {
229+
try {
230+
method.invoke(obj, value);
231+
return;
232+
} catch (IllegalAccessException | InvocationTargetException e) {
233+
// try to set field directly
228234
}
229235
}
230236
}

google-http-client/src/test/java/com/google/api/client/util/FieldInfoTest.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
package com.google.api.client.util;
1616

17+
import com.google.api.client.json.GenericJson;
1718
import junit.framework.TestCase;
1819

1920
/**
@@ -49,4 +50,36 @@ public void testEnumValue() {
4950
assertEquals(E.OTHER_VALUE, FieldInfo.of(E.OTHER_VALUE).<E>enumValue());
5051
assertEquals(E.NULL, FieldInfo.of(E.NULL).<E>enumValue());
5152
}
53+
54+
public static final class Data extends GenericJson {
55+
@Key String passcode;
56+
@Key String passCode;
57+
58+
public Data setPasscode(String passcode) {
59+
this.passcode = passcode;
60+
return this;
61+
}
62+
63+
public Data setPassCode(String passCode) {
64+
this.passCode = passCode;
65+
return this;
66+
}
67+
}
68+
69+
public void testSetValueCaseSensitivityPriority() {
70+
Data data = new Data();
71+
data.setPasscode("pass1");
72+
data.setPassCode("pass2");
73+
data.set("passCode", "passX");
74+
75+
assertEquals(data.passcode, "pass1");
76+
assertEquals(data.passCode, "passX");
77+
78+
data.setPasscode("pass1");
79+
data.setPassCode("pass2");
80+
data.set("passcode", "passX");
81+
82+
assertEquals(data.passcode, "passX");
83+
assertEquals(data.passCode, "pass2");
84+
}
5285
}

0 commit comments

Comments
 (0)