Skip to content

Advanced validation support #143

Closed
Closed

Description

@toronik

Hi, thanks for the great project!
I have a question regarding request params object validation.

For example I have a request with 2 dates like this:

    PeriodRequest:
      type: object
      properties:
        start:
          type: string
          format: date
        end:
          type: string
          format: date

and want to validate that start doesn't exceeds end.

One option is to use custom Bean Validation constraint annotation:

    // here  @StartEndConstraint
    @GetMapping(...)
    Collection getResponse(
      @Valid 
      // or here  @StartEndConstraint 
       PeriodRequest r
     );

But I can't do it because Api interface above is generated and at the same time i can't put annotations on the implementation class:

javax.validation.ConstraintDeclarationException: HV000151: A method overriding another method must not redefine the parameter constraint configuration

As I understand the only solution is to switch to the not generated pojo and use annotation on it:

map:
  types: 
    - type: PeriodRequest => my.not.generated.PeriodRequest

But that's undesirable solution because of the potential discrepancy between validation described in api.yaml and in code.

Are there any other options to solve this problem?
May be to have some annotation mapping features would be a better solution, something like:

map:
  types: 
    - type: PeriodRequest annotate my.validation.StartEndConstraint

Thanks in advance!

Activity

  1. hauner commented on Jul 28, 2022

    @hauner
    Member

    I fear there is currently no way to add additional annotations except skipping the generated type.

    I'm fine with adding it. It will add a bit of flexibility.

    It is just a matter of choosing the right path ;-)

    The simple path will only allow custom annotations in context of bean validations. The bean validation code already handles annotations so it would be "just" a matter of providing the info to it (with something like your suggestion above).

    More complex one would allow custom annotations independent of bean validation. More flexibility, but I'm not sure it is worth implementing. It would have bean nice maybe once or twice. ;-)

  2. toronik commented on Jul 29, 2022

    @toronik
    Author

    I'm not feeling that I'm in a position to decide on the right path =)
    but "the simple path" seems more preferable to me from the pragmatical perspective: as I understand it's simpler to implement and covers specific and very typical problem (in contrast to rare and imaginary ones).

  3. hauner commented on Jul 29, 2022

    @hauner
    Member

    Was more a question to myself ;-)

    .. but you are right. I prefer the first path too :-)

  4. added this to the core: next milestone on Jul 29, 2022
  5. added 2 commits that reference this issue on Aug 20, 2022
  6. modified the milestones: core: next, 2022.5 on Aug 22, 2022
  7. added 2 commits that reference this issue on Aug 24, 2022
  8. hauner commented on Sep 7, 2022

    @hauner
    Member

    have something running, somehow went for the more general approach. :-)

    the mapping looks like this:

    map:
        parameters:
          - type: Foo @ io.openapiprocessor.samples.validations.Sum

    Foo is the OpenAPI object, and the mapping will add @Sum to a Foo parameter of an endpoint method:

        @PostMapping(
                path = "/validation",
                consumes = {"application/json"},
                produces = {"application/json"})
        Foo postValidation(@RequestBody @Sum @Valid @NotNull Foo body);

    I think, I will add the following variation

    map:
        types:
           - type: Foo @ io.openapiprocessor.samples.validations.Sum

    to add the annotation to the generated Foo class:

    @Sum
    public class Foo {
    
        @DecimalMin(value = "0")
        @JsonProperty("foo1")
        private Integer foo1;
    
        // ....
    }
  9. added 3 commits that reference this issue on Sep 8, 2022
  10. added 4 commits that reference this issue on Sep 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions