Skip to content

Commit 399217e

Browse files
committed
Fix #288
1 parent ff7a2e9 commit 399217e

File tree

4 files changed

+82
-6
lines changed

4 files changed

+82
-6
lines changed

csv/src/main/java/com/fasterxml/jackson/dataformat/csv/CsvMapper.java

+12-6
Original file line numberDiff line numberDiff line change
@@ -425,18 +425,24 @@ public final CsvSchema typedSchemaForWithView(TypeReference<?> pojoTypeRef, Clas
425425
protected CsvSchema _schemaFor(JavaType pojoType, LRUMap<JavaType,CsvSchema> schemas,
426426
boolean typed, Class<?> view)
427427
{
428-
synchronized (schemas) {
429-
CsvSchema s = schemas.get(pojoType);
430-
if (s != null) {
431-
return s;
428+
// 15-Dec-2021, tatu: [dataformats-text#288] Only cache if we don't have
429+
// a view, to avoid conflicts
430+
if (view == null) {
431+
synchronized (schemas) {
432+
CsvSchema s = schemas.get(pojoType);
433+
if (s != null) {
434+
return s;
435+
}
432436
}
433437
}
434438
final AnnotationIntrospector intr = _deserializationConfig.getAnnotationIntrospector();
435439
CsvSchema.Builder builder = CsvSchema.builder();
436440
_addSchemaProperties(builder, intr, typed, pojoType, null, view);
437441
CsvSchema result = builder.build();
438-
synchronized (schemas) {
439-
schemas.put(pojoType, result);
442+
if (view == null) { // only cache without view (see above)
443+
synchronized (schemas) {
444+
schemas.put(pojoType, result);
445+
}
440446
}
441447
return result;
442448
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.fasterxml.jackson.dataformat.csv.schema;
2+
3+
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
4+
import com.fasterxml.jackson.annotation.JsonView;
5+
6+
import com.fasterxml.jackson.dataformat.csv.CsvMapper;
7+
import com.fasterxml.jackson.dataformat.csv.CsvSchema;
8+
import com.fasterxml.jackson.dataformat.csv.ModuleTestBase;
9+
10+
public class SchemaCaching288Test extends ModuleTestBase
11+
{
12+
static class ViewA { }
13+
static class ViewAA extends ViewA { }
14+
static class ViewB { }
15+
static class ViewBB extends ViewB { }
16+
17+
@JsonPropertyOrder({ "a", "aa", "b" })
18+
static class Bean288
19+
{
20+
@JsonView({ ViewA.class, ViewB.class })
21+
public String a = "1";
22+
23+
@JsonView({ViewAA.class })
24+
public String aa = "2";
25+
26+
@JsonView(ViewB.class)
27+
public String b = "3";
28+
}
29+
30+
/*
31+
/**********************************************************
32+
/* Test methods
33+
/**********************************************************
34+
*/
35+
36+
// [dataformats-text#288]: caching should not overlap with View
37+
public void testCachingNoViewFirst() throws Exception
38+
{
39+
CsvMapper mapper1 = mapperForCsv();
40+
CsvSchema schemaNoView = mapper1.schemaFor(Bean288.class);
41+
assertEquals("1,2,3",
42+
mapper1.writer(schemaNoView).writeValueAsString(new Bean288()).trim());
43+
44+
CsvSchema schemaB = mapper1.schemaForWithView(Bean288.class, ViewB.class);
45+
assertEquals("1,3", mapper1.writer(schemaB).withView(ViewB.class)
46+
.writeValueAsString(new Bean288()).trim());
47+
}
48+
49+
// [dataformats-text#288]: caching should not overlap with View
50+
public void testCachingWithViewFirst() throws Exception
51+
{
52+
CsvMapper mapper1 = mapperForCsv();
53+
CsvSchema schemaB = mapper1.schemaForWithView(Bean288.class, ViewB.class);
54+
assertEquals("1,3", mapper1.writer(schemaB).withView(ViewB.class)
55+
.writeValueAsString(new Bean288()).trim());
56+
CsvSchema schemaNoView = mapper1.schemaFor(Bean288.class);
57+
assertEquals("1,2,3",
58+
mapper1.writer(schemaNoView).writeValueAsString(new Bean288()).trim());
59+
}
60+
}

release-notes/CREDITS-2.x

+4
Original file line numberDiff line numberDiff line change
@@ -189,4 +189,8 @@ PJ Fanning (pjfanning@github)
189189
are empty
190190
(2.13.0)
191191

192+
Falk Hanisch (mrpiggi@github)
193+
#288: Caching conflict when creating CSV schemas with different views
194+
for the same POJO
195+
(2.13.1)
192196

release-notes/VERSION-2.x

+6
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ Active Maintainers:
1818

1919
No changes since 2.13
2020

21+
2.13.1 (not yet released)
22+
23+
#288: Caching conflict when creating CSV schemas with different views
24+
for the same POJO
25+
(reported by Falk H)
26+
2127
2.13.0 (30-Sep-2021)
2228

2329
#219: (toml) Add TOML (https://en.wikipedia.org/wiki/TOML) support

0 commit comments

Comments
 (0)