Skip to content

Commit

Permalink
Fix REST error 500 when wrong field name is used
Browse files Browse the repository at this point in the history
Instead of producing a NPE, we now catch the problem (using the wrong field name `file` and return a proper error message.

Closes #1748.
  • Loading branch information
dadoonet committed Jan 16, 2024
1 parent fd2a436 commit 5169bac
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ protected interface HitChecker {
void check(ESSearchHit hit);
}

private static final Map<String, Object> debugOption = new HashMap<>();
protected static final Map<String, Object> debugOption = new HashMap<>();

static {
debugOption.put("debug", true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,17 @@
import fr.pilato.elasticsearch.crawler.fs.settings.FsSettings;
import fr.pilato.elasticsearch.crawler.fs.settings.Rest;
import fr.pilato.elasticsearch.crawler.fs.test.integration.AbstractRestITCase;
import jakarta.ws.rs.core.MediaType;
import org.glassfish.jersey.media.multipart.FormDataMultiPart;
import org.glassfish.jersey.media.multipart.file.FileDataBodyPart;
import org.junit.Test;

import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Predicate;

Expand Down Expand Up @@ -280,4 +285,26 @@ public void testDocumentWithExternalTags() throws Exception {
expectThrows(PathNotFoundException.class, () -> JsonPath.read(hit.getSource(), "$.external"));
});
}

@Test
public void testUploadUsingWrongFieldName() {
Path from = rootTmpDir.resolve("resources").resolve("documents").resolve("test.txt");
if (Files.notExists(from)) {
staticLogger.error("file [{}] should exist before we start tests", from);
throw new RuntimeException(from + " doesn't seem to exist. Check your JUnit tests.");
}

Map<String, Object> params = new HashMap<>();
FileDataBodyPart filePart = new FileDataBodyPart("anotherfieldname", from.toFile(), MediaType.APPLICATION_OCTET_STREAM_TYPE);
FormDataMultiPart mp = new FormDataMultiPart();
mp.bodyPart(filePart);
if (staticLogger.isDebugEnabled()) {
staticLogger.debug("Rest response: {}", post(target, "/_document", mp, String.class, debugOption));
}

UploadResponse response = post(target, "/_document", mp, UploadResponse.class, params);

assertThat(response.isOk(), is(false));
assertThat(response.getMessage(), containsString("No file has been sent or you are not using [file] as the field name."));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,13 @@ private UploadResponse uploadToDocumentService(
// Create the Doc object
Doc doc = new Doc();

if (d == null) {
UploadResponse response = new UploadResponse();
response.setOk(false);
response.setMessage("No file has been sent or you are not using [file] as the field name.");
return response;
}

String filename = new String(d.getFileName().getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
long filesize = d.getSize();

Expand Down

0 comments on commit 5169bac

Please sign in to comment.