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

Removing the Jackson leak in HttpHeaders #4597

Merged
merged 1 commit into from
Jul 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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