Swagger-codegen: [Java*] BeanValidation for all Java languages

Created on 27 Oct 2016  路  21Comments  路  Source: swagger-api/swagger-codegen

Description

BeanValidation support has been added in #4003 for Java and jaxrs-cxf/jaxrs-cxf-client.
This needs to be added to all other Java languages

  • [x] jaxrs-spec
  • [x] jaxrs (Jersey2 + library jersey1)
  • [x] jaxrs-resteasy
  • [x] jaxrs-cxf-cdi
  • [x] spring (default library spring-boot + library spring-mvc, spring-cloud)
Related issues

PR Beanvalidation annotation for Java and CXF #4003
Issues with language jaxrs-spec #4509

Issues with language jaxrs-spec (see #4509)
  • the api is generated as class, but IMHO this should be an interface!?
  • Jaxrs Response is used instead of the real response data type
  • @ApiResponse uses wrong response type (the return type, not the response type of the contract)
  • src/gen/java is not added to the build path
  • "Attachment" is used for attachments, but no import is added for this (I think org.apache.cxf.jaxrs.ext.multipart.Attachment is referenced, but this is CXF-specific)

Update: moved all the issues with jaxrs-spec to #4509

Support for @DecimalMin/@DecimalMax

Also add support for @DecimalMin/@DecimalMax

  • [x] jaxrs-spec
  • [x] jaxrs (Jersey2 + library jersey1)
  • [x] jaxrs-resteasy
  • [x] jaxrs-cxf-cdi
Final Cleanup (PR #4635)
  • [x] move beanvalidation code to AbstractJavaJAXRSServerCodegen.java
    (so it will stay redundant in AbstractJavaJAXRSServerCodegen.java and SpringCodegen.java) or to AbstractJavaCodegen?
  • [x] Backport support for @DecimalMin/@DecimalMax
    backport to languages which already have beanvalidation annotation support:

    • [x] java

    • [x] jaxrs-cxf / jaxrs-cxf-client

Final Cleanup implemented in PR #4635

Java Feature Java

Most helpful comment

@jfiala

We would like to get support for server side code generation for jaxrs ( jersey2) with JSR303 annotations. We are doing topdown approach of writing apis in swagger editor and generating the code. I would really appreciate if you prioritize the JSR303 addition to server code for jaxrs ( jersey2). Will it add support for custom annotation also ? Thanks.

All 21 comments

@wing328 If you like I can cycle through the language templates and add (optional) beanvalidation support there. Or do you prefer the language maintainers to do the job?

@jfiala if you've time, please add the bean validation support. I'm sure the community and the template owner would appreciate your effort.

Small remark: if bean validation is an option, it should be activated by default (since you expect the generated code to follow the contract established by the swagger spec)

@jfiala

We would like to get support for server side code generation for jaxrs ( jersey2) with JSR303 annotations. We are doing topdown approach of writing apis in swagger editor and generating the code. I would really appreciate if you prioritize the JSR303 addition to server code for jaxrs ( jersey2). Will it add support for custom annotation also ? Thanks.

@deveshpujari @wing328 added beanvalidation support to jaxrs-spec, I also had to fix an issue with outer Enums. BeanValidation annotations are now active by default and can be turned off using the CLI parameter useBeanValidation.

Additional issues with jaxrs-spec:

  • the api is generated as class, but IMHO this should be an interface!?
  • Jaxrs Response is used instead of the real response data type
  • @ApiResponse uses wrong response type (the return type, not the response type of the contract)
  • src/gen/java is not added to the build path
  • "Attachment" is used for attachments, but no import is added for this (I think org.apache.cxf.jaxrs.ext.multipart.Attachment is referenced, but this is CXF-specific)

I suggest fixing those in a separate issue for jaxrs-spec.

@deveshpujari @wing328 added beanvalidation support to jaxrs (jersey2) and library jersey1, please take a look.

Issues with jaxrs (=Jersey)

  • maximum and minimum are double, but @Min/@Max allows only Integer values, I commented @Min/@Max out in the template beanValidation.mustache for now.
 - name: number
          type: number
          maximum: 543.2
          minimum: 32.1

For type number, the annotations should probably be @DecimalMin/@DecimalMax

@DecimalMin/@DecimalMax are for BigDecimal only, so IMHO we need to distinguish between integer and decimal datatypes to generate the correct annotations. Is there already a flag for to do this already available in the mustache templates?

Type number is mapped to BigDecimal.
If you talk about Float/Double formats, I don't know if @Min/Max or @DecimalXXX can be used. It probably depends on the validation framework used. So you should probably not use format: float with min and max in your swagger spec...

And I think @DecimalXXX works with Integer, Long, etc ...

@DecimalMin/DecimalMax is reserved for decimal values:
https://docs.oracle.com/cd/E19798-01/821-1841/gircz/index.html
"The value of the field or property must be a decimal value lower than or equal to the number in the value element. "

@Min/@Max are for integer values:
"The value of the field or property must be an integer value lower than or equal to the number in the value element. "

http://swagger.io/specification/
type=number => @DecimalMin/DecimalMax
type=integer => @Min/@Max

I added checks for isInteger/^isInteger and now we support @Min/@Max/@DecimalMin/@DecimalMax, thx for the hint.

That's not what I read here : http://docs.oracle.com/javaee/6/api/javax/validation/constraints/DecimalMin.html
A quick test should answer that 馃槃

Hm - Using @Min/@Max for integers appears cleaner to me, as @DecimalMin/@DecimalMax use a string for the limit.

@jfiala agreed

You probably also want @Min/@Max for isLong

common name integer/long = type integer => isInteger should include both format32 (integer) and format64 (long) (see http://swagger.io/specification/).

changed in jaxrs/jaxrs-spec and jaxrs-resteasy

common name integer/long = type integer => isInteger should include both format32 (integer) and format64 (long) (see http://swagger.io/specification/).

Looking at the code, it doesn't seem to be the case...

you're right, should I add support for isLong or we can we better add isTypeInteger (which could then correctly include integer + long)?

So far added beanvalidation annotation support to all Java languages listed at the top.
Pls advise regarding isInteger/isLong vs isTypeInteger then I can backport this to cxf/java as well.

Java beanvalidation has been fully implemented, further enhancements for @Valid are dealt with separate issues.

Was this page helpful?
0 / 5 - 0 ratings