Skip to content

Commit 5502e61

Browse files
committed
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
1 parent ab9bea1 commit 5502e61

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

spring-web/src/main/java/org/springframework/http/converter/json/AbstractJackson2HttpMessageConverter.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -30,6 +30,7 @@
3030
import java.util.LinkedHashMap;
3131
import java.util.List;
3232
import java.util.Map;
33+
import java.util.Optional;
3334
import java.util.concurrent.atomic.AtomicReference;
3435
import java.util.function.Consumer;
3536

@@ -471,7 +472,7 @@ protected void writeInternal(Object object, @Nullable Type type, HttpOutputMessa
471472
if (filters != null) {
472473
objectWriter = objectWriter.with(filters);
473474
}
474-
if (javaType != null && javaType.isContainerType()) {
475+
if (javaType != null && (javaType.isContainerType() || javaType.isTypeOrSubTypeOf(Optional.class))) {
475476
objectWriter = objectWriter.forType(javaType);
476477
}
477478
SerializationConfig config = objectWriter.getConfig();

spring-web/src/test/java/org/springframework/http/converter/json/MappingJackson2HttpMessageConverterTests.java

+28-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -24,8 +24,11 @@
2424
import java.util.HashMap;
2525
import java.util.List;
2626
import java.util.Map;
27+
import java.util.Optional;
2728

2829
import com.fasterxml.jackson.annotation.JsonFilter;
30+
import com.fasterxml.jackson.annotation.JsonSubTypes;
31+
import com.fasterxml.jackson.annotation.JsonTypeInfo;
2932
import com.fasterxml.jackson.annotation.JsonView;
3033
import com.fasterxml.jackson.databind.DeserializationFeature;
3134
import com.fasterxml.jackson.databind.JavaType;
@@ -345,6 +348,18 @@ public void writeParameterizedBaseType() throws Exception {
345348
JSONAssert.assertEquals(body, outputMessage.getBodyAsString(StandardCharsets.UTF_8), true);
346349
}
347350

351+
// gh-24498
352+
@Test
353+
public void writeOptional() throws IOException {
354+
ParameterizedTypeReference<Optional<MyParent>> optionalParent = new ParameterizedTypeReference<>() {};
355+
Optional<MyParent> result = Optional.of(new Impl1());
356+
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
357+
converter.write(result, optionalParent.getType(), MediaType.APPLICATION_JSON, outputMessage);
358+
359+
assertThat(outputMessage.getBodyAsString(StandardCharsets.UTF_8))
360+
.contains("@type");
361+
}
362+
348363
@Test
349364
public void prettyPrint() throws Exception {
350365
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
@@ -773,6 +788,18 @@ public String toString() {
773788
}
774789
}
775790

791+
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME)
792+
@JsonSubTypes(value = {@JsonSubTypes.Type(value = Impl1.class),
793+
@JsonSubTypes.Type(value = Impl2.class)})
794+
public static interface MyParent {
795+
}
796+
797+
public static class Impl1 implements MyParent {
798+
}
799+
800+
public static class Impl2 implements MyParent {
801+
}
802+
776803
private static class MappingJackson2HttpMessageConverterWithCustomization extends MappingJackson2HttpMessageConverter {
777804

778805
@Override

0 commit comments

Comments
 (0)