Skip to content

Commit

Permalink
Removing the Jackson leak in HttpHeaders, instead registering a separ…
Browse files Browse the repository at this point in the history
…ate serializer directly with the JacksonAdapter. Tests that failed after functionality was removed from HttpHeaders now pass with the new HttpHeaderSerializer. (#4597)
  • Loading branch information
JonathanGiles authored Jul 26, 2019
1 parent 570632c commit ea2f43a
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -17,7 +11,7 @@
/**
* A collection of headers on an HTTP request or response.
*/
public class HttpHeaders implements Iterable<HttpHeader>, JsonSerializable {
public class HttpHeaders implements Iterable<HttpHeader> {
private final Map<String, HttpHeader> headers = new HashMap<>();

/**
Expand Down Expand Up @@ -128,14 +122,4 @@ public Map<String, String> toMap() {
public Iterator<HttpHeader> 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);
}
}
Original file line number Diff line number Diff line change
@@ -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<HttpHeaders> {
/**
* 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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,8 @@ private static <T extends ObjectMapper> 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)
Expand Down

0 comments on commit ea2f43a

Please sign in to comment.