Skip to content

Conversation

@huningd
Copy link
Contributor

@huningd huningd commented Feb 24, 2017

PR checklist

  • Read the contribution guildelines.
  • 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)
  • 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

If bean validation is activated this pull request adds @Valid to request body for the JavaSpring generator.

solves #4847
refs #4600

@wing328
Copy link
Contributor

wing328 commented Mar 6, 2017

@huningd thanks for the PR.

cc @cbornet @jfiala, please take a look when you've time.

@jfiala
Copy link
Contributor

jfiala commented Mar 6, 2017

@wing328 looks good, I copied from here to jaxrs-cxf, but I didn't add an additional shellscript/sample for now.

@kenjones-cisco
Copy link
Contributor

kenjones-cisco commented Mar 8, 2017

@huningd Does this also solve the issue #4738?

Just want to confirm if your solution does both request body validation and nested attribute validation in a single statement then great.

I had done something similar where I used org.springframework.validation.annotation.Validated at the API and APIController level. And then at the Model level, I applied javax.validation.Valid on each of the attributes. Feature: Enable validation I had plans to create the PR but required immediate solution for a really tight timeline.

Example:

  @ApiOperation(
    value = "Create Profile",
    notes = "Creates a new profile for the given application. ",
    response = ProfileStatusResponse.class,
    tags = {
      "Admin",
    }
  )
  @ApiResponses(
    value = {
      @ApiResponse(code = 201, message = "Profile created", response = ProfileStatusResponse.class),
      @ApiResponse(
        code = 400,
        message = "Invalid Request Error. Check error entity in the response body for details. ",
        response = ProfileStatusResponse.class
      ),
      @ApiResponse(
        code = 401,
        message = "Authentication Failure ",
        response = ProfileStatusResponse.class
      ),
      @ApiResponse(
        code = 403,
        message = "Authorization Failure ",
        response = ProfileStatusResponse.class
      ),
      @ApiResponse(
        code = 500,
        message = "Internal Server Error.  Check error entity in the response body for details. ",
        response = ProfileStatusResponse.class
      )
    }
  )
  @RequestMapping(
    value = "/apps/{id}/profiles",
    produces = {"application/vnd.cia.v1+json"},
    consumes = {"application/vnd.cia.v1+json"},
    method = RequestMethod.POST
  )
  default ResponseEntity<ProfileStatusResponse> createProfile(
      @ApiParam(value = "reference to the parent application", required = true) @PathVariable("id")
          String id,
      @ApiParam(value = "the profile data", required = true) @Validated @RequestBody
          Profile profile) {
    // do some magic!
    return new ResponseEntity<ProfileStatusResponse>(HttpStatus.OK);
  }

Then on the Model:

  /**
   * Profile identity, represented by a UUID
   *
   * @return id
   */
  @ApiModelProperty(
    example = "1b12555a-0e17-4f6f-a5fe-5fcc2be01c97",
    readOnly = true,
    value = "Profile identity, represented by a UUID"
  )
  @Valid
  public UUID getId() {
    return id;
  }

@JeffAtDeere
Copy link

@wing328 any update on this?

@rmharris157
Copy link

Any chance these should also work with @PathVariables, even if I have to add the annotations to the rendered code by hand?

@wing328
Copy link
Contributor

wing328 commented Mar 17, 2017

@huningd @JeffAtDeere I'll review again over the weekend.

( @JeffAtDeere I assume you've also reviewed the change and it looks good to you )

@wing328
Copy link
Contributor

wing328 commented Apr 13, 2017

I noticed tab instead of 4-spaces is used for code indentation in some templates. I'll file a separate PR to clean up those.

I'll also add samples/server/petstore/springboot-beanvalidation to the Travis CI

@wing328 wing328 merged commit 8cf0f70 into swagger-api:master Apr 13, 2017
@wing328
Copy link
Contributor

wing328 commented Apr 13, 2017

UPDATE: replaced tab with 4-space via #5389, new directory also added to pom.xml for Travis CI to test.

davidgri pushed a commit to davidgri/swagger-codegen that referenced this pull request May 11, 2017
* Adds Valid annotation for request body (swagger-api#4847)

If useBeanValidation is active, this change will add Valid annotation to ReqeustBody

* Adds generated samples for bean vaildation in spring boot (swagger-api#4847)

* Adds Valid import to Controller

* Adds generated code for bean validation change
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.

7 participants