Skip to content

Commit 3338cac

Browse files
committed
Add feature to inverse read/write access logic
1 parent 0b82c78 commit 3338cac

File tree

4 files changed

+54
-0
lines changed

4 files changed

+54
-0
lines changed

.vscode/settings.json

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"java.format.comments.enabled": false,
3+
"java.format.enabled": false,
4+
"java.format.onType.enabled": false,
5+
"java.saveActions.organizeImports": false,
6+
}

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

@@ -176,6 +177,14 @@ public enum MapperFeature implements ConfigFeature
176177
*/
177178
OVERRIDE_PUBLIC_ACCESS_MODIFIERS(true),
178179

180+
/**
181+
* Feature that inverse logic in {@link JsonProperty#access}
182+
* for <code>READ_ONLY</code> and <code>WRITE_ONLY</code>.
183+
*<p>
184+
* Feature is disabled by default.
185+
*/
186+
INVERSE_READ_WRITE_ACCESS(false),
187+
179188
/*
180189
/**********************************************************************
181190
/* Type-handling features

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

+14
Original file line numberDiff line numberDiff line change
@@ -814,6 +814,20 @@ public JsonProperty.Access removeNonVisible(boolean inferMutators,
814814
if (acc == null) {
815815
acc = JsonProperty.Access.AUTO;
816816
}
817+
818+
// [databind#2951] add feature to inverse access logic
819+
if (_config.isEnabled(MapperFeature.INVERSE_READ_WRITE_ACCESS)) {
820+
switch (acc) {
821+
case READ_ONLY:
822+
acc = JsonProperty.Access.WRITE_ONLY;
823+
break;
824+
case WRITE_ONLY:
825+
acc = JsonProperty.Access.READ_ONLY;
826+
break;
827+
default:
828+
}
829+
}
830+
817831
switch (acc) {
818832
case READ_ONLY:
819833
// [databind#2719]: Need to add ignorals, first, keeping in mind

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

+25
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import com.fasterxml.jackson.annotation.JsonProperty;
1010

1111
import com.fasterxml.jackson.databind.*;
12+
import com.fasterxml.jackson.databind.json.JsonMapper;
1213

1314
public class ReadOrWriteOnlyTest extends BaseMapTest
1415
{
@@ -29,6 +30,15 @@ public int getY() {
2930
}
3031
}
3132

33+
// for [databind#2951], add feature to inverse access logic
34+
static class ReadAWriteB {
35+
@JsonProperty(access=JsonProperty.Access.READ_ONLY)
36+
public int a = 1;
37+
38+
@JsonProperty(access=JsonProperty.Access.WRITE_ONLY)
39+
public int b = 2;
40+
}
41+
3242
public static class Pojo935
3343
{
3444
private String firstName = "Foo";
@@ -144,6 +154,21 @@ public void testReadOnlyAndWriteOnly() throws Exception
144154
assertEquals(6, result.y);
145155
}
146156

157+
// [databind#2951] add feature to inverse access logic
158+
public void testInverseReadOnlyAndWriteOnly() throws Exception {
159+
ObjectMapper mapper = JsonMapper.builder()
160+
.enable(MapperFeature.INVERSE_READ_WRITE_ACCESS)
161+
.build();
162+
163+
String json = mapper.writeValueAsString(new ReadAWriteB());
164+
assertEquals("{\"b\":2}", json);
165+
166+
ReadAWriteB result = mapper.readValue("{\"a\":5, \"b\":6}", ReadAWriteB.class);
167+
assertNotNull(result);
168+
assertEquals(5, result.a);
169+
assertEquals(2, result.b);
170+
}
171+
147172
public void testReadOnly935() throws Exception
148173
{
149174
String json = MAPPER.writeValueAsString(new Pojo935());

0 commit comments

Comments
 (0)