diff --git a/csv/src/main/java/com/fasterxml/jackson/dataformat/csv/CsvMapper.java b/csv/src/main/java/com/fasterxml/jackson/dataformat/csv/CsvMapper.java index 31830f01..d2cd4a4e 100644 --- a/csv/src/main/java/com/fasterxml/jackson/dataformat/csv/CsvMapper.java +++ b/csv/src/main/java/com/fasterxml/jackson/dataformat/csv/CsvMapper.java @@ -425,18 +425,24 @@ public final CsvSchema typedSchemaForWithView(TypeReference pojoTypeRef, Clas protected CsvSchema _schemaFor(JavaType pojoType, LRUMap schemas, boolean typed, Class view) { - synchronized (schemas) { - CsvSchema s = schemas.get(pojoType); - if (s != null) { - return s; + // 15-Dec-2021, tatu: [dataformats-text#288] Only cache if we don't have + // a view, to avoid conflicts + if (view == null) { + synchronized (schemas) { + CsvSchema s = schemas.get(pojoType); + if (s != null) { + return s; + } } } final AnnotationIntrospector intr = _deserializationConfig.getAnnotationIntrospector(); CsvSchema.Builder builder = CsvSchema.builder(); _addSchemaProperties(builder, intr, typed, pojoType, null, view); CsvSchema result = builder.build(); - synchronized (schemas) { - schemas.put(pojoType, result); + if (view == null) { // only cache without view (see above) + synchronized (schemas) { + schemas.put(pojoType, result); + } } return result; } diff --git a/csv/src/test/java/com/fasterxml/jackson/dataformat/csv/schema/SchemaCaching288Test.java b/csv/src/test/java/com/fasterxml/jackson/dataformat/csv/schema/SchemaCaching288Test.java new file mode 100644 index 00000000..95b55984 --- /dev/null +++ b/csv/src/test/java/com/fasterxml/jackson/dataformat/csv/schema/SchemaCaching288Test.java @@ -0,0 +1,60 @@ +package com.fasterxml.jackson.dataformat.csv.schema; + +import com.fasterxml.jackson.annotation.JsonPropertyOrder; +import com.fasterxml.jackson.annotation.JsonView; + +import com.fasterxml.jackson.dataformat.csv.CsvMapper; +import com.fasterxml.jackson.dataformat.csv.CsvSchema; +import com.fasterxml.jackson.dataformat.csv.ModuleTestBase; + +public class SchemaCaching288Test extends ModuleTestBase +{ + static class ViewA { } + static class ViewAA extends ViewA { } + static class ViewB { } + static class ViewBB extends ViewB { } + + @JsonPropertyOrder({ "a", "aa", "b" }) + static class Bean288 + { + @JsonView({ ViewA.class, ViewB.class }) + public String a = "1"; + + @JsonView({ViewAA.class }) + public String aa = "2"; + + @JsonView(ViewB.class) + public String b = "3"; + } + + /* + /********************************************************** + /* Test methods + /********************************************************** + */ + + // [dataformats-text#288]: caching should not overlap with View + public void testCachingNoViewFirst() throws Exception + { + CsvMapper mapper1 = mapperForCsv(); + CsvSchema schemaNoView = mapper1.schemaFor(Bean288.class); + assertEquals("1,2,3", + mapper1.writer(schemaNoView).writeValueAsString(new Bean288()).trim()); + + CsvSchema schemaB = mapper1.schemaForWithView(Bean288.class, ViewB.class); + assertEquals("1,3", mapper1.writer(schemaB).withView(ViewB.class) + .writeValueAsString(new Bean288()).trim()); + } + + // [dataformats-text#288]: caching should not overlap with View + public void testCachingWithViewFirst() throws Exception + { + CsvMapper mapper1 = mapperForCsv(); + CsvSchema schemaB = mapper1.schemaForWithView(Bean288.class, ViewB.class); + assertEquals("1,3", mapper1.writer(schemaB).withView(ViewB.class) + .writeValueAsString(new Bean288()).trim()); + CsvSchema schemaNoView = mapper1.schemaFor(Bean288.class); + assertEquals("1,2,3", + mapper1.writer(schemaNoView).writeValueAsString(new Bean288()).trim()); + } +} diff --git a/release-notes/CREDITS-2.x b/release-notes/CREDITS-2.x index 9fed4a7f..e0fadc8f 100644 --- a/release-notes/CREDITS-2.x +++ b/release-notes/CREDITS-2.x @@ -189,4 +189,8 @@ PJ Fanning (pjfanning@github) are empty (2.13.0) +Falk Hanisch (mrpiggi@github) +#288: Caching conflict when creating CSV schemas with different views + for the same POJO + (2.13.1) diff --git a/release-notes/VERSION-2.x b/release-notes/VERSION-2.x index 8d4a0dc7..44700397 100644 --- a/release-notes/VERSION-2.x +++ b/release-notes/VERSION-2.x @@ -18,6 +18,12 @@ Active Maintainers: No changes since 2.13 +2.13.1 (not yet released) + +#288: Caching conflict when creating CSV schemas with different views + for the same POJO + (reported by Falk H) + 2.13.0 (30-Sep-2021) #219: (toml) Add TOML (https://en.wikipedia.org/wiki/TOML) support