Skip to content

Commit

Permalink
Use ObjectMapper from context (#768)
Browse files Browse the repository at this point in the history
  • Loading branch information
yawkat authored Jan 10, 2024
1 parent dd25cce commit 9794677
Show file tree
Hide file tree
Showing 10 changed files with 129 additions and 35 deletions.
4 changes: 2 additions & 2 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[versions]
micronaut = "4.2.0"
micronaut = "4.2.2"
micronaut-platform = "4.1.6"
micronaut-docs = "2.0.0"
fn = '1.0.180'
Expand All @@ -25,7 +25,7 @@ micronaut-kotlin = "4.1.0"
micronaut-micrometer = "5.2.0"
micronaut-reactor = "3.1.0"
micronaut-rxjava2 = "2.1.0"
micronaut-serde = "2.4.0"
micronaut-serde = "2.7.0"
micronaut-servlet = "4.2.1"
micronaut-sql = "5.2.0"
micronaut-test = "4.1.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@
import io.micronaut.http.client.annotation.Client;
import io.micronaut.json.JsonMapper;
import io.micronaut.oraclecloud.serde.OciSdkMicronautSerializer;
import io.micronaut.oraclecloud.serde.OciSerdeConfiguration;
import io.micronaut.oraclecloud.serde.OciSerializationConfiguration;
import io.micronaut.scheduling.TaskExecutors;
import io.micronaut.serde.ObjectMapper;
import jakarta.inject.Inject;
import jakarta.inject.Named;
import jakarta.inject.Singleton;
Expand All @@ -47,10 +50,26 @@ public class ManagedNettyHttpProvider implements HttpProvider {
final JsonMapper jsonMapper;

@Inject
public ManagedNettyHttpProvider(@Client(id = SERVICE_ID) HttpClient mnHttpClient, @Named(TaskExecutors.BLOCKING) ExecutorService ioExecutor, JsonMapper jsonMapper) {
public ManagedNettyHttpProvider(
@Client(id = SERVICE_ID) HttpClient mnHttpClient,
@Named(TaskExecutors.BLOCKING) ExecutorService ioExecutor,
ObjectMapper jsonMapper,
OciSerdeConfiguration ociSerdeConfiguration,
OciSerializationConfiguration ociSerializationConfiguration
) {
this.mnHttpClient = mnHttpClient;
this.ioExecutor = ioExecutor;
this.jsonMapper = jsonMapper;
this.jsonMapper = jsonMapper.cloneWithConfiguration(ociSerdeConfiguration, ociSerializationConfiguration, null);
}

// for OKE
public ManagedNettyHttpProvider(
HttpClient mnHttpClient,
ExecutorService ioExecutor
) {
this.mnHttpClient = mnHttpClient;
this.ioExecutor = ioExecutor;
this.jsonMapper = OciSdkMicronautSerializer.getDefaultObjectMapper();
}

@Override
Expand All @@ -60,7 +79,6 @@ public HttpClientBuilder newBuilder() {

@Override
public Serializer getSerializer() {
// todo: use serializer from context
return OciSdkMicronautSerializer.getDefaultSerializer();
return new OciSdkMicronautSerializer(jsonMapper);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import io.micronaut.http.client.DefaultHttpClientConfiguration;
import io.micronaut.http.client.netty.ConnectionManager;
import io.micronaut.http.client.netty.DefaultHttpClient;
import io.micronaut.json.JsonMapper;
import io.micronaut.oraclecloud.serde.OciSdkMicronautSerializer;
import io.netty.buffer.ByteBufAllocator;

import java.io.Closeable;
Expand Down Expand Up @@ -58,6 +60,7 @@ final class NettyHttpClient implements HttpClient {
final Closeable upstreamHttpClient;
final ConnectionManager connectionManager;
final DefaultHttpClient.RequestKey requestKey;
final JsonMapper jsonMapper;

static {
ClientConfiguration cfg = ClientConfiguration.builder().build();
Expand All @@ -81,6 +84,7 @@ final class NettyHttpClient implements HttpClient {
}
mnClient = new DefaultHttpClient((URI) null, cfg);
blockingIoExecutor = Executors.newCachedThreadPool();
jsonMapper = OciSdkMicronautSerializer.getDefaultObjectMapper();
} else {
hasContext = true;
for (Map.Entry<ClientProperty<?>, Object> entry : builder.properties.entrySet()) {
Expand All @@ -90,6 +94,7 @@ final class NettyHttpClient implements HttpClient {
}
mnClient = (DefaultHttpClient) builder.managedProvider.mnHttpClient;
blockingIoExecutor = builder.managedProvider.ioExecutor;
jsonMapper = builder.managedProvider.jsonMapper;
}
upstreamHttpClient = mnClient;
connectionManager = mnClient.connectionManager();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import com.oracle.bmc.http.client.Method;
import com.oracle.bmc.http.client.RequestInterceptor;
import io.micronaut.http.client.netty.ConnectionManager;
import io.micronaut.oraclecloud.serde.OciSdkMicronautSerializer;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.buffer.Unpooled;
Expand Down Expand Up @@ -127,7 +126,7 @@ public HttpRequest body(Object body) {
// anything but String
String json;
try {
json = OciSdkMicronautSerializer.getDefaultObjectMapper().writeValueAsString(body);
json = client.jsonMapper.writeValueAsString(body);
} catch (IOException e) {
throw new IllegalArgumentException("Unable to process JSON body", e);
}
Expand Down Expand Up @@ -382,7 +381,7 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) {
skipLast = true;
}
} else {
future.complete(new NettyHttpResponse(response, limitedBufferingBodyHandler, undecidedBodyHandler, offloadExecutor));
future.complete(new NettyHttpResponse(client.jsonMapper, response, limitedBufferingBodyHandler, undecidedBodyHandler, offloadExecutor));
ctx.pipeline().remove(this);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import com.oracle.bmc.http.client.HttpResponse;
import io.micronaut.core.type.Argument;
import io.micronaut.oraclecloud.serde.OciSdkMicronautSerializer;
import io.micronaut.json.JsonMapper;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufInputStream;

Expand All @@ -33,12 +33,14 @@
import java.util.function.Function;

final class NettyHttpResponse implements HttpResponse {
private final JsonMapper jsonMapper;
private final io.netty.handler.codec.http.HttpResponse nettyResponse;
private final LimitedBufferingBodyHandler limitedBufferingBodyHandler;
private final UndecidedBodyHandler undecidedBodyHandler;
private final Executor offloadExecutor;

NettyHttpResponse(io.netty.handler.codec.http.HttpResponse nettyResponse, LimitedBufferingBodyHandler limitedBufferingBodyHandler, UndecidedBodyHandler undecidedBodyHandler, Executor offloadExecutor) {
NettyHttpResponse(JsonMapper jsonMapper, io.netty.handler.codec.http.HttpResponse nettyResponse, LimitedBufferingBodyHandler limitedBufferingBodyHandler, UndecidedBodyHandler undecidedBodyHandler, Executor offloadExecutor) {
this.jsonMapper = jsonMapper;
this.nettyResponse = nettyResponse;
this.limitedBufferingBodyHandler = limitedBufferingBodyHandler;
this.undecidedBodyHandler = undecidedBodyHandler;
Expand Down Expand Up @@ -98,7 +100,7 @@ public <T> CompletionStage<T> body(Class<T> type) {
return null;
}

return OciSdkMicronautSerializer.getDefaultObjectMapper().readValue(new ByteBufInputStream(buf), type);
return jsonMapper.readValue(new ByteBufInputStream(buf), type);
} catch (IOException e) {
throw new CompletionException(e);
} finally {
Expand All @@ -112,7 +114,7 @@ public <T> CompletionStage<List<T>> listBody(Class<T> type) {
Argument<List<T>> listArgument = Argument.listOf(type);
return thenApply(bodyAsBuffer(), buf -> {
try {
return OciSdkMicronautSerializer.getDefaultObjectMapper().readValue(new ByteBufInputStream(buf), listArgument);
return jsonMapper.readValue(new ByteBufInputStream(buf), listArgument);
} catch (IOException e) {
throw new CompletionException(e);
} finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.oracle.bmc.http.internal.ResponseHelper;
import com.oracle.bmc.model.RegionSchema;
import io.micronaut.core.annotation.Internal;
import io.micronaut.json.JsonMapper;
import io.micronaut.serde.ObjectMapper;
import io.micronaut.serde.annotation.SerdeImport;
import io.micronaut.serde.config.annotation.SerdeConfig;
Expand All @@ -48,27 +49,13 @@
@SerdeImport(OkeResourcePrincipalSessionToken.class)
public final class OciSdkMicronautSerializer implements Serializer {

private static final Map<String, Object> DEFAULT_MAPPER_CONFIG = Map.of(
"micronaut.serde.writeDatesAsTimestamps", false,
"micronaut.serde.write-binary-as-array", false,
"micronaut.serde.serialization.inclusion", SerdeConfig.SerInclude.NON_NULL
);

private static final ObjectMapper DEFAULT_MAPPER = ObjectMapper.create(
DEFAULT_MAPPER_CONFIG,
"io.micronaut.oraclecloud.serde.filter",
"io.micronaut.oraclecloud.serde.serializers"
);

private static final Serializer DEFAULT_SERIALIZER = new OciSdkMicronautSerializer(DEFAULT_MAPPER);

private final ObjectMapper objectMapper;
private final JsonMapper objectMapper;

/**
* Create Serializer from micronaut serde {@link ObjectMapper}.
* @param objectMapper the object mapper
*/
public OciSdkMicronautSerializer(ObjectMapper objectMapper) {
public OciSdkMicronautSerializer(JsonMapper objectMapper) {
this.objectMapper = objectMapper;
}

Expand All @@ -91,13 +78,31 @@ public String writeValueAsString(Object o) throws IOException {
* @return The implementation of object mapper configured for oci java sdk
*/
public static ObjectMapper getDefaultObjectMapper() {
return DEFAULT_MAPPER;
return UnmanagedSerializerHolder.DEFAULT_MAPPER;
}

/**
* @return The implementation of serializer configured for oci java sdk
*/
public static Serializer getDefaultSerializer() {
return DEFAULT_SERIALIZER;
return UnmanagedSerializerHolder.DEFAULT_SERIALIZER;
}

private static class UnmanagedSerializerHolder {
// only initialize if necessary

private static final Map<String, Object> DEFAULT_MAPPER_CONFIG = Map.of(
"micronaut.serde.writeDatesAsTimestamps", false,
"micronaut.serde.write-binary-as-array", false,
"micronaut.serde.serialization.inclusion", SerdeConfig.SerInclude.NON_NULL
);

private static final ObjectMapper DEFAULT_MAPPER = ObjectMapper.create(
DEFAULT_MAPPER_CONFIG,
"io.micronaut.oraclecloud.serde.filter",
"io.micronaut.oraclecloud.serde.serializers"
);

private static final Serializer DEFAULT_SERIALIZER = new OciSdkMicronautSerializer(DEFAULT_MAPPER);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright 2017-2024 original authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.micronaut.oraclecloud.serde;

import io.micronaut.context.annotation.Bean;
import io.micronaut.context.annotation.ConfigurationProperties;
import io.micronaut.core.annotation.Internal;
import io.micronaut.core.bind.annotation.Bindable;
import io.micronaut.serde.config.SerdeConfiguration;

@ConfigurationProperties("oci.serde")
@Bean(typed = OciSerdeConfiguration.class)
@Internal
public interface OciSerdeConfiguration extends SerdeConfiguration {
@Override
@Bindable(defaultValue = "false")
boolean isWriteBinaryAsArray();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright 2017-2024 original authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.micronaut.oraclecloud.serde;

import io.micronaut.context.annotation.Bean;
import io.micronaut.context.annotation.ConfigurationProperties;
import io.micronaut.core.annotation.Internal;
import io.micronaut.core.bind.annotation.Bindable;
import io.micronaut.serde.config.SerializationConfiguration;
import io.micronaut.serde.config.annotation.SerdeConfig;

@ConfigurationProperties("oci.serde.serialization")
@Bean(typed = OciSerializationConfiguration.class)
@Internal
public interface OciSerializationConfiguration extends SerializationConfiguration {
@Bindable(defaultValue = "NON_NULL")
@Override
SerdeConfig.SerInclude getInclusion();
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
import io.micronaut.context.annotation.Requires;
import io.micronaut.http.client.HttpClient;
import io.micronaut.http.client.annotation.Client;
import io.micronaut.json.JsonMapper;
import io.micronaut.oraclecloud.serde.OciSerdeConfiguration;
import io.micronaut.oraclecloud.serde.OciSerializationConfiguration;
import io.micronaut.scheduling.TaskExecutors;
import io.micronaut.serde.ObjectMapper;
import io.micronaut.test.extensions.junit5.annotation.MicronautTest;
import jakarta.inject.Inject;
import jakarta.inject.Named;
Expand Down Expand Up @@ -43,8 +45,8 @@ public void managedClientUsesManagedProvider() {
public static class MockProvider extends ManagedNettyHttpProvider {
int buildersCreated = 0;

public MockProvider(@Client(id = SERVICE_ID) HttpClient mnHttpClient, @Named(TaskExecutors.BLOCKING) ExecutorService ioExecutor, JsonMapper jsonMapper) {
super(mnHttpClient, ioExecutor, jsonMapper);
public MockProvider(@Client(id = SERVICE_ID) HttpClient mnHttpClient, @Named(TaskExecutors.BLOCKING) ExecutorService ioExecutor, ObjectMapper jsonMapper, OciSerdeConfiguration ociSerdeConfiguration, OciSerializationConfiguration ociSerializationConfiguration) {
super(mnHttpClient, ioExecutor, jsonMapper, ociSerdeConfiguration, ociSerializationConfiguration);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ protected HttpClient makeClient(String endpoint, RequestSigner requestSigner) {
return null;
}

HttpClientBuilder rptBuilder = new ManagedNettyHttpProvider(defaultHttpClient(), Executors.newCachedThreadPool(), JsonMapper.createDefault())
HttpClientBuilder rptBuilder = new ManagedNettyHttpProvider(defaultHttpClient(), Executors.newCachedThreadPool())
.newBuilder()
.baseUri(URI.create(endpoint))
.registerRequestInterceptor(
Expand Down

0 comments on commit 9794677

Please sign in to comment.