Swagger-codegen: Add support for BeanValidation @Valid annotation on nested attributes

Created on 7 Feb 2017  路  9Comments  路  Source: swagger-api/swagger-codegen

Description

When useBeanValidation flag is true, it does not add @Valid annotation to attributes of type object. For instance, I have the class JoinDetails which has a field of type MemberProfile. When I validate joinDetails instance, the validator does not validate memberProfile because there is no @Valid annotation on it. It would be better if swagger-codegen can support this use cases either using extraAnnotation (i.e. x-extraAnnotation: "@Valid") in swagger config or automatically added during code generation.

Swagger-codegen version

2.2.2-SNAPSHOT

Swagger declaration file content or url
```yaml
JoinDetails:
type: object
required:
  - memberProfile
properties:
  memberProfile:
    $ref: '#/definitions/MemberProfile'
 MemberProfile:
type: object
required:
  - firstName
  - lastName
properties:
  firstName:
    type: string
    maxLength: 50
  lastName:
    type: string
    maxLength: 50
```
Command line used for generation

I'm using maven:

io.swagger
swagger-codegen-maven-plugin
2.2.2-SNAPSHOT

Steps to reproduce
Related issues
Suggest a Fix

Add support via x- extension or automatically detect complex object or entities from the model.

Java General Suggestion help wanted

All 9 comments

@jmiddleton thanks for the suggestion. For this particular case, I think @Valid should be added automatically.

cc @jfiala

There are two different use-cases for @Valid:
1.) Cascaded bean validation:
https://docs.jboss.org/hibernate/validator/5.0/reference/en-US/html/chapter-bean-constraints.html#example-cascaded-validation

This is the topic of the current issue.

2.) Cascaded method-level bean validation:
https://docs.jboss.org/hibernate/validator/5.1/reference/en-US/html/chapter-method-constraints.html#chapter-method-constraints

This is described in #4847 (including PR for Spring)

Overview of all the languages to be updated:

  • [ ] jaxrs-spec
  • [ ] jaxrs (Jersey2 + library jersey1)
  • [ ] jaxrs-resteasy
  • [ ] jaxrs-resteasy-eap
  • [x] jaxrs-cxf - PR #4738
  • [ ] jaxrs-cxf-cdi
  • [ ] spring (default library spring-boot + library spring-mvc, spring-cloud)

This also applies to jaxrs (Jersey2). I could provide a patch.

Is someone already working on the same issue for jaxrs-cxf? @michaelpaesold are you still working on a patch for jaxrs?

@olivierpaquet I did the PR in summer last year as you already seem to have discovered. But currently I'm not working on it, so feel free to catch up and get it pulled in...

@olivierpaquet I have rebased @jfiala 's changes on the current master. I will check now how to ensure that the Valid-annotation is only applied to types which are generated by the model (the problem mentioned in https://github.com/swagger-api/swagger-codegen/pull/4922).
When ready I'll submit a new pull-request.
See https://github.com/fabian-braun/swagger-codegen/commits/cxf_beanval_valid_pojo

@jfiala , would you do the review or maybe point me to someone else I could ask? If I can further support with anything let me know

@fabian-braun Sorry I'm totally occupied with other things, maybe @cbornet can take a look in the meanwhile?

Was this page helpful?
0 / 5 - 0 ratings