I have a REST API defined in a swagger.yaml. Inside there are all the fields with their specifications (eg: length, pattern, etc.). Is there a way in java to validate these fields according to their specifications without having to do it manually?
In this way I should go and set the annotation manually above each
field, I was looking for a library that would automate everything by
reading the rules on the swagger
Yes, the OpenApi tool generator is able to do that.
https://openapi-generator.tech/docs/generators/jaxrs-spec
If the option "useBeanValidation" is enabled (default value), then the javax.validation annotations will be generated in your jaxrs bean.
If you are trying to validate request bodies in controller level, you should check javax validation constrains https://docs.oracle.com/javaee/7/api/javax/validation/constraints/package-summary.html
There are plenty of annotations such as #NotNull, #Pattern #Max etc. which will do the job
Related
My team is evaluating adopting OpenAPI Generator for our project, and the ability to declaratively keep Spring MVC mappings in sync with the spec is attractive. However, much of Spring MVC's value comes from its ability to automatically resolve request information and provide it as strongly typed method parameters, and in the examples I've seen the generated Java interfaces only reflect the request attributes as directly listed in the spec. For example, userId is a string in a path mapping, and in my controller I want to inject that as a UUID or even a User (with DomainClassConverter). Additionally, I may want access to the HttpSession.
Is there any way (short of rewriting the templates) to customize the method parameters so that I can get the mappings autogenerated but add attributes for my own requirements?
I annotated my various controller methods with swagger Annotations but now I want to be able to retrieve the info for a given method programmatically, e.g. I want to retrieve the APIOperation value and notes from within Java. Is that possible without me checking through all the Swagger Annotations?
Is it possible to turn off certain constraints / annotations per class at runtime? For instance if I wanted to turn of a #NotNull check on a firstName field, is that possible?
This would make testing to see whether a certain constraint is triggered correctly simpler, as I could turn off all the other constraints, and just check that one constraint.
Is it possible to turn off certain constraints / annotations per class
at runtime? For instance if I wanted to turn of a #NotNull check on a
firstName field, is that possible?
No it is not. Bean Validation does not define such a feature. There is an open issue in Hibernate Validator HV-98 which discusses the possibility of reloading metadata, but even there you would need to rebuild the validator factory.
You could override annotations via XML configuration and then recreate the Validator(Factory) instance using different configurations at the time, but that's probably not easy to mange.
This would make testing to see whether a certain constraint is
triggered correctly simpler, as I could turn off all the other
constraints, and just check that one constraint.
If it is about testing, you can use Validator.validateValue to just validate a given field. Other than that, if you validate the whole object graph and get a set of constraint violations back, you can just iterate over them and inspect the metadata. There is enough information in the metadata to verify that a specific constraints was executed and failed.
The hibernate validation annotation are usually used together with database constraints so it does not make sense to change the behavior at runtime. However if you want to do it you can implement your own validators (by overriding existing) and do whatever you want.
I am maintaining a web app that is using Java bean validations (as part of JSR303 I believe).
Members are mapped with annotations like #Pattern and then we have a LocalValidatorFactoryBean that performs the validation. The Validator that this factory bean bootstraps is actually the Hibernate validation instance.
My problem is that my #Pattern regex needs to be loaded at runtime when my application starts up, so I cannot use the annotation.
Therefore, I'm wondering if there is an alternative way such as XML to plug in such validation?
If not, I may just have to use a separate Spring validator to do this work
I recommend creating custom validator as described in http://docs.spring.io/spring/docs/3.0.0.RC3/reference/html/ch05s07.html#validation.beanvalidation.spring.constraints. Thus you will need to check the value agains the pattern in the code of constraint validator class.
You can configure bean validation using XML for such a thing. The syntax is a lot like the JavaBean XML format and maps rather well to the hierarchical structure of the equivalent Java syntax.
I've following problem, there's a regular spring model (let's call it "A") with some validations-related annotations. Next, there's a command object (regular POJO class that defines some field, one of them is object of type A). The command object implements Validator interface, to make binding and validation work in controller methods.
Question is, how to make use of annotations-configured validations inside the command object (given it implements Validator interface, hence it has supports() and validate() methods).
What I'm trying to achive is to have basic validations on model that is reused and mixed with some heavier business-logic validations in other parts of the system.
I have had the exact same problem. I wanted to use automatic annotation validation for "simple things" and then pass the complex validation logic to my custom spring Validator. But whenever I set the controller validator, all of hibernate's validation stopped working, as described at the end of this tutorial:
http://www.captaindebug.com/2011/07/applying-custom-spring-validator-to.html#.VQR0OI7F-gd
This technique should be used when you need to do ALL your
controller’s validation yourself, and you can’t or don’t want to make
use of the Hibernate’s reference implementation of a JSR 303
validator. From this, you’ll guess that you can’t mix your own custom
Spring validator with Hibernate’s JSR 303 validator. For example,
adding the built-in annotations to the Address command object will
have no effect:
You should forget about old style Spring Validator and delete "setInitBinder()" as described in this related question:
Spring MVC validator annotation + custom validation
You should then only rely on hibernate validation and JSR303.
To add a complex validation to your class (model), say you want to check two date fields - use a custom annotation constraint on class level as described in the link below.
https://docs.jboss.org/hibernate/validator/5.1/reference/en-US/html/validator-customconstraints.html#section-class-level-constraints
Hope this helps.
Best Regards,
Alexander
Once look at this may this help you
Using both JSR-303 and Traditional Bean Validation?. There i have given one example for custom validation for model using custom annotation.