Skip to content

Commit 1fa2d86

Browse files
authored
Record constructor with single write-only parameter should result in properties-based creator, to fix #3897. (#3910)
1 parent 7547591 commit 1fa2d86

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

src/main/java/com/fasterxml/jackson/databind/deser/BasicDeserializerFactory.java

+4
Original file line numberDiff line numberDiff line change
@@ -1032,6 +1032,10 @@ private boolean _checkIfCreatorPropertyBased(BeanDescription beanDesc,
10321032
}
10331033
}
10341034
}
1035+
// [databind#3897]: Record canonical constructor will have implicitly named propDef
1036+
if (propDef != null && !propDef.isExplicitlyNamed() && beanDesc.isRecordType()) {
1037+
return true;
1038+
}
10351039
// in absence of everything else, default to delegating
10361040
return false;
10371041
}

src/test-jdk14/java/com/fasterxml/jackson/databind/records/RecordBasicsTest.java

+61
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,20 @@ record SnakeRecord(String myId, String myValue){}
4040

4141
record RecordWithJsonDeserialize(int id, @JsonDeserialize(converter = StringTrimmer.class) String name) { }
4242

43+
record RecordSingleWriteOnly(@JsonProperty(access = JsonProperty.Access.WRITE_ONLY) int id) { }
44+
45+
record RecordSomeWriteOnly(
46+
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY) int id,
47+
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY) String name,
48+
String email) {
49+
}
50+
51+
record RecordAllWriteOnly(
52+
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY) int id,
53+
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY) String name,
54+
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY) String email) {
55+
}
56+
4357
private final ObjectMapper MAPPER = newJsonMapper();
4458

4559
/*
@@ -204,6 +218,53 @@ public void testDeserializeJsonDeserializeRecord() throws Exception {
204218
assertEquals(new RecordWithJsonDeserialize(123, "Bob"), value);
205219
}
206220

221+
/*
222+
/**********************************************************************
223+
/* Test methods, JsonProperty(access=WRITE_ONLY)
224+
/**********************************************************************
225+
*/
226+
227+
public void testSerialize_SingleWriteOnlyParameter() throws Exception {
228+
String json = MAPPER.writeValueAsString(new RecordSingleWriteOnly(123));
229+
230+
assertEquals("{}", json);
231+
}
232+
233+
// [databind#3897]
234+
public void testDeserialize_SingleWriteOnlyParameter() throws Exception {
235+
RecordSingleWriteOnly value = MAPPER.readValue("{\"id\":123}", RecordSingleWriteOnly.class);
236+
237+
assertEquals(new RecordSingleWriteOnly(123), value);
238+
}
239+
240+
public void testSerialize_SomeWriteOnlyParameter() throws Exception {
241+
String json = MAPPER.writeValueAsString(new RecordSomeWriteOnly(123, "Bob", "[email protected]"));
242+
243+
assertEquals("{\"email\":\"[email protected]\"}", json);
244+
}
245+
246+
public void testDeserialize_SomeWriteOnlyParameter() throws Exception {
247+
RecordSomeWriteOnly value = MAPPER.readValue(
248+
"{\"id\":123,\"name\":\"Bob\",\"email\":\"[email protected]\"}",
249+
RecordSomeWriteOnly.class);
250+
251+
assertEquals(new RecordSomeWriteOnly(123, "Bob", "[email protected]"), value);
252+
}
253+
254+
public void testSerialize_AllWriteOnlyParameter() throws Exception {
255+
String json = MAPPER.writeValueAsString(new RecordAllWriteOnly(123, "Bob", "[email protected]"));
256+
257+
assertEquals("{}", json);
258+
}
259+
260+
public void testDeserialize_AllWriteOnlyParameter() throws Exception {
261+
RecordAllWriteOnly value = MAPPER.readValue(
262+
"{\"id\":123,\"name\":\"Bob\",\"email\":\"[email protected]\"}",
263+
RecordAllWriteOnly.class);
264+
265+
assertEquals(new RecordAllWriteOnly(123, "Bob", "[email protected]"), value);
266+
}
267+
207268
/*
208269
/**********************************************************************
209270
/* Internal helper methods

0 commit comments

Comments
 (0)