Skip to content

Commit

Permalink
Fix #288
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Dec 16, 2021
1 parent ff7a2e9 commit 399217e
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -425,18 +425,24 @@ public final CsvSchema typedSchemaForWithView(TypeReference<?> pojoTypeRef, Clas
protected CsvSchema _schemaFor(JavaType pojoType, LRUMap<JavaType,CsvSchema> 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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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());
}
}
4 changes: 4 additions & 0 deletions release-notes/CREDITS-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -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)

6 changes: 6 additions & 0 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 399217e

Please sign in to comment.