Skip to content

Commit 62101d0

Browse files
committed
Fix NPE in source metadata of TextReader
1 parent e29d38d commit 62101d0

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

spring-ai-core/src/main/java/org/springframework/ai/reader/TextReader.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,11 @@ public class TextReader implements DocumentReader {
4545
private final Resource resource;
4646

4747
/**
48-
* @return Character set to be used when loading data from the
48+
* Character set to be used when loading data from the
4949
*/
5050
private Charset charset = StandardCharsets.UTF_8;
5151

52-
private Map<String, Object> customMetadata = new HashMap<>();
52+
private final Map<String, Object> customMetadata = new HashMap<>();
5353

5454
public TextReader(String resourceUrl) {
5555
this(new DefaultResourceLoader().getResource(resourceUrl));
@@ -85,7 +85,8 @@ public List<Document> get() {
8585

8686
// Inject source information as a metadata.
8787
this.customMetadata.put(CHARSET_METADATA, this.charset.name());
88-
this.customMetadata.put(SOURCE_METADATA, this.resource.getFilename());
88+
this.customMetadata.put(SOURCE_METADATA, this.resource.getFilename() != null ? this.resource.getFilename()
89+
: Objects.toIdentityString(this.resource));
8990

9091
return List.of(new Document(document, this.customMetadata));
9192

0 commit comments

Comments
 (0)