Skip to content

Commit 6e883c7

Browse files
authored
fix: handling empty yaml config file with a comment only (#3301)
1 parent 2da47e4 commit 6e883c7

2 files changed

Lines changed: 62 additions & 2 deletions

File tree

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,16 @@ private static Map<String, Object> load(Path path) {
100100
return Map.of();
101101
}
102102

103-
try (InputStream in = Files.newInputStream(path)) {
104-
Map<String, Object> result = MAPPER.readValue(in, Map.class);
103+
try (InputStream in = Files.newInputStream(path);
104+
com.fasterxml.jackson.core.JsonParser parser = MAPPER.getFactory().createParser(in)) {
105+
if (parser.nextToken() == null) {
106+
log.debug(
107+
"YAML file contains no mappings (possibly empty or comments only). "
108+
+ "Returning empty config. Path: {}",
109+
path);
110+
return Map.of();
111+
}
112+
Map<String, Object> result = MAPPER.readValue(parser, Map.class);
105113
return result != null ? result : Map.of();
106114
} catch (IOException e) {
107115
throw new UncheckedIOException("Failed to load config YAML from " + path, e);

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

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package io.javaoperatorsdk.operator.config.loader.provider;
1717

1818
import java.io.IOException;
19+
import java.io.UncheckedIOException;
1920
import java.nio.file.Files;
2021
import java.nio.file.Path;
2122
import java.time.Duration;
@@ -26,6 +27,7 @@
2627
import org.junit.jupiter.api.io.TempDir;
2728

2829
import static org.assertj.core.api.Assertions.assertThat;
30+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
2931
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
3032

3133
class YamlConfigProviderTest {
@@ -132,10 +134,60 @@ void loadsFromFile(@TempDir Path dir) throws IOException {
132134
assertThat(provider.getValue("josdk.test.integer", Integer.class)).hasValue(7);
133135
}
134136

137+
@Test
138+
void emptyFile(@TempDir Path dir) throws IOException {
139+
Path file = dir.resolve("empty.yaml");
140+
Files.writeString(file, "");
141+
142+
var provider = new YamlConfigProvider(file);
143+
assertThat(provider.getValue("any.key", String.class)).isEmpty();
144+
}
145+
146+
@Test
147+
void fileWithCommentOnly(@TempDir Path dir) throws IOException {
148+
Path file = dir.resolve("empty.yaml");
149+
Files.writeString(
150+
file,
151+
"""
152+
# sample comment
153+
""");
154+
var provider = new YamlConfigProvider(file);
155+
assertThat(provider.getValue("any.key", String.class)).isEmpty();
156+
}
157+
135158
@Test
136159
void returnsEmptyForNonExistingFile(@TempDir Path dir) {
137160
Path missing = dir.resolve("does-not-exist.yaml");
138161
var provider = new YamlConfigProvider(missing);
139162
assertThat(provider.getValue("any.key", String.class)).isEmpty();
140163
}
164+
165+
@Test
166+
void throwsForMalformedYaml(@TempDir Path dir) throws IOException {
167+
Path file = dir.resolve("bad.yaml");
168+
// YAML list where a map is expected
169+
Files.writeString(
170+
file,
171+
"""
172+
- item1
173+
- item2
174+
""");
175+
assertThatExceptionOfType(UncheckedIOException.class)
176+
.isThrownBy(() -> new YamlConfigProvider(file));
177+
}
178+
179+
@Test
180+
void commentWithProperDocument(@TempDir Path dir) throws IOException {
181+
Path file = dir.resolve("filewithcomment.yaml");
182+
// YAML comment followed by a proper mapping document
183+
Files.writeString(
184+
file,
185+
"""
186+
# comment
187+
key:
188+
subkey: val
189+
""");
190+
var provider = new YamlConfigProvider(file);
191+
assertThat(provider.getValue("key.subkey", String.class)).contains("val");
192+
}
141193
}

0 commit comments

Comments
 (0)