Skip to content

Commit 67a70eb

Browse files
committed
fix handling empty yaml config file with a comment only
Signed-off-by: Attila Mészáros <a_meszaros@apple.com>
1 parent 21fd3b8 commit 67a70eb

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

operator-framework/src/main/java/io/javaoperatorsdk/operator/config/loader/provider/YamlConfigProvider.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,9 @@ private static Map<String, Object> load(Path path) {
103103
try (InputStream in = Files.newInputStream(path)) {
104104
Map<String, Object> result = MAPPER.readValue(in, Map.class);
105105
return result != null ? result : Map.of();
106+
} catch (com.fasterxml.jackson.databind.exc.MismatchedInputException e) {
107+
log.warn("{} contains no configuration data", path);
108+
return Map.of();
106109
} catch (IOException e) {
107110
throw new UncheckedIOException("Failed to load config YAML from " + path, e);
108111
}

operator-framework/src/test/java/io/javaoperatorsdk/operator/config/loader/provider/YamlConfigProviderTest.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,27 @@ void loadsFromFile(@TempDir Path dir) throws IOException {
132132
assertThat(provider.getValue("josdk.test.integer", Integer.class)).hasValue(7);
133133
}
134134

135+
@Test
136+
void returnsEmptyForEmptyFile(@TempDir Path dir) throws IOException {
137+
Path file = dir.resolve("empty.yaml");
138+
Files.writeString(file, "");
139+
140+
var provider = new YamlConfigProvider(file);
141+
assertThat(provider.getValue("any.key", String.class)).isEmpty();
142+
}
143+
144+
@Test
145+
void fileWithCommentOnly(@TempDir Path dir) throws IOException {
146+
Path file = dir.resolve("empty.yaml");
147+
Files.writeString(
148+
file,
149+
"""
150+
# sample comment
151+
""");
152+
var provider = new YamlConfigProvider(file);
153+
assertThat(provider.getValue("any.key", String.class)).isEmpty();
154+
}
155+
135156
@Test
136157
void returnsEmptyForNonExistingFile(@TempDir Path dir) {
137158
Path missing = dir.resolve("does-not-exist.yaml");

0 commit comments

Comments
 (0)