Spring Framework
  1. Spring Framework
  2. SPR-6380

Support for @Valid on @PathVariable, @RequestHeader, @RequestParam

    Details

    • Type: Improvement Improvement
    • Status: Open
    • Priority: Major Major
    • Resolution: Unresolved
    • Affects Version/s: 3.0 RC2
    • Fix Version/s: 4.1 RC1
    • Component/s: Web
    • Labels:
      None
    • Last commented by a User:
      true

      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
          Thad West added a comment -

          " Instead, we'd have to support constraint annotations like @NotNull, @Size etc themselves as parameter annotations. We can certainly consider that."...yes please!

          Show
          Thad West added a comment - " Instead, we'd have to support constraint annotations like @NotNull, @Size etc themselves as parameter annotations. We can certainly consider that."...yes please!
          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

            People

            • Assignee:
              Rossen Stoyanchev
              Reporter:
              Gerrit Brehmer
              Last updater:
              Benjamin M
            • Votes:
              32 Vote for this issue
              Watchers:
              34 Start watching this issue

              Dates

              • Created:
                Updated:
                Days since last comment:
                5 days ago