Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AWS to M7 #1760

Merged
merged 13 commits into from
Jun 12, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@

import io.micronaut.core.annotation.Internal;
import io.micronaut.core.annotation.NonNull;
import io.micronaut.core.annotation.Nullable;
import io.micronaut.core.convert.ConversionService;
import io.micronaut.core.convert.value.MutableConvertibleValues;
import io.micronaut.core.convert.value.MutableConvertibleValuesMap;
import io.micronaut.core.execution.ExecutionFlow;
import io.micronaut.core.io.buffer.ByteBuffer;
import io.micronaut.core.type.Argument;
import io.micronaut.core.util.CollectionUtils;
import io.micronaut.core.util.SupplierUtil;
import io.micronaut.http.FullHttpRequest;
import io.micronaut.http.HttpMethod;
import io.micronaut.http.MediaType;
import io.micronaut.http.MutableHttpRequest;
Expand All @@ -34,6 +37,8 @@
import io.micronaut.servlet.http.BodyBuilder;
import io.micronaut.servlet.http.ServletExchange;
import io.micronaut.servlet.http.ServletHttpRequest;
import io.micronaut.servlet.http.ParsedBodyHolder;
import io.micronaut.servlet.http.ByteArrayByteBuffer;
import org.slf4j.Logger;

import java.io.BufferedReader;
Expand All @@ -57,7 +62,7 @@
*/
@Internal
@SuppressWarnings("java:S119") // More descriptive generics are better here
public abstract class ApiGatewayServletRequest<T, REQ, RES> implements MutableServletHttpRequest<REQ, T>, ServletExchange<REQ, RES> {
public abstract class ApiGatewayServletRequest<T, REQ, RES> implements MutableServletHttpRequest<REQ, T>, ServletExchange<REQ, RES>, FullHttpRequest<T>, ParsedBodyHolder<T> {

private static final Set<Class<?>> RAW_BODY_TYPES = CollectionUtils.setOf(String.class, byte[].class, ByteBuffer.class, InputStream.class);

Expand All @@ -70,8 +75,11 @@ public abstract class ApiGatewayServletRequest<T, REQ, RES> implements MutableSe
private final MediaTypeCodecRegistry codecRegistry;
private MutableConvertibleValues<Object> attributes;
private Supplier<Optional<T>> body;
private T parsedBody;
private T overriddenBody;

private ByteArrayByteBuffer<T> servletByteBuffer;

protected ApiGatewayServletRequest(
ConversionService conversionService,
MediaTypeCodecRegistry codecRegistry,
Expand All @@ -88,7 +96,7 @@ protected ApiGatewayServletRequest(
this.httpMethod = httpMethod;
this.log = log;
this.body = SupplierUtil.memoizedNonEmpty(() -> {
T built = (T) bodyBuilder.buildBody(this::getInputStream, this);
T built = parsedBody != null ? parsedBody : (T) bodyBuilder.buildBody(this::getInputStream, this);
return Optional.ofNullable(built);
});
}
Expand All @@ -97,7 +105,7 @@ protected ApiGatewayServletRequest(

@Override
public InputStream getInputStream() throws IOException {
return new ByteArrayInputStream(getBodyBytes());
return servletByteBuffer != null ? servletByteBuffer.toInputStream() : new ByteArrayInputStream(getBodyBytes());
}

@Override
Expand Down Expand Up @@ -220,4 +228,30 @@ protected MapListOfStringAndMapStringMutableHttpParameters getParametersFromBody
public void setConversionService(ConversionService conversionService) {
this.conversionService = conversionService;
}

@Override
public void setParsedBody(T body) {
this.parsedBody = body;
}

@Override
public @Nullable ByteBuffer<?> contents() {
try {
if (servletByteBuffer == null) {
this.servletByteBuffer = new ByteArrayByteBuffer<>(getInputStream().readAllBytes());
}
return servletByteBuffer;
} catch (IOException e) {
throw new IllegalStateException("Error getting all body contents", e);
}
}

@Override
public @Nullable ExecutionFlow<ByteBuffer<?>> bufferContents() {
ByteBuffer<?> contents = contents();
if (contents == null) {
return null;
}
return ExecutionFlow.just(contents);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import java.util.Set;

/**
* Bean to check if response content is binary and should be base 64 encoded
* Bean to check if response content is binary and should be base 64 encoded.
*/
@Internal
@Singleton
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import io.micronaut.runtime.server.EmbeddedServer
//tag::rxImport[]
import org.reactivestreams.Publisher
import reactor.core.publisher.Mono
import spock.lang.PendingFeature

//end::rxImport[]
import spock.lang.Specification
Expand All @@ -37,7 +36,6 @@ import spock.lang.Specification
*/
class LocalFunctionInvokeSpec extends Specification {

@PendingFeature(reason = "https://github.com/micronaut-projects/micronaut-core/issues/9234")
//tag::invokeLocalFunction[]
void "test invoking a local function"() {
given:
Expand All @@ -55,7 +53,6 @@ class LocalFunctionInvokeSpec extends Specification {
}
//end::invokeLocalFunction[]

@PendingFeature(reason = "https://github.com/micronaut-projects/micronaut-core/issues/9234")
//tag::invokeRxLocalFunction[]
void "test invoking a local function - rx"() {
given:
Expand Down
24 changes: 12 additions & 12 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[versions]
micronaut = "4.0.0-M6"
micronaut = "4.0.0-M7"
micronaut-docs = "2.0.0"
micronaut-test = "4.0.0-M6"
micronaut-test = "4.0.0-M7"
micronaut-testresources = "2.0.0-M9"
groovy = "4.0.12"
spock = "2.3-groovy-4.0"
Expand All @@ -13,15 +13,15 @@ logback-json-classic = '0.1.5'
assertj = '3.24.2'

micronaut-discovery = "4.0.0-M3"
micronaut-groovy = "4.0.0-M1"
micronaut-logging = "1.0.0-M3"
micronaut-mongodb = "5.0.0-M2"
micronaut-reactor = "3.0.0-M4"
micronaut-security = "4.0.0-M5"
micronaut-serde = "2.0.0-M9"
micronaut-servlet = "4.0.0-M7"
micronaut-views = "4.0.0-M4"
micronaut-validation = "4.0.0-M9"
micronaut-groovy = "4.0.0-M3"
micronaut-logging = "1.0.0-M4"
micronaut-mongodb = "5.0.0-M4"
micronaut-reactor = "3.0.0-M6"
micronaut-security = "4.0.0-M6"
micronaut-serde = "2.0.0-M10"
micronaut-servlet = "4.0.0-M8"
micronaut-views = "4.0.0-M5"
micronaut-validation = "4.0.0-M10"

managed-alexa-ask-sdk = "2.71.0"
managed-aws-java-sdk-v1 = '1.12.483'
Expand All @@ -43,7 +43,7 @@ graal = "22.3.2"
kotlin = "1.8.21"

# Micronaut
micronaut-gradle-plugin = "4.0.0-M4"
micronaut-gradle-plugin = "4.0.0-M3"

[libraries]
# Core
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
})
@SuiteDisplayName("HTTP Server TCK for Function AWS API Gateway Proxy v1 Event model")
@ExcludeClassNamePatterns({
"io.micronaut.http.server.tck.tests.filter.RequestFilterTest", // Broken in servlet
"io.micronaut.http.server.tck.tests.constraintshandler.ControllerConstraintHandlerTest" // Broken in servlet
"io.micronaut.http.server.tck.tests.MissingBodyAnnotationTest",
"io.micronaut.http.server.tck.tests.StreamTest"
})
public class FunctionAwsApiGatewayProxyV1HttpServerTestSuite {
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
})
@SuiteDisplayName("HTTP Server TCK for Function AWS API Gateway Proxy v2 Event model")
@ExcludeClassNamePatterns({
"io.micronaut.http.server.tck.tests.filter.RequestFilterTest", // Broken in servlet
"io.micronaut.http.server.tck.tests.constraintshandler.ControllerConstraintHandlerTest" // Broken in servlet
"io.micronaut.http.server.tck.tests.MissingBodyAnnotationTest",
"io.micronaut.http.server.tck.tests.StreamTest"
})
public class FunctionAwsApiGatewayProxyV2HttpServerTestSuite {
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"io.micronaut.http.server.tck.tests.endpoints.health.HealthTest",
"io.micronaut.http.server.tck.tests.filter.RequestFilterTest",
"io.micronaut.http.server.tck.tests.filter.ResponseFilterTest",
"io.micronaut.http.server.tck.tests.constraintshandler.ControllerConstraintHandlerTest" // Broken in servlet
"io.micronaut.http.server.tck.tests.StreamTest" // Broken in servlet
})
public class MicronautLambdaHandlerHttpServerTestSuite {
}