Skip to content

Add bean validation annotations for the method parameters which are treated as headers #10555

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

valery1707
Copy link
Contributor

Bean validation annotations are not generated for the method parameters which are treated as headers.

Resolve #7125

PR checklist

  • Read the contribution guidelines.
  • Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community.
  • Run the following to build the project and update samples:
    ./mvnw clean package 
    ./bin/generate-samples.sh
    ./bin/utils/export_docs_generators.sh
    
    Commit all changed files.
    This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
    These must match the expectations made by your contribution.
    You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example ./bin/generate-samples.sh bin/configs/java*.
    For Windows users, please run the script in Git BASH.
  • File the PR against the correct branch: master (5.3.0), 6.0.x
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

@nmuesch

@valery1707 valery1707 force-pushed the java-spring-validate-headers branch from f7907ce to c43eef3 Compare October 7, 2021 18:19
@aaronbush
Copy link

I ran into a similar issue this week with Request Header validations. Happy to see someone has started to look into it already.

In trying your update to modules/openapi-generator/src/main/resources/JavaSpring/headerParams.mustache I ran into a couple of issues. Hoping you can validate these.

  • In the case where a header is set to required: false in specification and the useOptional configuration option is set the template will result in adding @Size/etc. annotations to a java.util.Optional type and at validation runtime an error will occur (in my test case No Validator could be found for constraint 'javax.validation.constraint.Size' validating type 'java.util.Optional....'
  • It looks like using beanValidationCore in the updated headerParams.mustache template will miss applying the @Valid and @NotNull which are in the beanValidation.mustache template.

One thing that I tried, which may address the issues, is to change the following section in headerParams.mustache from:

{{#useBeanValidation}}{{>beanValidationCore}}{{/useBeanValidation}}

to:

{{required}}{{#useBeanValidation}}{{>beanValidation}}{{/useBeanValidation}}{{/required}}

This will apply the beanValidation (which includes beanValidationCore) in the case where a field is marked as required.

@valery1707
Copy link
Contributor Author

Using the {{required}}{{#useBeanValidation}}{{>beanValidation}}{{/useBeanValidation}}{{/required}} will add validation constraints only in the case where a field is marked as required, but validation should be applied even for optional headers.
As I see - the problem exists only in the case required: false and useOptional: true because in this case generated wrapper around validated type.
I will try to fix this case.

@valery1707 valery1707 marked this pull request as draft October 13, 2021 06:27
@valery1707 valery1707 marked this pull request as ready for review October 13, 2021 16:33
@aaronbush
Copy link

In the case where the specification of the header has a schema type of object instead of a primitive we will need the @Valid annotation. I am thinking that the beanValidation template (includes the beanValidationCore) will help pick up the @Valid annotation. Not sure if this will break your primitive validations. (I don't expect there is heavy use of headers defined as Objects but it is used and valid in the spec - was actually where I hit this issue.)

I would include a part of the specification that includes a header with object type and ensure @Valid is attached to that. Maybe add to the schema something like this:

        - name: requiredObjectWithStringOfLength
          in: header
          required: true
          schema:
            type: object
            required:
              - aString
            properties:
              aString:
                type: string
                minLength: 32
                maxLength: 32

@valery1707 valery1707 force-pushed the java-spring-validate-headers branch from 2bc51b6 to b3990ba Compare October 13, 2021 19:22
@DonKeyHot1
Copy link

Hi, is there any progress on that pull-request?

@wing328
Copy link
Member

wing328 commented Apr 26, 2022

@valery1707 can you please resolve the merge conflicts when you've time?

@wing328
Copy link
Member

wing328 commented Apr 26, 2022

cc @cachescrubber (2022/02) @welshm (2022/02) @MelleD (2022/02) @atextor (2022/02) @manedev79 (2022/02) @javisst (2022/02) @borsch (2022/02) @banlevente (2022/02)

@cowclaw
Copy link
Contributor

cowclaw commented Jun 15, 2022

I'd love to see this integrated! It would save us having to maintain a customized mustache template for header parameters. Thanks for the PR!

@wing328
Copy link
Member

wing328 commented Jun 16, 2022

@valery1707 can you please resolve the merge conflicts when you've time?

kstrtNBS added a commit to kstrtNBS/openapi-generator that referenced this pull request Jul 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[BUG][JAVA] @Size not generated in the method parameters which are treated as headers
6 participants