Skip to content

Commit

Permalink
Add feature to inverse read/write access logic
Browse files Browse the repository at this point in the history
  • Loading branch information
ggrebert committed Dec 2, 2020
1 parent 0b82c78 commit 8b8d98c
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"java.format.comments.enabled": false,
"java.format.enabled": false,
"java.format.onType.enabled": false,
"java.saveActions.organizeImports": false,
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.fasterxml.jackson.databind;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.cfg.ConfigFeature;

Expand Down Expand Up @@ -176,6 +177,14 @@ public enum MapperFeature implements ConfigFeature
*/
OVERRIDE_PUBLIC_ACCESS_MODIFIERS(true),

/**
* Feature that inverse logic in {@link JsonProperty#access}
* for <code>READ_ONLY</code> and <code>WRITE_ONLY</code>.
*<p>
* Feature is disabled by default.
*/
INVERSE_READ_WRITE_ACCESS(false),

/*
/**********************************************************************
/* Type-handling features
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -814,6 +814,20 @@ public JsonProperty.Access removeNonVisible(boolean inferMutators,
if (acc == null) {
acc = JsonProperty.Access.AUTO;
}

// [databind#2951] add feature to inverse access logic
if (_config.isEnabled(MapperFeature.INVERSE_READ_WRITE_ACCESS)) {
switch (acc) {
case READ_ONLY:
acc = JsonProperty.Access.WRITE_ONLY;
break;
case WRITE_ONLY:
acc = JsonProperty.Access.READ_ONLY;
break;
default:
}
}

switch (acc) {
case READ_ONLY:
// [databind#2719]: Need to add ignorals, first, keeping in mind
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.fasterxml.jackson.annotation.JsonProperty;

import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.json.JsonMapper;

public class ReadOrWriteOnlyTest extends BaseMapTest
{
Expand All @@ -29,6 +30,15 @@ public int getY() {
}
}

// for [databind#2951], add feature to inverse access logic
static class ReadAWriteB {
@JsonProperty(access=JsonProperty.Access.READ_ONLY)
public int a = 1;

@JsonProperty(access=JsonProperty.Access.WRITE_ONLY)
public int b = 2;
}

public static class Pojo935
{
private String firstName = "Foo";
Expand Down Expand Up @@ -144,6 +154,21 @@ public void testReadOnlyAndWriteOnly() throws Exception
assertEquals(6, result.y);
}

// [databind#2951] add feature to inverse access logic
public void testInverseReadOnlyAndWriteOnly() throws Exception {
ObjectMapper mapper = JsonMapper.builder()
.enable(MapperFeature.INVERSE_READ_WRITE_ACCESS)
.build();

String json = mapper.writeValueAsString(new ReadAWriteB());
assertEquals("{\"b\":2}", json);

ReadAWriteB result = mapper.readValue("{\"a\":5, \"b\":6}", ReadAWriteB.class);
assertNotNull(result);
assertEquals(5, result.a);
assertEquals(2, result.b);
}

public void testReadOnly935() throws Exception
{
String json = MAPPER.writeValueAsString(new Pojo935());
Expand Down

0 comments on commit 8b8d98c

Please sign in to comment.