Support Optional in AbstractJackson2HttpMessageConverter

This commit introduces support for Optional in the
AbstractJackson2HttpMessageConverter, similar the existing support for
collection types were supported.

Closes gh-24498
This commit is contained in:
Arjen Poutsma 2023-01-24 13:06:41 +01:00
parent ab9bea1d93
commit 5502e61519
2 changed files with 31 additions and 3 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -30,6 +30,7 @@ import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Consumer;
@ -471,7 +472,7 @@ public abstract class AbstractJackson2HttpMessageConverter extends AbstractGener
if (filters != null) {
objectWriter = objectWriter.with(filters);
}
if (javaType != null && javaType.isContainerType()) {
if (javaType != null && (javaType.isContainerType() || javaType.isTypeOrSubTypeOf(Optional.class))) {
objectWriter = objectWriter.forType(javaType);
}
SerializationConfig config = objectWriter.getConfig();

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -24,8 +24,11 @@ import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import com.fasterxml.jackson.annotation.JsonFilter;
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.JsonView;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JavaType;
@ -345,6 +348,18 @@ public class MappingJackson2HttpMessageConverterTests {
JSONAssert.assertEquals(body, outputMessage.getBodyAsString(StandardCharsets.UTF_8), true);
}
// gh-24498
@Test
public void writeOptional() throws IOException {
ParameterizedTypeReference<Optional<MyParent>> optionalParent = new ParameterizedTypeReference<>() {};
Optional<MyParent> result = Optional.of(new Impl1());
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
converter.write(result, optionalParent.getType(), MediaType.APPLICATION_JSON, outputMessage);
assertThat(outputMessage.getBodyAsString(StandardCharsets.UTF_8))
.contains("@type");
}
@Test
public void prettyPrint() throws Exception {
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
@ -773,6 +788,18 @@ public class MappingJackson2HttpMessageConverterTests {
}
}
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME)
@JsonSubTypes(value = {@JsonSubTypes.Type(value = Impl1.class),
@JsonSubTypes.Type(value = Impl2.class)})
public static interface MyParent {
}
public static class Impl1 implements MyParent {
}
public static class Impl2 implements MyParent {
}
private static class MappingJackson2HttpMessageConverterWithCustomization extends MappingJackson2HttpMessageConverter {
@Override