Skip to content

Commit 8299067

Browse files
committed
Add feature to inverse read/write access logic
1 parent 31f1e45 commit 8299067

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

src/main/java/com/fasterxml/jackson/databind/MapperFeature.java

+9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.fasterxml.jackson.databind;
22

3+
import com.fasterxml.jackson.annotation.JsonProperty;
34
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
45
import com.fasterxml.jackson.databind.cfg.ConfigFeature;
56

@@ -280,6 +281,14 @@ public enum MapperFeature implements ConfigFeature
280281
*/
281282
OVERRIDE_PUBLIC_ACCESS_MODIFIERS(true),
282283

284+
/**
285+
* Feature that inverse logic in {@link JsonProperty#access}
286+
* for <code>READ_ONLY</code> and <code>WRITE_ONLY</code>.
287+
*<p>
288+
* Feature is disabled by default.
289+
*/
290+
INVERSE_READ_WRITE_ACCESS(false),
291+
283292
/*
284293
/******************************************************
285294
/* Type-handling features

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

+14
Original file line numberDiff line numberDiff line change
@@ -946,6 +946,20 @@ public JsonProperty.Access removeNonVisible(boolean inferMutators,
946946
if (acc == null) {
947947
acc = JsonProperty.Access.AUTO;
948948
}
949+
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+
949963
switch (acc) {
950964
case READ_ONLY:
951965
// [databind#2719]: Need to add ignorals, first, keeping in mind

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

+26
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import com.fasterxml.jackson.annotation.JsonProperty;
1212

1313
import com.fasterxml.jackson.databind.*;
14+
import com.fasterxml.jackson.databind.json.JsonMapper;
1415

1516
import static org.junit.jupiter.api.Assertions.*;
1617

@@ -35,6 +36,15 @@ public int getY() {
3536
}
3637
}
3738

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+
3848
public static class Pojo935
3949
{
4050
private String firstName = "Foo";
@@ -151,6 +161,22 @@ public void testReadOnlyAndWriteOnly() throws Exception
151161
assertEquals(6, result.y);
152162
}
153163

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+
154180
@Test
155181
public void testReadOnly935() throws Exception
156182
{

0 commit comments

Comments
 (0)