Skip to content

Commit 8b8d5cc

Browse files
committed
Allow trailing whitespace document split marker
Refine `OriginTrackedPropertiesLoader` document split detection to be more lenient if there is trailing whitespace. Closes gh-23399
1 parent 90483d3 commit 8b8d5cc

File tree

2 files changed

+38
-8
lines changed

2 files changed

+38
-8
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/env/OriginTrackedPropertiesLoader.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,7 @@ class OriginTrackedPropertiesLoader {
5757
}
5858

5959
/**
60-
* Load {@code .properties} data and return a map of {@code String} ->
61-
* {@link OriginTrackedValue}.
60+
* Load {@code .properties} data and return a list of documents.
6261
* @return the loaded properties
6362
* @throws IOException on read error
6463
*/
@@ -79,7 +78,7 @@ List<Document> load(boolean expandLists) throws IOException {
7978
try (CharacterReader reader = new CharacterReader(this.resource)) {
8079
StringBuilder buffer = new StringBuilder();
8180
while (reader.read()) {
82-
if (reader.getCharacter() == '#') {
81+
if (reader.isPoundCharacter()) {
8382
if (isNewDocument(reader)) {
8483
if (!document.isEmpty()) {
8584
result.add(document);
@@ -150,12 +149,13 @@ private OriginTrackedValue loadValue(StringBuilder buffer, CharacterReader reade
150149
}
151150

152151
boolean isNewDocument(CharacterReader reader) throws IOException {
153-
boolean result = reader.isPoundCharacter();
152+
boolean result = reader.getLocation().getColumn() == 0 && reader.isPoundCharacter();
154153
result = result && readAndExpect(reader, reader::isHyphenCharacter);
155154
result = result && readAndExpect(reader, reader::isHyphenCharacter);
156155
result = result && readAndExpect(reader, reader::isHyphenCharacter);
157-
result = result && readAndExpect(reader, reader::isEndOfLine);
158-
return result;
156+
reader.read();
157+
reader.skipWhitespace();
158+
return result && reader.isEndOfLine();
159159
}
160160

161161
private boolean readAndExpect(CharacterReader reader, BooleanSupplier check) throws IOException {
@@ -198,7 +198,7 @@ boolean read(boolean wrappedLine) throws IOException {
198198
this.character = this.reader.read();
199199
this.columnNumber++;
200200
if (this.columnNumber == 0) {
201-
skipLeadingWhitespace();
201+
skipWhitespace();
202202
if (!wrappedLine) {
203203
if (this.character == '!') {
204204
skipComment();
@@ -215,7 +215,7 @@ else if (this.character == '\n') {
215215
return !isEndOfFile();
216216
}
217217

218-
private void skipLeadingWhitespace() throws IOException {
218+
private void skipWhitespace() throws IOException {
219219
while (isWhiteSpace()) {
220220
this.character = this.reader.read();
221221
this.columnNumber++;

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/env/OriginTrackedPropertiesLoaderTests.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.boot.env;
1818

19+
import java.io.IOException;
1920
import java.util.List;
2021
import java.util.Properties;
2122

@@ -25,6 +26,7 @@
2526
import org.springframework.boot.env.OriginTrackedPropertiesLoader.Document;
2627
import org.springframework.boot.origin.OriginTrackedValue;
2728
import org.springframework.boot.origin.TextResourceOrigin;
29+
import org.springframework.core.io.ByteArrayResource;
2830
import org.springframework.core.io.ClassPathResource;
2931
import org.springframework.core.io.support.PropertiesLoaderUtils;
3032

@@ -180,6 +182,34 @@ void getImmediateMultiline() {
180182
assertThat(getLocation(value)).isEqualTo("32:1");
181183
}
182184

185+
@Test
186+
void loadWhenMultiDocumentWithoutWhitespaceLoadsMultiDoc() throws IOException {
187+
String content = "a=a\n#---\nb=b";
188+
List<Document> loaded = new OriginTrackedPropertiesLoader(new ByteArrayResource(content.getBytes())).load();
189+
assertThat(loaded).hasSize(2);
190+
}
191+
192+
@Test
193+
void loadWhenMultiDocumentWithLeadingWhitespaceLoadsSingleDoc() throws IOException {
194+
String content = "a=a\n \t#---\nb=b";
195+
List<Document> loaded = new OriginTrackedPropertiesLoader(new ByteArrayResource(content.getBytes())).load();
196+
assertThat(loaded).hasSize(1);
197+
}
198+
199+
@Test
200+
void loadWhenMultiDocumentWithTrailingWhitespaceLoadsMultiDoc() throws IOException {
201+
String content = "a=a\n#--- \t \nb=b";
202+
List<Document> loaded = new OriginTrackedPropertiesLoader(new ByteArrayResource(content.getBytes())).load();
203+
assertThat(loaded).hasSize(2);
204+
}
205+
206+
@Test
207+
void loadWhenMultiDocumentWithTrailingCharsLoadsSingleDoc() throws IOException {
208+
String content = "a=a\n#--- \tcomment\nb=b";
209+
List<Document> loaded = new OriginTrackedPropertiesLoader(new ByteArrayResource(content.getBytes())).load();
210+
assertThat(loaded).hasSize(1);
211+
}
212+
183213
@Test
184214
void getPropertyWithWhitespaceAfterKey() {
185215
OriginTrackedValue value = getFromFirst("bar");

0 commit comments

Comments
 (0)