I am using Spring Boot 3.2 with Java 21, along with SnakeYAML 2.2.
I am trying to load yaml file sources.yaml. This is how my YAML looks like:
sources:
"S&P":
cron: '0 30 1 * * SUN-SAT'
calculateCA: 'true'
modifyOhlc: 'true'
And this is how SourcesConfig class looks like:
@Configuration
@EnableConfigurationProperties
@ConfigurationProperties
@Slf4j
@PropertySource(value = "file:config/sources.yml", factory = YamlPropertySourceFactory.class)
public class SourcesConfig2 {
@Getter
@Setter
private Map<String, MetaData> sources;
@Data
public static class MetaData {
private String cron;
private boolean calculateCA;
private boolean modifyOhlc;
private String lastUpdatedTime;
private ZoneOffset gmtOffset;
}
}
public class YamlPropertySourceFactory implements PropertySourceFactory {
@Override
public PropertySource<?> createPropertySource(String name, EncodedResource encodedResource) {
YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
factory.setResources(encodedResource.getResource());
Properties properties = factory.getObject();
String fileName = encodedResource.getResource().getFilename();
Objects.requireNonNull(fileName);
Objects.requireNonNull(properties);
return new PropertiesPropertySource(fileName, properties);
}
}
I have removed other methods for brevity. At initial loadup, I have noticed an error popping up and went to debug. S&P is loaded as SP.
While debugging I noticed that the file is correctly loading. Something changed after its loadup and I cannot pinpoint what went wrong exactly.
What is wrong here? I have tried with other combinations like 'S$P', 'S@P' etc. and for some reason any special character is missed.
I believe you are running into this issue. You need to surround the S&P
config/map key with square brackets as suggested here, so that the key becomes "[S&P]"
from "S&P"