From 8dd5ecb2f04a923a8676c6afc9946f81b0a27d44 Mon Sep 17 00:00:00 2001 From: Jonathan Giles Date: Fri, 26 Jul 2019 12:19:08 +1200 Subject: [PATCH] Removing the Jackson leak in HttpHeaders, instead registering a separate serializer directly with the JacksonAdapter. Tests that failed after functionality was removed from HttpHeaders now pass with the new HttpHeaderSerializer. --- .../java/com/azure/core/http/HttpHeaders.java | 18 +--------- .../jackson/HttpHeadersSerializer.java | 35 +++++++++++++++++++ .../serializer/jackson/JacksonAdapter.java | 3 +- 3 files changed, 38 insertions(+), 18 deletions(-) create mode 100644 sdk/core/azure-core/src/main/java/com/azure/core/implementation/serializer/jackson/HttpHeadersSerializer.java diff --git a/sdk/core/azure-core/src/main/java/com/azure/core/http/HttpHeaders.java b/sdk/core/azure-core/src/main/java/com/azure/core/http/HttpHeaders.java index e39bdd33b2483..ee0ad8cfdb57b 100644 --- a/sdk/core/azure-core/src/main/java/com/azure/core/http/HttpHeaders.java +++ b/sdk/core/azure-core/src/main/java/com/azure/core/http/HttpHeaders.java @@ -3,12 +3,6 @@ package com.azure.core.http; -import com.fasterxml.jackson.core.JsonGenerator; -import com.fasterxml.jackson.databind.JsonSerializable; -import com.fasterxml.jackson.databind.SerializerProvider; -import com.fasterxml.jackson.databind.jsontype.TypeSerializer; - -import java.io.IOException; import java.util.HashMap; import java.util.Iterator; import java.util.Locale; @@ -17,7 +11,7 @@ /** * A collection of headers on an HTTP request or response. */ -public class HttpHeaders implements Iterable, JsonSerializable { +public class HttpHeaders implements Iterable { private final Map headers = new HashMap<>(); /** @@ -128,14 +122,4 @@ public Map toMap() { public Iterator iterator() { return headers.values().iterator(); } - - @Override - public void serialize(JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException { - jsonGenerator.writeObject(toMap()); - } - - @Override - public void serializeWithType(JsonGenerator jsonGenerator, SerializerProvider serializerProvider, TypeSerializer typeSerializer) throws IOException { - serialize(jsonGenerator, serializerProvider); - } } diff --git a/sdk/core/azure-core/src/main/java/com/azure/core/implementation/serializer/jackson/HttpHeadersSerializer.java b/sdk/core/azure-core/src/main/java/com/azure/core/implementation/serializer/jackson/HttpHeadersSerializer.java new file mode 100644 index 0000000000000..4174330878b02 --- /dev/null +++ b/sdk/core/azure-core/src/main/java/com/azure/core/implementation/serializer/jackson/HttpHeadersSerializer.java @@ -0,0 +1,35 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.azure.core.implementation.serializer.jackson; + +import com.azure.core.http.HttpHeaders; +import com.fasterxml.jackson.core.JsonGenerator; +import com.fasterxml.jackson.databind.JsonSerializer; +import com.fasterxml.jackson.databind.SerializerProvider; +import com.fasterxml.jackson.databind.jsontype.TypeSerializer; +import com.fasterxml.jackson.databind.module.SimpleModule; + +import java.io.IOException; + +/** + * Custom serializer for serializing {@code HttpHeaders} objects into Base64 strings. + */ +final class HttpHeadersSerializer extends JsonSerializer { + /** + * Gets a module wrapping this serializer as an adapter for the Jackson + * ObjectMapper. + * + * @return a simple module to be plugged onto Jackson ObjectMapper. + */ + public static SimpleModule getModule() { + SimpleModule module = new SimpleModule(); + module.addSerializer(HttpHeaders.class, new HttpHeadersSerializer()); + return module; + } + + @Override + public void serialize(HttpHeaders value, JsonGenerator jgen, SerializerProvider provider) throws IOException { + jgen.writeObject(value.toMap()); + } +} diff --git a/sdk/core/azure-core/src/main/java/com/azure/core/implementation/serializer/jackson/JacksonAdapter.java b/sdk/core/azure-core/src/main/java/com/azure/core/implementation/serializer/jackson/JacksonAdapter.java index c5a0a8c8fd382..c8fa542aa34e3 100644 --- a/sdk/core/azure-core/src/main/java/com/azure/core/implementation/serializer/jackson/JacksonAdapter.java +++ b/sdk/core/azure-core/src/main/java/com/azure/core/implementation/serializer/jackson/JacksonAdapter.java @@ -170,7 +170,8 @@ private static T initializeObjectMapper(T mapper) { .registerModule(Base64UrlSerializer.getModule()) .registerModule(DateTimeSerializer.getModule()) .registerModule(DateTimeRfc1123Serializer.getModule()) - .registerModule(DurationSerializer.getModule()); + .registerModule(DurationSerializer.getModule()) + .registerModule(HttpHeadersSerializer.getModule()); mapper.setVisibility(mapper.getSerializationConfig().getDefaultVisibilityChecker() .withFieldVisibility(JsonAutoDetect.Visibility.ANY) .withSetterVisibility(JsonAutoDetect.Visibility.NONE)