swagger-codegen icon indicating copy to clipboard operation
swagger-codegen copied to clipboard

[Jaxrs-cxf] Add bean-level cascaded beanvalidation for pojos (@Valid) #4738

Open jfiala opened this issue 8 years ago • 9 comments

PR checklist

  • [x] Read the contribution guildelines.
  • [x] Ran the shell/batch script under ./bin/ to update Petstore sample so that CIs can verify the change. (For instance, only need to run ./bin/{LANG}-petstore.sh and ./bin/security/{LANG}-petstore.sh if updating the {LANG} (e.g. php, ruby, python, etc) code generator or {LANG} client's mustache templates)
  • [x] Filed the PR against the correct branch: master for non-breaking changes and 2.3.0 branch for breaking (non-backward compatible) changes.

Description of the PR

Implemented #4738 for CXF

Open issues: @Valid is currently also generated for properties of type (isPrimitiveType/^isContainer does not cover these):

  • java.lang.BigDecimal
  • javax.xml.datatype.XMLGregorianCalendar
  • org.joda.time.LocalDate
  • java.util.UUID

Please advise how we can best check for model-specific pojos?

In fact, there is only one valid @Valid-reference in Pet.java:

@Valid
private Category category = null;

jfiala avatar Mar 04 '17 21:03 jfiala

@wing328 can you pls advise regarding the open issues above? The question is how to best scope @Valid to be generated only for classes of the model, not for other classes.

jfiala avatar Mar 09 '17 18:03 jfiala

@jfiala My take is that {{#isPrimitive}} should cover the following as well

  • java.lang.BigDecimal
  • javax.xml.datatype.XMLGregorianCalendar
  • org.joda.time.LocalDate
  • java.util.UUID

so that the following should imply models:

{{^isContainer}}{{^isPrimitiveType}} ... {{/isPrimitiveType}}{{/isContainer}}

wing328 avatar Mar 12 '17 08:03 wing328

This is exactly what I am using at pojo.mustache:

{{^isPrimitiveType}}{{^isContainer}}{{#useBeanValidation}}
+  @Valid{{/useBeanValidation}}{{/isContainer}}{{/isPrimitiveType}}

PrimitiveType covers only the real primitive types and container the List/Map items. So I think we need a new flag to check for isModelType?

jfiala avatar Mar 12 '17 18:03 jfiala

@jfiala shouldn't primitive or not primitive cover it?

fehguy avatar Mar 12 '17 19:03 fehguy

@fehguy unfortunately not, see above listed mustache snippet, I actually used ^isPrimitiveType/^isContainer. The problem is that the above datatypes (BigDecimal, UUID etc.) are not considered primitives. On the other hand, probably it would be better to introduce a new flag to check if a datatype is a pojo of the model?

jfiala avatar Mar 12 '17 19:03 jfiala

So I think we need a new flag to check for isModelType?

I would rather update "isPrimitiveType" to include the additional items (e.g. uuid) you've listed.

Ref: https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#data-types

wing328 avatar Mar 13 '17 07:03 wing328

@wing328 any news about this PR? We also need this feature but seems that the PR is not yet merged / final.

olivierpaquet avatar Jan 31 '18 13:01 olivierpaquet

@olivierpaquet Thx for asking, but currently I'm too busy to rebase, if you like you can rebase and re-provide the PR.

jfiala avatar Jan 31 '18 13:01 jfiala

We were trying to use this plugin for open api 3, and the validation of cascading beans is failing as @Valid annotation is not added on cascaded pojos. Is this pull req merged to 3.X version for open api-3?

mohangadm avatar Jan 29 '20 16:01 mohangadm