Skip to content

Consider adding support for contextual converters #36263

@NicklasWallgren

Description

@NicklasWallgren

It would be useful if we could access contextual information during the conversion of a request parameter (or similar). That would allow us to influence the conversion result based on for example annotations present at the usage site.

Both Jackson and Hibernate offer comparable mechanisms for this:

  • Jackson via ContextualSerializer (now deprecated, with the functionality included into ValueSerializer)
  • Hibernate via DynamicParameterizedType (also deprecated, with the functionality now built directly into UserType)

Example

@RestController
static class ExampleController {
    @RequestMapping(value = "/v1/10-digit-ssn", method = RequestMethod.GET)
    public void tenDigitSsn(@RequestParam(name = "ssn") @SSNFormat(length=10) SSN tenDigitSsn) {}

    @RequestMapping(value = "/v1/12-digit-ssn", method = RequestMethod.GET)
    public void twelveDigitSsn(@RequestParam(name = "ssn") @SSNFormat(length=12) SSN twelveDigitSsn) {}
}

What is possible using Jackson;

record Response(@SsnFormat(as = Type.INTEGER) SSN ssn) {}

What is possible using Hibernate;

@SSNFormat(as = Type.INTEGER)
@Column(name = "ssn")
SSN ssn;

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions