Skip to content

Commit 8c48469

Browse files
committed
Final tweaks wrt #2951
1 parent a518c0a commit 8c48469

File tree

3 files changed

+39
-41
lines changed

3 files changed

+39
-41
lines changed

release-notes/VERSION-2.x

+3-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ Project: jackson-databind
99
#2461: Nested `@JsonUnwrapped` property names not correctly handled
1010
(reported by @plovell)
1111
(fix contributed by @SandeepGaur2016)
12-
#2951: Allow inverting `@JsonProperty(access=)` so it can work differently
13-
on server side and client side
12+
#2951: Allow inverting `@JsonProperty(access=)` with
13+
`MapperFeature.INVERSE_READ_WRITE_ACCESS` to work differently on
14+
server and client side
1415
(requested by @qianlong)
1516
(contributed by Geoffrey G)
1617
#4674: Allow setting global enum naming strategy similar to property naming strategy

src/main/java/com/fasterxml/jackson/databind/introspect/POJOPropertyBuilder.java

+11-14
Original file line numberDiff line numberDiff line change
@@ -854,12 +854,22 @@ public List<PropertyName> findAliases() {
854854
}
855855

856856
public JsonProperty.Access findAccess() {
857-
return fromMemberAnnotationsExcept(new WithMember<JsonProperty.Access>() {
857+
JsonProperty.Access acc = fromMemberAnnotationsExcept(new WithMember<JsonProperty.Access>() {
858858
@Override
859859
public JsonProperty.Access withMember(AnnotatedMember member) {
860860
return _annotationIntrospector.findPropertyAccess(member);
861861
}
862862
}, JsonProperty.Access.AUTO);
863+
864+
// [databind#2951] add feature to inverse access logic
865+
if (_config.isEnabled(MapperFeature.INVERSE_READ_WRITE_ACCESS)) {
866+
if (acc == JsonProperty.Access.READ_ONLY) {
867+
acc = JsonProperty.Access.WRITE_ONLY;
868+
} else if (acc == JsonProperty.Access.WRITE_ONLY) {
869+
acc = JsonProperty.Access.READ_ONLY;
870+
}
871+
}
872+
return acc;
863873
}
864874

865875
/*
@@ -947,19 +957,6 @@ public JsonProperty.Access removeNonVisible(boolean inferMutators,
947957
acc = JsonProperty.Access.AUTO;
948958
}
949959

950-
// [databind#2951] add feature to inverse access logic
951-
if (_config.isEnabled(MapperFeature.INVERSE_READ_WRITE_ACCESS)) {
952-
switch (acc) {
953-
case READ_ONLY:
954-
acc = JsonProperty.Access.WRITE_ONLY;
955-
break;
956-
case WRITE_ONLY:
957-
acc = JsonProperty.Access.READ_ONLY;
958-
break;
959-
default:
960-
}
961-
}
962-
963960
switch (acc) {
964961
case READ_ONLY:
965962
// [databind#2719]: Need to add ignorals, first, keeping in mind

src/test/java/com/fasterxml/jackson/databind/deser/filter/ReadOrWriteOnlyTest.java

+25-25
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,6 @@ public int getY() {
3636
}
3737
}
3838

39-
// for [databind#2951], add feature to inverse access logic
40-
static class ReadAWriteB {
41-
@JsonProperty(access=JsonProperty.Access.READ_ONLY)
42-
public int a = 1;
43-
44-
@JsonProperty(access=JsonProperty.Access.WRITE_ONLY)
45-
public int b = 2;
46-
}
47-
4839
public static class Pojo935
4940
{
5041
private String firstName = "Foo";
@@ -140,6 +131,15 @@ public void setWorks(String works) {
140131
}
141132
}
142133

134+
// for [databind#2951], add feature to inverse access logic
135+
static class ReadAWriteB {
136+
@JsonProperty(access=JsonProperty.Access.READ_ONLY)
137+
public int a = 1;
138+
139+
@JsonProperty(access=JsonProperty.Access.WRITE_ONLY)
140+
public int b = 2;
141+
}
142+
143143
/*
144144
/**********************************************************
145145
/* Test methods
@@ -161,22 +161,6 @@ public void testReadOnlyAndWriteOnly() throws Exception
161161
assertEquals(6, result.y);
162162
}
163163

164-
// [databind#2951] add feature to inverse access logic
165-
@Test
166-
public void testInverseReadOnlyAndWriteOnly() throws Exception {
167-
ObjectMapper mapper = JsonMapper.builder()
168-
.enable(MapperFeature.INVERSE_READ_WRITE_ACCESS)
169-
.build();
170-
171-
String json = mapper.writeValueAsString(new ReadAWriteB());
172-
assertEquals("{\"b\":2}", json);
173-
174-
ReadAWriteB result = mapper.readValue("{\"a\":5, \"b\":6}", ReadAWriteB.class);
175-
assertNotNull(result);
176-
assertEquals(5, result.a);
177-
assertEquals(2, result.b);
178-
}
179-
180164
@Test
181165
public void testReadOnly935() throws Exception
182166
{
@@ -236,4 +220,20 @@ public void testIssue2779() throws Exception
236220
Bean2779 newBean = MAPPER.readValue(json, Bean2779.class);
237221
assertNotNull(newBean);
238222
}
223+
224+
// [databind#2951] add feature to inverse access logic
225+
@Test
226+
public void testInverseReadOnlyAndWriteOnly() throws Exception {
227+
ObjectMapper mapper = JsonMapper.builder()
228+
.enable(MapperFeature.INVERSE_READ_WRITE_ACCESS)
229+
.build();
230+
231+
String json = mapper.writeValueAsString(new ReadAWriteB());
232+
assertEquals("{\"b\":2}", json);
233+
234+
ReadAWriteB result = mapper.readValue("{\"a\":5, \"b\":6}", ReadAWriteB.class);
235+
assertNotNull(result);
236+
assertEquals(5, result.a);
237+
assertEquals(2, result.b);
238+
}
239239
}

0 commit comments

Comments
 (0)