3535import java .time .Instant ;
3636import java .time .temporal .ChronoUnit ;
3737import java .util .Map ;
38- import java .util .OptionalLong ;
38+ import java .util .Optional ;
3939import java .util .OptionalInt ;
40- import java .util .stream .Stream ;
41- import java .util .Spliterators ;
40+ import java .util .OptionalLong ;
4241import java .util .Spliterator ;
42+ import java .util .Spliterators ;
43+ import java .util .stream .Stream ;
4344import java .util .stream .StreamSupport ;
4445
4546import static software .amazon .lambda .powertools .core .internal .LambdaConstants .LAMBDA_FUNCTION_NAME_ENV ;
@@ -117,8 +118,13 @@ public void saveSuccess(JsonNode data, Object result, Instant now) {
117118 } else {
118119 responseJson = writer .writeValueAsString (result );
119120 }
121+ Optional <String > hashedIdempotencyKey = getHashedIdempotencyKey (data );
122+ if (!hashedIdempotencyKey .isPresent ()) {
123+ // missing idempotency key => non-idempotent transaction, we do not store the data, simply return
124+ return ;
125+ }
120126 DataRecord record = new DataRecord (
121- getHashedIdempotencyKey ( data ),
127+ hashedIdempotencyKey . get ( ),
122128 DataRecord .Status .COMPLETED ,
123129 getExpiryEpochSecond (now ),
124130 responseJson ,
@@ -140,8 +146,13 @@ public void saveSuccess(JsonNode data, Object result, Instant now) {
140146 * @param now
141147 */
142148 public void saveInProgress (JsonNode data , Instant now , OptionalInt remainingTimeInMs ) throws IdempotencyItemAlreadyExistsException {
143- String idempotencyKey = getHashedIdempotencyKey (data );
149+ Optional <String > hashedIdempotencyKey = getHashedIdempotencyKey (data );
150+ if (!hashedIdempotencyKey .isPresent ()) {
151+ // missing idempotency key => non-idempotent transaction, we do not store the data, simply return
152+ return ;
153+ }
144154
155+ String idempotencyKey = hashedIdempotencyKey .get ();
145156 if (retrieveFromCache (idempotencyKey , now ) != null ) {
146157 throw new IdempotencyItemAlreadyExistsException ();
147158 }
@@ -170,8 +181,13 @@ public void saveInProgress(JsonNode data, Instant now, OptionalInt remainingTime
170181 * @param throwable The throwable thrown by the function
171182 */
172183 public void deleteRecord (JsonNode data , Throwable throwable ) {
173- String idemPotencyKey = getHashedIdempotencyKey (data );
184+ Optional <String > hashedIdempotencyKey = getHashedIdempotencyKey (data );
185+ if (!hashedIdempotencyKey .isPresent ()) {
186+ // missing idempotency key => non-idempotent transaction, we do not delete the data, simply return
187+ return ;
188+ }
174189
190+ String idemPotencyKey = hashedIdempotencyKey .get ();
175191 LOG .debug ("Function raised an exception {}. " +
176192 "Clearing in progress record in persistence store for idempotency key: {}" ,
177193 throwable .getClass (),
@@ -190,8 +206,13 @@ public void deleteRecord(JsonNode data, Throwable throwable) {
190206 * @throws IdempotencyItemNotFoundException Exception thrown if no record exists in persistence store with the idempotency key
191207 */
192208 public DataRecord getRecord (JsonNode data , Instant now ) throws IdempotencyValidationException , IdempotencyItemNotFoundException {
193- String idemPotencyKey = getHashedIdempotencyKey (data );
209+ Optional <String > hashedIdempotencyKey = getHashedIdempotencyKey (data );
210+ if (!hashedIdempotencyKey .isPresent ()) {
211+ // missing idempotency key => non-idempotent transaction, we do not get the data, simply return nothing
212+ return null ;
213+ }
194214
215+ String idemPotencyKey = hashedIdempotencyKey .get ();
195216 DataRecord cachedRecord = retrieveFromCache (idemPotencyKey , now );
196217 if (cachedRecord != null ) {
197218 LOG .debug ("Idempotency record found in cache with idempotency key: {}" , idemPotencyKey );
@@ -211,7 +232,7 @@ public DataRecord getRecord(JsonNode data, Instant now) throws IdempotencyValida
211232 * @param data incoming data
212233 * @return Hashed representation of the data extracted by the jmespath expression
213234 */
214- private String getHashedIdempotencyKey (JsonNode data ) {
235+ private Optional < String > getHashedIdempotencyKey (JsonNode data ) {
215236 JsonNode node = data ;
216237
217238 if (eventKeyJMESPath != null ) {
@@ -221,13 +242,15 @@ private String getHashedIdempotencyKey(JsonNode data) {
221242 if (isMissingIdemPotencyKey (node )) {
222243 if (throwOnNoIdempotencyKey ) {
223244 throw new IdempotencyKeyException ("No data found to create a hashed idempotency key" );
245+ } else {
246+ LOG .warn ("No data found to create a hashed idempotency key. JMESPath: {}" , eventKeyJMESPath );
247+ return Optional .empty ();
224248 }
225- LOG .warn ("No data found to create a hashed idempotency key. JMESPath: {}" , eventKeyJMESPath );
226249 }
227250
228251 String hash = generateHash (node );
229252 hash = functionName + "#" + hash ;
230- return hash ;
253+ return Optional . of ( hash ) ;
231254 }
232255
233256 private boolean isMissingIdemPotencyKey (JsonNode data ) {
0 commit comments