From 6b441edd1913809467ce5608c93a7cabb8ad2fde Mon Sep 17 00:00:00 2001 From: Tatu Saloranta Date: Sat, 20 May 2023 20:38:39 -0700 Subject: [PATCH] Fix #3906 by forcing Record constructors to be visible regardless of overrides --- .../databind/cfg/MapperConfigBase.java | 5 +++++ .../RecordCreatorVisibility3906Test.java | 22 +++++++++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 src/test-jdk14/java/com/fasterxml/jackson/databind/records/RecordCreatorVisibility3906Test.java diff --git a/src/main/java/com/fasterxml/jackson/databind/cfg/MapperConfigBase.java b/src/main/java/com/fasterxml/jackson/databind/cfg/MapperConfigBase.java index b0da7abc20..ad0fd4897d 100644 --- a/src/main/java/com/fasterxml/jackson/databind/cfg/MapperConfigBase.java +++ b/src/main/java/com/fasterxml/jackson/databind/cfg/MapperConfigBase.java @@ -832,6 +832,11 @@ public final VisibilityChecker getDefaultVisibilityChecker(Class baseType, vc = VisibilityChecker.Std.allPublicInstance(); } else { vc = getDefaultVisibilityChecker(); + // 20-May-2023, tatu: [databind#3906] Must reset visibility for Records + // to avoid hiding Constructors + if (ClassUtil.isRecordType(baseType)) { + vc = vc.withCreatorVisibility(Visibility.NON_PRIVATE); + } } AnnotationIntrospector intr = getAnnotationIntrospector(); if (intr != null) { diff --git a/src/test-jdk14/java/com/fasterxml/jackson/databind/records/RecordCreatorVisibility3906Test.java b/src/test-jdk14/java/com/fasterxml/jackson/databind/records/RecordCreatorVisibility3906Test.java new file mode 100644 index 0000000000..fa620e6b04 --- /dev/null +++ b/src/test-jdk14/java/com/fasterxml/jackson/databind/records/RecordCreatorVisibility3906Test.java @@ -0,0 +1,22 @@ +package com.fasterxml.jackson.databind.records; + +import com.fasterxml.jackson.annotation.JsonAutoDetect; +import com.fasterxml.jackson.annotation.PropertyAccessor; +import com.fasterxml.jackson.databind.*; + +// [databind#3906] +public class RecordCreatorVisibility3906Test extends BaseMapTest +{ + // [databind#3906] + record Record3906(String string, int integer) { } + + // [databind#3906] + public void testRecordCreatorVisibility3906() throws Exception { + ObjectMapper mapper = jsonMapperBuilder() + .visibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.NONE) + .visibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY) + .build(); + Record3906 recordTest_deserialized = mapper.readValue("{}", Record3906.class); + assertEquals(new Record3906(null, 0), recordTest_deserialized); + } +}