Skip to content

Add failing test for #5049 #5061

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

Merged
merged 6 commits into from
Apr 3, 2025
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
* without Jackson itself being run on (or even built with) Java 14.
* In particular allows better support of {@code java.lang.Record}
* types (see <a href="https://openjdk.java.net/jeps/359">JEP 359</a>).
*<p>
* NOTE: actually Records really in JDK 17. But cannot safely change
* name of public class.
*
* @since 2.12
*/
Expand Down Expand Up @@ -197,7 +200,6 @@ protected Object[] recordComponents(Class<?> recordType) throws IllegalArgumentE

}

@Deprecated // since 2.18
static class RawTypeName {
public final Class<?> rawType;
public final String name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@

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

public class RecordWithReadOnlyTest extends DatabindTestUtil {

public class RecordWithReadOnlyTest extends DatabindTestUtil
{
record RecordWithReadOnly(int id, @JsonProperty(access = Access.READ_ONLY) String name) {
}

Expand Down Expand Up @@ -94,7 +94,10 @@ public void testSerializeReadOnlyNamedProperty() throws Exception {
public void testDeserializeReadOnlyNamedProperty() throws Exception {
RecordWithReadOnlyNamedProperty value = MAPPER.readValue(a2q("{'id':123,'name':'Bob'}"),
RecordWithReadOnlyNamedProperty.class);
assertEquals(new RecordWithReadOnlyNamedProperty(123, "Bob"), value); // BUG: should be `null` instead of "Bob"

// BUG: should be `null` instead of "Bob"
// 01-Apr-2025, tatu: Should be in "tofix", then?
assertEquals(new RecordWithReadOnlyNamedProperty(123, "Bob"), value);
}

/*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.fasterxml.jackson.databind.tofix;

import org.junit.jupiter.api.Test;

import com.fasterxml.jackson.annotation.JsonProperty;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.testutil.DatabindTestUtil;
import com.fasterxml.jackson.databind.testutil.failure.JacksonTestFailureExpected;

import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;

public class RecordWithReadOnly5049Test extends DatabindTestUtil
{
record ReadOnly5049Record(
@JsonProperty(value = "a", access = JsonProperty.Access.READ_ONLY) String a,
@JsonProperty(value = "b", access = JsonProperty.Access.READ_ONLY) String b) {
}

static class ReadOnly5049Pojo
{
protected String a, b;

ReadOnly5049Pojo(
@JsonProperty(value = "a", access = JsonProperty.Access.READ_ONLY) String a,
@JsonProperty(value = "b", access = JsonProperty.Access.READ_ONLY) String b) {
this.a = a;
this.b = b;
}

public String getA() { return a; }
public String getB() { return b; }
}

private final ObjectMapper MAPPER = newJsonMapper();

@Test
void testRoundtripPOJO() throws Exception
{
String json = MAPPER.writeValueAsString(new ReadOnly5049Pojo("hello", "world"));
//System.err.println("JSON/pojo: "+json);
ReadOnly5049Pojo pojo = MAPPER.readerFor(ReadOnly5049Pojo.class).readValue(json);
assertNotNull(pojo);
assertNull(pojo.a);
assertNull(pojo.b);
}

@JacksonTestFailureExpected
@Test
void testRoundtripRecord() throws Exception
{
// json = MAPPER.writeValueAsString(new ReadOnly5049Record("hello", "world"));
String json = "{\"a\":\"hello\",\"b\":\"world\"}";
ReadOnly5049Record record = MAPPER.readValue(json, ReadOnly5049Record.class);
assertNotNull(record);
assertNull(record.a());
assertNull(record.b());
}
}