Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@JsonPropertyOrder ignored for dataformats-csv #279

Open
kenyee opened this issue Jun 28, 2021 · 2 comments
Open

@JsonPropertyOrder ignored for dataformats-csv #279

kenyee opened this issue Jun 28, 2021 · 2 comments
Labels
csv need-test-case Further progress requires inclusion of problem reproduction, ideally test case

Comments

@kenyee
Copy link

kenyee commented Jun 28, 2021

Using Kotlin, defining a class like this:


@JsonPropertyOrder(
    "Module", "FilePath", "Line", "Offset", "Severity", "UniqueID", "Category", "Description",
    "FileCount, LinesOfCode", "ComplexityTotal", "CoverageTotal",
    "NumBlockers", "NumCriticals", "NumMajors", "NumMinors", "NumInfos"
)
private data class SonarFileStatsCsv(
    @field:JsonProperty("Module")
    val module: String,
    @field:JsonProperty("FilePath")
    val filePath: String = "-",
    @field:JsonProperty("Line")
    val line: String = "-",
    @field:JsonProperty("Offset")
    val offset: String = "-",
    @field:JsonProperty("Severity")
    val severity: String = "-",
    @field:JsonProperty("UniqueID")
    val uniqueId: String = "-",
    @field:JsonProperty("Category")
    val category: String = "-",
    @field:JsonProperty("Description")
    val description: String = "-",
    @field:JsonProperty("FileCount")
    val fileCount: Int,
    @field:JsonProperty("LinesOfCode")
    val linesOfCode: Int,
    @field:JsonProperty("ComplexityTotal")
    val complexityTotal: Int,
    @field:JsonProperty("CoverageTotal")
    val coverageTotal: Float,
    @field:JsonProperty("NumBlockers")
    val numBlockers: Int,
    @field:JsonProperty("NumCriticals")
    val numCriticals: Int,
    @field:JsonProperty("NumMajors")
    val numMajors: Int,
    @field:JsonProperty("NumMinors")
    val numMinors: Int,
    @field:JsonProperty("NumInfos")
    val numInfos: Int
)

And writing it out using:


        val mapper = CsvMapper().apply {
            disable(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY)
        }
        val schema: CsvSchema = mapper.schemaFor(SonarFileStatsCsv::class.java)
            .withHeader()
     val csvWriter: ObjectWriter = mapper.writer(schema)
        withContext(Dispatchers.IO) {
            csvWriter.writeValue(File(fileName), sonarCsvStats)
        }

Doesn't work...the order is not preserved.
Workaround is to use the addColumn syntax to force the order:

        val schema = CsvSchema.builder()
            .addColumn("Module")
...

Also had to use @field:JsonProperty...using just @JsonProperty and withHeaders() wrote out the field names instead of the @JsonProperty name.

Used:
Kotlin 1.3.72
dataformats-csv 2.12.2
jackson 2.12.2

@cowtowncoder
Copy link
Member

Ok, first things first: would it be possible to minimize the example to include fewer columns?
Also, it'd be good to have unit test style assertions (write as a String, checkout output).
With a quick look usage looks correct: setting of SORT_PROPERTIES_ALPHABETICALLY has no effect with explicit ordering.

Beyond this there are 2 possibilities:

  1. This is general CSV handling problem, reproducible without Kotlin. If so, issue should remain here
  2. This only fails on Kotlin (for some reason), if so, would need to be transferred to jackson-module-kotlin (since CSV format module does not and cannot depend on Kotlin module).

It would be great if a simplified reproduction with Java-only usage could decide between (1) and (2).

@cowtowncoder cowtowncoder changed the title @JsonPropertyOrder ignored for dataformats-csv @JsonPropertyOrder ignored for dataformats-csv Jul 9, 2021
@cowtowncoder
Copy link
Member

To proceed any further, I will need to know whether this is Kotlin specific or not: the easiest way would be to provide Java-only reproduction.

@cowtowncoder cowtowncoder added the need-test-case Further progress requires inclusion of problem reproduction, ideally test case label Dec 19, 2021
@cowtowncoder cowtowncoder removed the 2.13 label Apr 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
csv need-test-case Further progress requires inclusion of problem reproduction, ideally test case
Projects
None yet
Development

No branches or pull requests

2 participants