Spring Framework
  1. Spring Framework
  2. SPR-6380

Support for javax.validation annotations on @PathVariable, @RequestHeader, @RequestParam

    Details

    • Type: Improvement Improvement
    • Status: Open
    • Priority: Major Major
    • Resolution: Unresolved
    • Affects Version/s: 3.0 RC2, 4.0.5
    • Fix Version/s: 4.x Backlog
    • Component/s: Web
    • Labels:
      None
    • Last commented by a User:
      false

      Description

      I thought this was already done, because support for controller method-parameter validation should be available for all parameters.

      I there a reason to exclude annotated (@RequestParam, etc.) method parameters? Is it not possible to validate primitive values?

      Simple example:
      For Paging-Support i need two request parameters: "start" & "size". "start" must have a positive value or 0 and "size" must have a positive value and also a maximum set.

        Issue Links

          Activity

          Hide
          Johan Kindgren added a comment -

          I've started to look into this feature, would it be okay to assume that "primitive" arguments are annotated with javax.validation-annotations? (More precisely, annotated with annotations that themselves are annotated with javax.validation.Constraint.)
          "Complex" arguments (which internally are annotated with javax.validation-annotations) should be annotated with @Valid or @Validated?

          I also assume that the solution can't reference classes/interfaces from javax.validation?

          Show
          Johan Kindgren added a comment - I've started to look into this feature, would it be okay to assume that "primitive" arguments are annotated with javax.validation-annotations? (More precisely, annotated with annotations that themselves are annotated with javax.validation.Constraint.) "Complex" arguments (which internally are annotated with javax.validation-annotations) should be annotated with @Valid or @Validated? I also assume that the solution can't reference classes/interfaces from javax.validation?
          Hide
          Johan Kindgren added a comment -

          I was perhaps a bit enthusiastic about the solution. The upcoming release of Validation-api 1.1 could help a lot, but then again maybe it isn't possible to bump the validation-api version right away.

          Another possible solution could be to use a bytecode modifier to create a class with the correct annotations, but that seems somewhat like a hack?

          My attempted solution would add a "validate" method called from AbstractNamedValueMethodArgumentResolver.resolveArgument, seems like it could handle @RequestParameter, @RequestHeader and @PathVariable.

          Show
          Johan Kindgren added a comment - I was perhaps a bit enthusiastic about the solution. The upcoming release of Validation-api 1.1 could help a lot, but then again maybe it isn't possible to bump the validation-api version right away. Another possible solution could be to use a bytecode modifier to create a class with the correct annotations, but that seems somewhat like a hack? My attempted solution would add a "validate" method called from AbstractNamedValueMethodArgumentResolver.resolveArgument, seems like it could handle @RequestParameter, @RequestHeader and @PathVariable.
          Hide
          Jan Held added a comment -

          Is this topic still on the possible roadmap? I have the same problem, and fixed it for my case. I think a similar solution that Johan Kindgren proposed. Nevertheless I would prefer if this will be supported as a standard spring feature, so I can remove my workaround an simply provide custom validation Annotations and Classes.

          Show
          Jan Held added a comment - Is this topic still on the possible roadmap? I have the same problem, and fixed it for my case. I think a similar solution that Johan Kindgren proposed. Nevertheless I would prefer if this will be supported as a standard spring feature, so I can remove my workaround an simply provide custom validation Annotations and Classes.
          Hide
          Benjamin M added a comment -

          +1

          Show
          Benjamin M added a comment - +1
          Hide
          Rossen Stoyanchev added a comment -

          The underlying implementation for this, i.e. the ability to validate method parameters, has run into unexpected challenges. For a proper resolution unfortunately we'll have to revisit in 4.2, after SPR-11900 is resolved (see comments there for further detail).

          Show
          Rossen Stoyanchev added a comment - The underlying implementation for this, i.e. the ability to validate method parameters, has run into unexpected challenges. For a proper resolution unfortunately we'll have to revisit in 4.2, after SPR-11900 is resolved (see comments there for further detail).

            People

            • Assignee:
              Rossen Stoyanchev
              Reporter:
              Gerrit Brehmer
              Last updater:
              Rossen Stoyanchev
            • Votes:
              41 Vote for this issue
              Watchers:
              44 Start watching this issue

              Dates

              • Created:
                Updated:
                Days since last comment:
                38 weeks, 5 days ago