From 62b606fd78602daa600e1e79d763c7adc1f51604 Mon Sep 17 00:00:00 2001 From: Sergio Pro <22973227+serpro69@users.noreply.github.com> Date: Fri, 16 Feb 2024 21:07:35 +0100 Subject: [PATCH] Fix yml anchors/aliases when converting yml-to-json Apparently they've started using Anchors and Aliases in yml files upstream (see e.g. uk.yml -> male_first_name -> first_name). Seems like jackson can't handle Anchors properly (see https://github.com/FasterXML/jackson-dataformats-text/issues/98) As a workaround, we use snakeyaml to read .yml files to Map objects, and then we use jackson to write them to .json files, since snakeyaml seems to support anchors just fine without needing to jump through hoops. --- buildSrc/build.gradle.kts | 3 ++- buildSrc/src/main/kotlin/yaml-to-json.gradle.kts | 14 +++++++++----- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/buildSrc/build.gradle.kts b/buildSrc/build.gradle.kts index ed402458d..46c357d46 100644 --- a/buildSrc/build.gradle.kts +++ b/buildSrc/build.gradle.kts @@ -16,5 +16,6 @@ dependencies { implementation("com.adarshr:gradle-test-logger-plugin:4.0.0") // used by yaml-to-json buildSrc plugin implementation("com.fasterxml.jackson.core:jackson-databind:2.15.3") - implementation("com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:2.15.3") + // use snakeyaml instead of jackson-dataformat-yaml to properly handle yaml anchors and write them as actual values to json + implementation("org.yaml:snakeyaml:2.2") } diff --git a/buildSrc/src/main/kotlin/yaml-to-json.gradle.kts b/buildSrc/src/main/kotlin/yaml-to-json.gradle.kts index 0e7e54cc8..e659fb5d2 100644 --- a/buildSrc/src/main/kotlin/yaml-to-json.gradle.kts +++ b/buildSrc/src/main/kotlin/yaml-to-json.gradle.kts @@ -1,6 +1,5 @@ import com.fasterxml.jackson.databind.ObjectMapper -import com.fasterxml.jackson.dataformat.yaml.YAMLFactory -import org.gradle.kotlin.dsl.create +import org.yaml.snakeyaml.Yaml interface Yaml2JsonPluginExtension { val input: Property @@ -9,7 +8,13 @@ interface Yaml2JsonPluginExtension { class Yaml2JsonPlugin : Plugin { val jsonMapper = ObjectMapper() - val yamlMapper = ObjectMapper(YAMLFactory()) + // https://github.com/FasterXML/jackson-dataformats-text/issues/98 + /* We use snakeyaml since it can handle Anchors and References (Aliases) in yml files, which jackson can't apparently. + * We still can use jackson to write to json, because at that time, + * the Map we created from yaml will contain proper values and not simply anchor names, + * like it happens when using jackson to read yaml. + */ + val yamlMapper = Yaml() override fun apply(p: Project) { val ext = p.extensions.create("yaml2jsonExt") @@ -50,8 +55,7 @@ class Yaml2JsonPlugin : Plugin { } private fun writeYamlToJson(src: File, dest: File) { - val map = yamlMapper.readValue(src.inputStream(), Map::class.java) + val map = yamlMapper.loadAs(src.inputStream(), Map::class.java) jsonMapper.writeValue(dest, map) } } -