spring-projects / spring-projects/spring-data-rest

Unable to set custom converters on HttpHeadersPreparer

Open
#2,163 1 comment 0 reactions 1 assignee View on GitHub

@odrotbohm is already working on this.

Since Jul 25, 2022.

status: waiting-for-triage
Dominant language
Java
Stars
958
Forks
568
PR merge metrics
No merged PRs in 30d

Description

When using spring-data Auditing functionality with unnoficial supported types like OffsetDateTime, you could customize all process using property dateTimeProviderRef on EnableJpaAuditing/EnableMongoAuditing.

All works with no problem for @CreatedDate annotation, but problems comes in with @LastModifiedBy annotation.

Problem in concrete is with class org.springframework.data.rest.webmvc.HttpHeadersPreparer and method getLastModifiedInMilliseconds

	private Optional<Long> getLastModifiedInMilliseconds(Object object) {

		return getAuditableBeanWrapper(object)//
				.flatMap(it -> it.getLastModifiedDate())//
				.map(it -> conversionService.convert(it, Date.class))//
				.map(it -> conversionService.convert(it, Instant.class))//
				.map(it -> it.toEpochMilli());
	}

As you can see, conversionService try to convert lastModifiedDate to Date or Instant, the main problem is that conversionService couldn´t be addapted for inclusion of custom converters because it is initilized on HttpHeadersPreparer constructor:

	private final ConfigurableConversionService conversionService = new DefaultConversionService();

	public HttpHeadersPreparer(AuditableBeanWrapperFactory auditableBeanWrapperFactory) {
		Assert.notNull(auditableBeanWrapperFactory, "AuditableBeanWrapperFactory must not be null!");
		Jsr310Converters.getConvertersToRegister().forEach(conversionService::addConverter);
		this.auditableBeanWrapperFactory = auditableBeanWrapperFactory;
	}

It would be nice to reach that conversionService for registering custom converters

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.