Skip to content

Deserializing a date fails with RestClient but passes with WebTestClient #31568

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

Closed
donalmurtagh opened this issue Nov 7, 2023 · 3 comments
Closed
Assignees
Labels
for: external-project Needs a fix in external project in: web Issues in web modules (web, webmvc, webflux, websocket) status: duplicate A duplicate of another issue

Comments

@donalmurtagh
Copy link

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

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Nov 7, 2023
@snicoll snicoll added the in: web Issues in web modules (web, webmvc, webflux, websocket) label Nov 7, 2023
@poutsma poutsma self-assigned this Nov 7, 2023
@bclozel
Copy link
Member

bclozel commented Nov 7, 2023

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 spring-projects/spring-boot#37033

Thanks!

@bclozel bclozel closed this as not planned Won't fix, can't repro, duplicate, stale Nov 7, 2023
@bclozel bclozel added status: duplicate A duplicate of another issue for: external-project Needs a fix in external project and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Nov 7, 2023
@donalmurtagh
Copy link
Author

donalmurtagh commented Nov 7, 2023

@bclozel thanks very much for the explanation, I'll give your suggestion a try

Update: it worked!

@bclozel
Copy link
Member

bclozel commented Nov 8, 2023

Thanks for letting us know @donalmurtagh , and thanks for trying out the RC versions!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
for: external-project Needs a fix in external project in: web Issues in web modules (web, webmvc, webflux, websocket) status: duplicate A duplicate of another issue
Projects
None yet
Development

No branches or pull requests

5 participants