Skip to content

Commit da0c3d9

Browse files
committed
fix #1419
1 parent 6debe05 commit da0c3d9

File tree

2 files changed

+40
-19
lines changed

2 files changed

+40
-19
lines changed

powertools-idempotency/src/main/java/software/amazon/lambda/powertools/idempotency/persistence/BasePersistenceStore.java

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import java.security.NoSuchAlgorithmException;
2727
import java.time.Instant;
2828
import java.time.temporal.ChronoUnit;
29-
import java.util.Map;
3029
import java.util.Optional;
3130
import java.util.OptionalInt;
3231
import java.util.OptionalLong;
@@ -122,17 +121,17 @@ public void saveSuccess(JsonNode data, Object result, Instant now) {
122121
// missing idempotency key => non-idempotent transaction, we do not store the data, simply return
123122
return;
124123
}
125-
DataRecord record = new DataRecord(
124+
DataRecord dataRecord = new DataRecord(
126125
hashedIdempotencyKey.get(),
127126
DataRecord.Status.COMPLETED,
128127
getExpiryEpochSecond(now),
129128
responseJson,
130129
getHashedPayload(data)
131130
);
132131
LOG.debug("Function successfully executed. Saving record to persistence store with idempotency key: {}",
133-
record.getIdempotencyKey());
134-
updateRecord(record);
135-
saveToCache(record);
132+
dataRecord.getIdempotencyKey());
133+
updateRecord(dataRecord);
134+
saveToCache(dataRecord);
136135
} catch (JsonProcessingException e) {
137136
// TODO : throw ?
138137
throw new RuntimeException("Error while serializing the response", e);
@@ -164,16 +163,16 @@ public void saveInProgress(JsonNode data, Instant now, OptionalInt remainingTime
164163
OptionalLong.of(now.plus(remainingTimeInMs.getAsInt(), ChronoUnit.MILLIS).toEpochMilli());
165164
}
166165

167-
DataRecord record = new DataRecord(
166+
DataRecord dataRecord = new DataRecord(
168167
idempotencyKey,
169168
DataRecord.Status.INPROGRESS,
170169
getExpiryEpochSecond(now),
171170
null,
172171
getHashedPayload(data),
173172
inProgressExpirationMsTimestamp
174173
);
175-
LOG.debug("saving in progress record for idempotency key: {}", record.getIdempotencyKey());
176-
putRecord(record, now);
174+
LOG.debug("saving in progress record for idempotency key: {}", dataRecord.getIdempotencyKey());
175+
putRecord(dataRecord, now);
177176
}
178177

179178
/**
@@ -223,10 +222,10 @@ public DataRecord getRecord(JsonNode data, Instant now)
223222
return cachedRecord;
224223
}
225224

226-
DataRecord record = getRecord(idemPotencyKey);
227-
saveToCache(record);
228-
validatePayload(data, record);
229-
return record;
225+
DataRecord dataRecord = getRecord(idemPotencyKey);
226+
saveToCache(dataRecord);
227+
validatePayload(data, dataRecord);
228+
return dataRecord;
230229
}
231230

232231
/**
@@ -258,10 +257,10 @@ private Optional<String> getHashedIdempotencyKey(JsonNode data) {
258257

259258
private boolean isMissingIdemPotencyKey(JsonNode data) {
260259
if (data.isContainerNode()) {
261-
Stream<Map.Entry<String, JsonNode>> stream =
262-
StreamSupport.stream(Spliterators.spliteratorUnknownSize(data.fields(), Spliterator.ORDERED),
260+
Stream<JsonNode> stream =
261+
StreamSupport.stream(Spliterators.spliteratorUnknownSize(data.elements(), Spliterator.ORDERED),
263262
false);
264-
return stream.allMatch(e -> e.getValue().isNull());
263+
return stream.allMatch(JsonNode::isNull);
265264
}
266265
return data.isNull();
267266
}
@@ -378,10 +377,10 @@ private DataRecord retrieveFromCache(String idempotencyKey, Instant now) {
378377
return null;
379378
}
380379

381-
DataRecord record = cache.get(idempotencyKey);
382-
if (record != null) {
383-
if (!record.isExpired(now)) {
384-
return record;
380+
DataRecord dataRecord = cache.get(idempotencyKey);
381+
if (dataRecord != null) {
382+
if (!dataRecord.isExpired(now)) {
383+
return dataRecord;
385384
}
386385
LOG.debug("Removing expired local cache record for idempotency key: {}", idempotencyKey);
387386
deleteFromCache(idempotencyKey);

powertools-idempotency/src/test/java/software/amazon/lambda/powertools/idempotency/internal/IdempotencyAspectTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,28 @@ public void idempotencyOnSubMethodAnnotated_keyJMESPath_shouldPutInStoreWithKey(
378378
"testFunction.createBasket#a1d0c6e83f027327d8461063f4ac58a6");
379379
}
380380

381+
@Test
382+
public void idempotencyOnSubMethodAnnotated_keyJMESPathArray_shouldPutInStoreWithKey() {
383+
BasePersistenceStore persistenceStore = spy(BasePersistenceStore.class);
384+
385+
Idempotency.config()
386+
.withPersistenceStore(persistenceStore)
387+
.withConfig(IdempotencyConfig.builder().withEventKeyJMESPath("[id,name]").build())
388+
.configure();
389+
390+
// WHEN
391+
IdempotencyInternalFunctionInternalKey function = new IdempotencyInternalFunctionInternalKey();
392+
Product p = new Product(42, "fake product", 12);
393+
function.handleRequest(p, context);
394+
395+
// THEN
396+
ArgumentCaptor<DataRecord> recordCaptor = ArgumentCaptor.forClass(DataRecord.class);
397+
verify(persistenceStore).putRecord(recordCaptor.capture(), any());
398+
// eec7cd392d9e3bb20deb2c9676697c3c = MD5([42,"fake product"])
399+
assertThat(recordCaptor.getValue().getIdempotencyKey()).isEqualTo(
400+
"testFunction.createBasket#eec7cd392d9e3bb20deb2c9676697c3c");
401+
}
402+
381403
@Test
382404
public void idempotencyOnSubMethodNotAnnotated_shouldThrowException() {
383405
Idempotency.config()

0 commit comments

Comments
 (0)