From da4008f39f6452f7f514c7db44b711c6e6f14ecf Mon Sep 17 00:00:00 2001 From: musketyr Date: Tue, 22 Oct 2024 15:02:35 +0200 Subject: [PATCH] Do not try to convert null results from DynamoDB async --- .../AsyncDynamoDbServiceIntroduction.java | 48 +++++++++++++------ 1 file changed, 34 insertions(+), 14 deletions(-) diff --git a/subprojects/micronaut-amazon-awssdk-dynamodb/src/main/java/com/agorapulse/micronaut/amazon/awssdk/dynamodb/AsyncDynamoDbServiceIntroduction.java b/subprojects/micronaut-amazon-awssdk-dynamodb/src/main/java/com/agorapulse/micronaut/amazon/awssdk/dynamodb/AsyncDynamoDbServiceIntroduction.java index 7437b60fe..181a4a480 100644 --- a/subprojects/micronaut-amazon-awssdk-dynamodb/src/main/java/com/agorapulse/micronaut/amazon/awssdk/dynamodb/AsyncDynamoDbServiceIntroduction.java +++ b/subprojects/micronaut-amazon-awssdk-dynamodb/src/main/java/com/agorapulse/micronaut/amazon/awssdk/dynamodb/AsyncDynamoDbServiceIntroduction.java @@ -70,6 +70,22 @@ public AsyncDynamoDbServiceIntroduction( this.conversionService = conversionService; } + @Override + public Object doIntercept(MethodInvocationContext context, Class type, String tableName) { + AsyncDynamoDbService service = provider.findOrCreate(tableName, type); + + try { + return doIntercept(context, service); + } catch (ResourceNotFoundException ignored) { + return unwrapIfRequired(Flux.from(service.createTable()).map(t -> doIntercept(context, service)), context.getReturnType().getType()); + } + } + + private static void logTypeConversionFailure(Class type, Object result) { + String message = "Cannot convert value %s to type %s".formatted(result, type); + LOGGER.warn(message, result, type, new IllegalArgumentException(message)); + } + @SuppressWarnings("unchecked") private Publisher toPublisher(Class type, Argument itemArgument, Map> params) { Object item = params.get(itemArgument.getName()).getValue(); @@ -92,17 +108,6 @@ private Publisher toPublisher(Class type, Argument itemArgument, Ma return Flux.just((T) item); } - @Override - public Object doIntercept(MethodInvocationContext context, Class type, String tableName) { - AsyncDynamoDbService service = provider.findOrCreate(tableName, type); - - try { - return doIntercept(context, service); - } catch (ResourceNotFoundException ignored) { - return unwrapIfRequired(Flux.from(service.createTable()).map(t -> doIntercept(context, service)), context.getReturnType().getType()); - } - } - private Object doIntercept(MethodInvocationContext context, AsyncDynamoDbService service) { String methodName = context.getMethodName(); if (methodName.startsWith("save")) { @@ -205,14 +210,24 @@ private Object unwrapIfRequired(Publisher publisher, Class type) { if (Number.class.isAssignableFrom(type) || type.isPrimitive() && !boolean.class.isAssignableFrom(type)) { if (Publishers.isSingle(publisher.getClass())) { Object result = Mono.from(publisher).block(); + + if (result == null) { + return 0; + } + return conversionService.convert(result, type).orElseGet(() -> { - LOGGER.warn("Cannot convert value {} to type {}", result, type); + logTypeConversionFailure(type, result); return 0; }); } Long count = Flux.from(publisher).count().block(); + + if (count == null) { + return 0; + } + return conversionService.convert(count, type).orElseGet(() -> { - LOGGER.warn("Cannot convert value {} to type {}", count, type); + logTypeConversionFailure(type, count); return 0; }); } @@ -222,8 +237,13 @@ private Object unwrapIfRequired(Publisher publisher, Class type) { } Object value = Mono.from(publisher).block(); + + if (value == null) { + return null; + } + return conversionService.convert(value, type).orElseGet(() -> { - LOGGER.warn("Cannot convert value {} to type {}", value, type); + logTypeConversionFailure(type, value); return null; }); }