Skip to content

Commit f7c37be

Browse files
committed
Fix #2882: make naming rules for getters stricter
1 parent db17b3e commit f7c37be

File tree

2 files changed

+50
-3
lines changed

2 files changed

+50
-3
lines changed

src/main/java/tools/jackson/databind/util/BeanUtil.java

+5
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ public class BeanUtil
2121
/**********************************************************************
2222
*/
2323

24+
/**
25+
* @deprecated since 3.0.0-rc2 Use {@link tools.jackson.databind.introspect.DefaultAccessorNamingStrategy}
26+
* instead
27+
*/
28+
@Deprecated // since 3.0.0-rc2
2429
public static String stdManglePropertyName(final String basename, final int offset)
2530
{
2631
final int end = basename.length();

src/test/java/tools/jackson/databind/introspect/BeanNamingTest.java

+45-3
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
import org.junit.jupiter.api.Test;
44

55
import tools.jackson.databind.*;
6+
import tools.jackson.databind.exc.UnrecognizedPropertyException;
67
import tools.jackson.databind.testutil.DatabindTestUtil;
78

89
import static org.junit.jupiter.api.Assertions.assertEquals;
10+
import static org.junit.jupiter.api.Assertions.fail;
911

1012
// Tests for [databind#653]
1113
public class BeanNamingTest extends DatabindTestUtil
@@ -22,15 +24,55 @@ public int getA() {
2224
}
2325
}
2426

27+
// [databind#2882]
28+
static class Bean2882 {
29+
// These should NOT be detected as "getters" due to naming conventions
30+
public boolean island() { return true; }
31+
public boolean is_bad() { return true; }
32+
33+
public int get_value() { return -2; }
34+
public int getter() { return -3; }
35+
36+
// This is regular and should be detected
37+
public int getX() { return 1; }
38+
39+
// And bad "setter" too
40+
public void setter(int x) {
41+
throw new IllegalStateException("Should not get called");
42+
}
43+
}
44+
45+
private final ObjectMapper MAPPER = newJsonMapper();
46+
2547
// 24-Sep-2017, tatu: Used to test for `MapperFeature.USE_STD_BEAN_NAMING`, but with 3.x
2648
// that is always enabled.
2749
@Test
2850
public void testMultipleLeadingCapitalLetters() throws Exception
2951
{
30-
ObjectMapper mapper = newJsonMapper();
3152
assertEquals(a2q("{'URL':'http:'}"),
32-
mapper.writeValueAsString(new URLBean()));
53+
MAPPER.writeValueAsString(new URLBean()));
3354
assertEquals(a2q("{'a':3}"),
34-
mapper.writeValueAsString(new ABean()));
55+
MAPPER.writeValueAsString(new ABean()));
56+
}
57+
58+
// [databind#2882]
59+
@Test
60+
void testBadCasingForGetters() throws Exception
61+
{
62+
assertEquals(a2q("{'x':1}"),
63+
MAPPER.writeValueAsString(new Bean2882()));
64+
}
65+
66+
@Test
67+
void testBadCasingForSetters() throws Exception
68+
{
69+
try {
70+
MAPPER.readerFor(Bean2882.class)
71+
.with(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
72+
.readValue(a2q("{'ter':1}"));
73+
fail("Should not pass");
74+
} catch (UnrecognizedPropertyException e) {
75+
verifyException(e, "Unrecognized property \"ter\"");
76+
}
3577
}
3678
}

0 commit comments

Comments
 (0)