Skip to content

Commit

Permalink
Support loading YAML files containing anchors/references
Browse files Browse the repository at this point in the history
This allows anchors and references to be used in all configuration
files. The implementation is a workaround for jackson-dataformats-text
bug #98:
FasterXML/jackson-dataformats-text#98
  • Loading branch information
michel-kraemer committed Mar 10, 2021
1 parent 6159198 commit 970af89
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/main/kotlin/db/AbstractFileRegistry.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import io.vertx.core.Vertx
import io.vertx.kotlin.core.executeBlockingAwait
import io.vertx.kotlin.core.file.readFileAwait
import org.apache.sshd.common.util.io.DirectoryScanner
import org.yaml.snakeyaml.Yaml
import java.io.File
import java.nio.file.Path

Expand Down Expand Up @@ -51,7 +52,12 @@ abstract class AbstractFileRegistry {
if (file.toLowerCase().endsWith(".json")) {
JsonUtils.mapper.readValue(content, tr)
} else {
YamlUtils.mapper.readValue(content, tr)
// Use SnakeYML to parse file and then Jackson to convert it to an
// object. This is a workaround for jackson-dataformats-text bug #98:
// https://github.com/FasterXML/jackson-dataformats-text/issues/98
val yaml = Yaml()
val l = yaml.load<List<Any>>(content)
YamlUtils.mapper.convertValue(l, tr)
}
}
}
Expand Down
40 changes: 40 additions & 0 deletions src/test/kotlin/db/MetadataRegistryTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package db

import io.vertx.core.Vertx
import io.vertx.junit5.VertxExtension
import io.vertx.junit5.VertxTestContext
import io.vertx.kotlin.coroutines.dispatcher
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith

/**
* Tests for [MetadataRegistry]
* @author Michel Kraemer
*/
@ExtendWith(VertxExtension::class)
class MetadataRegistryTest {
/**
* Test if a YAML file with references can be loaded
*/
@Test
fun yamlWithReferences(vertx: Vertx, ctx: VertxTestContext) {
val registry = FileMetadataRegistry(listOf("src/test/resources/db/services_with_references.yaml"), vertx)

GlobalScope.launch(vertx.dispatcher()) {
val services = registry.findServices()
assertThat(services).hasSize(2)
val s1 = services[0]
val s2 = services[1]
assertThat(s1.id).isEqualTo("service1")
assertThat(s2.id).isEqualTo("service2")
assertThat(s1.path).isEqualTo("service.sh")
assertThat(s2.path).isEqualTo(s1.path)
assertThat(s2.runtime).isEqualTo(s1.runtime)
assertThat(s2.parameters).isEmpty()
ctx.completeNow()
}
}
}
13 changes: 13 additions & 0 deletions src/test/resources/db/services_with_references.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
- id: service1
name: Service 1
description: My first service
path: &mypath "service.sh"
runtime: &myruntime other
parameters: &myparams []

- id: service2
name: Service 2
description: My second service
path: *mypath
runtime: *myruntime
parameters: *myparams

0 comments on commit 970af89

Please sign in to comment.