From 66845ba6c2be63ba5039c9697810c31844c38494 Mon Sep 17 00:00:00 2001 From: f4lco Date: Fri, 8 Nov 2024 22:23:06 +0100 Subject: [PATCH] Reconfigure Jackson to make JSON output stable --- .../resources/com/libyear/expectedReport.json | 16 ++++---------- .../com/libyear/traversal/ReportingVisitor.kt | 21 +++++++++++++++---- 2 files changed, 21 insertions(+), 16 deletions(-) diff --git a/libyear-gradle-plugin/src/functionalTest/resources/com/libyear/expectedReport.json b/libyear-gradle-plugin/src/functionalTest/resources/com/libyear/expectedReport.json index ac57429..f19ef68 100644 --- a/libyear-gradle-plugin/src/functionalTest/resources/com/libyear/expectedReport.json +++ b/libyear-gradle-plugin/src/functionalTest/resources/com/libyear/expectedReport.json @@ -1,24 +1,16 @@ { "collected" : [ { "module" : { - "version" : "1.9", "group" : "org.apache.commons", - "module" : { - "group" : "org.apache.commons", - "name" : "commons-text" - }, - "name" : "commons-text" + "name" : "commons-text", + "version" : "1.9" }, "lag_days" : 1361 }, { "module" : { - "version" : "4.4", "group" : "org.apache.commons", - "module" : { - "group" : "org.apache.commons", - "name" : "commons-collections4" - }, - "name" : "commons-collections4" + "name" : "commons-collections4", + "version" : "4.4" }, "lag_days" : 1806 } ], diff --git a/libyear-gradle-plugin/src/main/kotlin/com/libyear/traversal/ReportingVisitor.kt b/libyear-gradle-plugin/src/main/kotlin/com/libyear/traversal/ReportingVisitor.kt index a841f9c..6193a72 100644 --- a/libyear-gradle-plugin/src/main/kotlin/com/libyear/traversal/ReportingVisitor.kt +++ b/libyear-gradle-plugin/src/main/kotlin/com/libyear/traversal/ReportingVisitor.kt @@ -1,6 +1,8 @@ package com.libyear.traversal +import com.fasterxml.jackson.databind.MapperFeature import com.fasterxml.jackson.databind.ObjectMapper +import com.fasterxml.jackson.databind.SerializationFeature import com.fasterxml.jackson.module.kotlin.registerKotlinModule import com.libyear.sourcing.VersionOracle import com.libyear.util.formatApproximate @@ -85,14 +87,14 @@ class ReportingVisitor( val report = mapOf( "collected" to collected.map { mapOf( - "module" to it.module, + "module" to ReportModule(it.module), "lag_days" to it.lag.toDays() ) }, - "missing_info" to missingInfo, - "errors" to errors + "missing_info" to missingInfo.map(::ReportModule), + "errors" to errors.map(::ReportModule) ) - val objectMapper = ObjectMapper().registerKotlinModule() + val objectMapper = ObjectMapper().registerKotlinModule().configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true) val json = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(report) val reportFile = File(project.buildDir, "reports/libyear/libyear.json") reportFile.parentFile.mkdirs() @@ -106,4 +108,15 @@ class ReportingVisitor( ModuleVersionIdentifier::getName, ModuleVersionIdentifier::getVersion ) + + /** JSON-serializable subset of [ModuleVersionIdentifier]. **/ + data class ReportModule( + val group: String, + val name: String, + val version: String + ) { + + constructor(mvi: ModuleVersionIdentifier): + this(mvi.group, mvi.name, mvi.version) + } }