From 14cba275b93071756b3af6e77f0d853b26da52cc Mon Sep 17 00:00:00 2001 From: Edgar Asatryan Date: Sun, 10 Nov 2019 12:52:29 +0400 Subject: [PATCH] CsvMapper now honors JsonProperty index. --- .../jackson/dataformat/csv/CsvMapper.java | 13 +++++++++- .../dataformat/csv/schema/SchemaTest.java | 25 ++++++++++++++++++- 2 files changed, 36 insertions(+), 2 deletions(-) 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 fda9a913..f48fe158 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 @@ -1,6 +1,11 @@ package com.fasterxml.jackson.dataformat.csv; +import static java.util.Comparator.comparing; +import static java.util.Comparator.naturalOrder; +import static java.util.Comparator.nullsLast; + import java.util.Collection; +import java.util.List; import com.fasterxml.jackson.core.type.TypeReference; @@ -459,7 +464,13 @@ protected void _addSchemaProperties(SerializerProvider ctxt, CsvSchema.Builder b } BeanDescription beanDesc = ctxt.introspectBeanDescription(pojoType); final AnnotationIntrospector intr = ctxt.getAnnotationIntrospector(); - for (BeanPropertyDefinition prop : beanDesc.findProperties()) { + List properties = beanDesc.findProperties(); + + // soring properties by @JsonProperty index + properties.sort(comparing(o -> intr.findPropertyIndex(ctxt.getConfig(), o.getPrimaryMember()), + nullsLast(naturalOrder()))); + + for (BeanPropertyDefinition prop : properties) { // ignore setter-only properties: if (!prop.couldSerialize()) { continue; diff --git a/csv/src/test/java/com/fasterxml/jackson/dataformat/csv/schema/SchemaTest.java b/csv/src/test/java/com/fasterxml/jackson/dataformat/csv/schema/SchemaTest.java index 0f55771f..57adfa64 100644 --- a/csv/src/test/java/com/fasterxml/jackson/dataformat/csv/schema/SchemaTest.java +++ b/csv/src/test/java/com/fasterxml/jackson/dataformat/csv/schema/SchemaTest.java @@ -2,6 +2,7 @@ import java.util.*; +import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonPropertyOrder; import com.fasterxml.jackson.dataformat.csv.CsvMapper; import com.fasterxml.jackson.dataformat.csv.CsvSchema; @@ -41,7 +42,17 @@ static abstract class YZ { public abstract int getY(); public abstract int getZ(); } - + + private static class PointWithExplicitIndices { + @JsonProperty(required = true, value = "y", index = 1) + public int y; + + @JsonProperty(required = true, value = "x", index = 2) + public int x; + + public int z; + } + /* /********************************************************************** /* Test methods @@ -206,4 +217,16 @@ public void testSchemaComposition() throws Exception assertEquals("lastName", it.next().getName()); assertEquals("x", it.next().getName()); } + + // For [dataformat-csv#115]: honor JsonProperty index + public void testSchemaWithExplicitIndices() + { + CsvSchema pointSchema = MAPPER.typedSchemaFor(PointWithExplicitIndices.class); + + assertEquals("y", pointSchema.column(0).getName()); + assertEquals("x", pointSchema.column(1).getName()); + assertEquals("z", pointSchema.column(2).getName()); + + _verifyLinks(pointSchema); + } }