Skip to content

Commit b7b2bb6

Browse files
6mmLIUyuluo-yx
authored andcommitted
fix(core): add blank-text validation in TextDocumentParser (alibaba#2237)
Co-authored-by: shown <yuluo08290126@gmail.com>
1 parent aa4188d commit b7b2bb6

File tree

2 files changed

+13
-11
lines changed

2 files changed

+13
-11
lines changed

spring-ai-alibaba-core/src/main/java/com/alibaba/cloud/ai/document/TextDocumentParser.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import org.springframework.ai.document.Document;
1919
import org.springframework.util.Assert;
2020

21+
import java.io.IOException;
2122
import java.io.InputStream;
2223
import java.nio.charset.Charset;
2324
import java.nio.charset.StandardCharsets;
@@ -44,13 +45,17 @@ public TextDocumentParser(Charset charset) {
4445
@Override
4546
public List<Document> parse(InputStream inputStream) {
4647
try {
47-
String text = new String(inputStream.readAllBytes(), charset);
48+
// Read all text from the input stream and decode it using the specified
49+
// character set.
50+
String text = new String(inputStream.readAllBytes(), this.charset);
51+
// If the text is completely empty, report an illegal argument exception.
4852
if (text.isBlank()) {
49-
throw new Exception();
53+
throw new IllegalArgumentException("text must not be blank");
5054
}
5155
return Collections.singletonList(new Document(text));
5256
}
53-
catch (Exception e) {
57+
catch (IOException e) {
58+
// Convert any IO exception into a RuntimeException for propagation.
5459
throw new RuntimeException(e);
5560
}
5661
}

spring-ai-alibaba-core/src/test/java/com/alibaba/cloud/ai/document/TextDocumentParserTests.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,24 +62,21 @@ void testParseWithCustomCharset() {
6262

6363
@Test
6464
void testParseEmptyText() {
65-
// Test parsing empty text should throw exception
65+
// An empty string should trigger an IllegalArgumentException.
6666
String text = "";
6767
TextDocumentParser parser = new TextDocumentParser();
6868

69-
RuntimeException exception = assertThrows(RuntimeException.class, () -> parser.parse(toInputStream(text)));
70-
71-
assertThat(exception).hasRootCauseInstanceOf(Exception.class);
69+
assertThrows(IllegalArgumentException.class, () -> parser.parse(toInputStream(text)));
7270
}
7371

7472
@Test
7573
void testParseBlankText() {
76-
// Test parsing blank text (only whitespace) should throw exception
74+
// A blank string containing spaces or inserted characters should also throw an
75+
// IllegalArgumentException.
7776
String text = " \n\t ";
7877
TextDocumentParser parser = new TextDocumentParser();
7978

80-
RuntimeException exception = assertThrows(RuntimeException.class, () -> parser.parse(toInputStream(text)));
81-
82-
assertThat(exception).hasRootCauseInstanceOf(Exception.class);
79+
assertThrows(IllegalArgumentException.class, () -> parser.parse(toInputStream(text)));
8380
}
8481

8582
@Test

0 commit comments

Comments
 (0)