This is related to #21310.
I'm using the latest spring boot version 2.2.6 and latest spring cloud config version 2.2.2.
I use GitHub as source of truth for fetching my configurations from base YAML file.
Check the property cards: [""] or just cards: [] in line number 4.
The response I get from my service for fetching the configuration is as below snapshot for a default profile API to spring cloud config server.
Please find below snapshot with response : "default.oihs.cards": ""
Stack Overflow question: https://stackoverflow.com/questions/61096022/yamlpropertiesfactorybean-has-empty-string-returned-when-empty-array-value-is-pr
Comment From: sbrannen
As per #21310, an empty array in a YAML file gets converted to an empty String when using YamlPropertiesFactoryBean.
That's be design, since values in a Properties object must be of type String.
As stated by @snicoll, conversion of an empty array to an empty string "corresponds to an empty comma-separated list of entries in the Properties format."
So what exactly does not work for you?
Comment From: dhivyaarumugam
@sbrannen : I'm retrieving the git yml properties, store it in cache and provide the configuration properties. For spring cloud config version : 1.2.1.RELEASE, nothing was impacted since nothing was returned in the response for an empty array in gitHub. Now with Latest spring cloud config version of usage, impact is there since I expect a empty array object but I get a String Object.
DataType mismatch is coming up.
Can the framework be fixed to return the object as -> cards: "[ ]" instead of cards: "" ?
For clarity to show empty String and empty Array get same o/p:
Git hub entries
Response from spring cloud config profile API:
Comment From: dhivyaarumugam
@sbrannen : more details on code:
https://github.com/spring-projects/spring-framework/commit/e51330e905473d2f193f667dbf4b93207a6454d3 - this is the commit which creates the [] -> “” package org.springframework.beans.factory.config; yamlProcessor Class
Comment From: snicoll
Can the framework be fixed to return the object as -> cards: "[ ]" instead of cards: "" ?
No. The properties format has no notion of an array.
DataType mismatch is coming up.
If you have a small sample (that does not involve spring cloud config server) that shows how a data type mismatch is thrown, we can have a look to your use case. Please share that as a small project we can run ourselves (a zip or a link to a github repo).
Comment From: dhivyaarumugam
@snicoll : Please find the sample code in https://github.com/dhivyaarumugam/yaml-processing-validator. Follow the readMe to have the local setup : https://github.com/dhivyaarumugam/yaml-processing-validator/blob/master/README.md
Comment From: dhivyaarumugam
@snicoll : A PR i have raised from my fork : https://github.com/dhivyaarumugam/spring-framework/commit/3536a21f8fbfc06a58c5f57d1cc02cc65ee7b75e
Comment From: snicoll
Thanks for the sample. As I've indicated previously, the properties format has no notion of an array so you can't expect that to be available in any shape or form in the returned Map.
Your sample is just raw checking the Map and it doesn't demonstrate the DataType mismatch you've described (all values are of type String). If you need to bind this to an Array, then whatever binder you have should account for the fact that an empty String bound to an Array leads to an empty array. The Spring Boot binder does that for you so perhaps you should be using that?
I've added this:
@Component
@ConfigurationProperties(prefix = "test")
public class TestProperties {
private String[] emptyArray;
public String[] getEmptyArray() {
return this.emptyArray;
}
public void setEmptyArray(String[] emptyArray) {
this.emptyArray = emptyArray;
}
}
and the following config:
test:
emptyArray: []
and when I inject TestProperties, getEmptyArray() returns an empty array. I am going to close this now as the sample works as designed.
Comment From: dhivyaarumugam
@snicoll : In Spring cloud config, when propertySource is created it uses the map from yaml processor directly and doesn't have spring Binder involved in it.
Yes Spring binder can help in case of a POJO not in our case.
Comment From: snicoll
In Spring cloud config, when propertySource is created it uses the map from yaml processor directly
That makes total sense since the goal is to output a "properties view" of the configuration. There's nothing Spring Cloud Config should do about that.
Yes Spring binder can help in case of a POJO not in our case.
You haven't shared what your case is in your sample so I can't say anything about that. I think this issue has run its course in the core framework though and I hope the rationale I've shared explains why we're not going to change this.