Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,28 @@ private Map<String, Object> convertToProperties(Environment profiles) {

private void postProcessProperties(Map<String, Object> propertiesMap) {
propertiesMap.keySet().removeIf(key -> key.equals("spring.profiles"));
restoreEmptyCollections(propertiesMap);
}

/**
* Flattened YAML properties represent empty collections as an empty string (see
* {@code YamlProcessor#buildFlattenedMap}). When rebuilding nested YAML/JSON, restore
* those markers to empty lists unless indexed entries exist for the same prefix.
*/
private void restoreEmptyCollections(Map<String, Object> propertiesMap) {
Set<String> arrayPrefixes = new HashSet<>();
for (String key : propertiesMap.keySet()) {
int indexOfBracket = key.indexOf('[');
if (indexOfBracket > 0) {
arrayPrefixes.add(key.substring(0, indexOfBracket));
}
}
for (Entry<String, Object> entry : propertiesMap.entrySet()) {
String key = entry.getKey();
if (!key.contains("[") && "".equals(entry.getValue()) && !arrayPrefixes.contains(key)) {
entry.setValue(Collections.emptyList());
}
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,26 @@ public void arrayInYaml() throws Exception {
assertThat(yaml).isEqualTo("a:\n b:\n - c\n - d\n");
}

@Test
public void emptyArrayInJson() throws Exception {
Map<String, Object> map = new LinkedHashMap<>();
map.put("network.urls", "");
this.environment.add(new PropertySource("one", map));
when(this.repository.findOne("foo", "bar", null, false)).thenReturn(this.environment);
String json = this.controller.jsonProperties("foo", "bar", false).getBody();
JSONAssert.assertEquals("{\"network\":{\"urls\":[]}}", json, JSONCompareMode.STRICT);
}

@Test
public void emptyArrayInYaml() throws Exception {
Map<String, Object> map = new LinkedHashMap<>();
map.put("network.urls", "");
this.environment.add(new PropertySource("one", map));
when(this.repository.findOne("foo", "bar", null, false)).thenReturn(this.environment);
String yaml = this.controller.yaml("foo", "bar", false).getBody();
assertThat(yaml).isEqualTo("network:\n urls: []\n");
}

@Test
public void yamlWithBrackets() throws Exception {
Map<String, Object> map = new LinkedHashMap<String, Object>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,16 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.skyscreamer.jsonassert.JSONAssert;
import org.skyscreamer.jsonassert.JSONCompareMode;

import org.springframework.boot.WebApplicationType;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.config.environment.Environment;
import org.springframework.cloud.config.environment.PropertySource;
import org.springframework.cloud.config.server.environment.SearchPathLocator.Locations;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.http.ResponseEntity;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
Expand Down Expand Up @@ -127,6 +130,26 @@ public void prefixedMultiDocProperties() {
.isNotEqualTo(environment.getPropertySources().get(1).getName());
}

@Test
public void emptyYamlArrayInJsonOutput() throws Exception {
this.repository.setSearchLocations("classpath:/test/empty-array");
Environment environment = this.repository.findOne("application", "default", "master");
EnvironmentRepository repository = new EnvironmentRepository() {
@Override
public Environment findOne(String application, String profile, String label) {
return environment;
}

@Override
public Environment findOne(String application, String profile, String label, boolean includeOrigin) {
return environment;
}
};
EnvironmentController controller = new EnvironmentController(repository);
ResponseEntity<String> json = controller.labelledJsonProperties("application", "default", "master", false);
JSONAssert.assertEquals("{\"network\":{\"urls\":[]}}", json.getBody(), JSONCompareMode.STRICT);
}

@Test
public void prefixedWithFile() {
this.repository.setSearchLocations("file:./src/test/resources/test");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
network:
urls: []