Skip to content
Snippets Groups Projects
Commit 36a57bea authored by Benoit Lavenier's avatar Benoit Lavenier
Browse files

[fix] when updating document, avoid NullPointerException when no time field or no document

parent 5755e79c
No related branches found
No related tags found
No related merge requests found
......@@ -62,6 +62,8 @@ public interface Duniter4jClient extends Bean, Client {
Map<String, Object> getMandatoryFieldsById(String index, String type, String docId, String... fieldNames);
<T> T getMandatoryTypedFieldById(String index, String type, String docId, String fieldName);
String indexDocumentFromJson(String index, String type, String json);
void updateDocumentFromJson(String index, String type, String id, String json);
......
......@@ -325,6 +325,15 @@ public class Duniter4jClientImpl implements Duniter4jClient {
return (T)getFieldById(index, type, docId, fieldName);
}
@Override
public <T> T getMandatoryTypedFieldById(String index, String type, String docId, String fieldName) {
Object result = getFieldById(index, type, docId, fieldName);
if (result == null) {
throw new NotFoundException(String.format("Document [%s/%s/%s] missing value for mandatory field [%s].", index, type, docId, fieldName));
}
return (T)result;
}
/**
* Retrieve a document by id (safe mode)
* @param docId
......
......@@ -50,7 +50,7 @@ public abstract class AbstractRestPostUpdateAction extends BaseRestHandler {
String typeName,
JsonUpdater updater) {
super(settings, controller, client);
log = Loggers.getLogger("duniter.rest" + indexName, settings, String.format("[%s]", indexName));
log = Loggers.getLogger("duniter.rest." + indexName, settings, String.format("[%s]", indexName));
controller.registerHandler(POST,
String.format("/%s/%s/{id}/_update", indexName, typeName),
this);
......
......@@ -170,7 +170,7 @@ public abstract class AbstractService implements Bean {
protected void verifyTimeForUpdate(String index, String type, String id, JsonNode actualObj, boolean allowOldDocuments, String timeFieldName) {
// Check time has been increase - fix #27
int actualTime = getMandatoryField(actualObj, timeFieldName).asInt();
int existingTime = client.getTypedFieldById(index, type, id, timeFieldName);
int existingTime = client.getMandatoryTypedFieldById(index, type, id, timeFieldName);
if (actualTime <= existingTime) {
throw new InvalidTimeException(String.format("Invalid '%s' value: can not be less or equal to the previous value.", timeFieldName, timeFieldName));
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment