|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2022 the original author or authors. |
| 2 | + * Copyright 2002-2023 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
24 | 24 | import java.util.HashMap;
|
25 | 25 | import java.util.List;
|
26 | 26 | import java.util.Map;
|
| 27 | +import java.util.Optional; |
27 | 28 |
|
28 | 29 | import com.fasterxml.jackson.annotation.JsonFilter;
|
| 30 | +import com.fasterxml.jackson.annotation.JsonSubTypes; |
| 31 | +import com.fasterxml.jackson.annotation.JsonTypeInfo; |
29 | 32 | import com.fasterxml.jackson.annotation.JsonView;
|
30 | 33 | import com.fasterxml.jackson.databind.DeserializationFeature;
|
31 | 34 | import com.fasterxml.jackson.databind.JavaType;
|
@@ -345,6 +348,18 @@ public void writeParameterizedBaseType() throws Exception {
|
345 | 348 | JSONAssert.assertEquals(body, outputMessage.getBodyAsString(StandardCharsets.UTF_8), true);
|
346 | 349 | }
|
347 | 350 |
|
| 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 | + |
348 | 363 | @Test
|
349 | 364 | public void prettyPrint() throws Exception {
|
350 | 365 | MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
|
@@ -773,6 +788,18 @@ public String toString() {
|
773 | 788 | }
|
774 | 789 | }
|
775 | 790 |
|
| 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 | + |
776 | 803 | private static class MappingJackson2HttpMessageConverterWithCustomization extends MappingJackson2HttpMessageConverter {
|
777 | 804 |
|
778 | 805 | @Override
|
|
0 commit comments