Affects: spring 6.1.0-RC2 with spring boot 3.2.0-RC2 and JDK21
Description
I've attached a sample Spring Boot application with a single endpoint that deserializes a date from the request data. The deserialization happens in the following lines of DateDeserializerConfig
module.addDeserializer(LocalDateTime.class, new JsonDeserializer<>() {
@Override
public LocalDateTime deserialize(JsonParser jsonParser, DeserializationContext context) throws IOException {
// the following line returns null when invoked with a RestClient
String dateTimeWithZone = jsonParser.getValueAsString();
logger.info("Converting {} to LocalDateTime", dateTimeWithZone);
return ZonedDateTime.parse(dateTimeWithZone).toLocalDateTime();
}
});
Steps to Reproduce
The application contains 2 integration tests, one of which invokes the endpoint with a RestClient
and the other uses WebTestClient
. The same request data, headers, etc. are used in both cases.
If the tests in DemoApplicationTests
are run, testEndpointWithWebClient
passes, but testEndpointWithRestClient
fails. The failure is caused by jsonParser.getValueAsString()
returning null when the latter test runs.
If the date deserialization logic is removed, both tests pass. demo.zip
Comment From: bclozel
I think this is due to the fact that you're creating your own RestClient
instance from a base builder; this means that any Spring Boot auto-configuration is not processed for this instance.
I've changed your test with the following:
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class DemoApplicationTests {
@Autowired
private WebTestClient webClient;
private RestClient restClient;
@LocalServerPort
private int port;
@BeforeEach
private void setup(@Autowired RestClient.Builder builder) {
restClient = builder.baseUrl("http://localhost:" + port)
.defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.build();
}
I think this support has been added in https://github.com/spring-projects/spring-boot/issues/37033
Thanks!
Comment From: donalmurtagh
@bclozel thanks very much for the explanation, I'll give your suggestion a try
Update: it worked!
Comment From: bclozel
Thanks for letting us know @donalmurtagh , and thanks for trying out the RC versions!