question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

YamlPropertiesFactoryBean has empty string returned when empty array value is present in the YAML document

See original GitHub issue

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.

Screen Shot 2020-04-08 at 1 18 57 PM

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”: “”

Screen Shot 2020-04-08 at 1 22 45 PM

Stack Overflow question: https://stackoverflow.com/questions/61096022/yamlpropertiesfactorybean-has-empty-string-returned-when-empty-array-value-is-pr

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
snicollcommented, Apr 20, 2020

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.

0reactions
snicollcommented, Apr 20, 2020

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Reading a map from yaml in Java getting null - Stack Overflow
I had the same problem with different generic types and I solved it by initializing the map member and remove the setter method,...
Read more >
24. Externalized Configuration - Spring
You can use properties files, YAML files, environment variables and command-line arguments to externalize configuration. Property values can be injected ...
Read more >
How to represent null values in YAML - Educative.io
Unknown or undefined values: When the value of a data element is unknown or undefined, null can be used as its value. Empty...
Read more >
Arrays | Dev Cheatsheets - Michael Currin
But, you can split an empty string. Or you can define an array in your frontmatter and then use that. Define an array...
Read more >
YamlProcessor (spring-beans 5.2.8.RELEASE API) - javadoc.io
Set the supported types that can be loaded from YAML documents. ... supportedTypes - the supported types, or an empty array to clear...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found