Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add feature to inverse read/write access logic #4761

Open
wants to merge 1 commit into
base: 2.19
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/main/java/com/fasterxml/jackson/databind/MapperFeature.java
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 @@ -280,6 +281,16 @@ 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.
*
* @since 2.19
*/
ggrebert marked this conversation as resolved.
Show resolved Hide resolved
INVERSE_READ_WRITE_ACCESS(false),

/*
/******************************************************
/* Type-handling features
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -946,6 +946,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 @@ -11,6 +11,7 @@
import com.fasterxml.jackson.annotation.JsonProperty;

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

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

Expand All @@ -35,6 +36,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 @@ -151,6 +161,22 @@ public void testReadOnlyAndWriteOnly() throws Exception
assertEquals(6, result.y);
}

// [databind#2951] add feature to inverse access logic
@Test
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);
}

@Test
public void testReadOnly935() throws Exception
{
Expand Down