Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for JSR-303 (Java Bean Validation) ? #356

Closed
omayevskiy opened this issue Jul 8, 2014 · 10 comments
Closed

Support for JSR-303 (Java Bean Validation) ? #356

omayevskiy opened this issue Jul 8, 2014 · 10 comments
Labels
Milestone

Comments

@omayevskiy
Copy link

Hi is there any support for JSR-303 (Java Bean Validation) ?

Given

A controller method

import javax.validation.Valid;
@RequestMapping(method = RequestMethod.PUT)
    public void create( @RequestBody @Valid MyDTO myDTO) {
        // do something with myDTO
    }

A command object

import javax.validation.constraints.NotNull;
public class MyDTO {

    @NotNull
    private Long myId;

    @NotNull
    private String myValue;

Swagger produces

MyDTO {
   myId (integer, optional),
   myValue (string, optional)
}

But should be

MyDTO {
   myId (long?, required),
   myValue (string, required)
}

I could add swagger annotations to MyDTO's fields, but then the NotNull/required logic would be duplicated and potentially leads to documentation inconsistence.

Thx for reading ;-)

@acwatson
Copy link
Contributor

In the very latest 0.8.6-SNAPSHOT, you can get JSR 303 validation working. Here is a sample controller:

@RestController
@RequestMapping(value = "/api/person")
@Api(value = "Person")
public class PersonController {

@RequestMapping(value = {"/blah"}, method = POST, produces = APPLICATION_JSON_VALUE)
@ApiOperation(value = "Creates and returns a Person",
notes="Populates a Person instance using the data supplied in the posted form fields")
public PersonResponse createPerson(
        @ModelAttribute @Valid
        final PersonForm person, final BindingResult result) {

    if (result.hasErrors()) {
        ...
    }
    return null;
}

}

@omayevskiy
Copy link
Author

How does the Person looks like in your example?
We cannot confirm it is working (version 0.8.8)
We still need to add a manual swagger annotation to make it work like:

import javax.validation.constraints.NotNull;
import com.wordnik.swagger.annotations.ApiModelProperty;
class Person {
      @NotNull
      @ApiModelProperty(required = true)
      private String foo;
}

@dilipkrish dilipkrish modified the milestone: 2.0 Dec 2, 2014
@dilipkrish dilipkrish modified the milestones: 2.0, 2.1 Apr 6, 2015
@jasonchaffee
Copy link

It would be nice if it could handle this not only in the model, but for Query, Path, Matrix params as well. Also, it would nice if it would handle more than @NotNull and could handle the other annotations as part of the spec. See http://docs.oracle.com/javaee/7/api/javax/validation/constraints/package-summary.html

Then, the clients of the swagger spec, such as the swagger-ui could validate the fields as they are entered without needing to make a call to the server.

@adrianbk
Copy link
Member

Relates to this issue: #311

As far as I can tell none of the JSR-303 constraint's are supported by springfox (including @NotNull).
It should be relatively straight forward to offer rudimentary support for these constraints but we would have to provide a way to disable the feature because there are scenarios, such as using groups to conditionally apply validation rules based on runtime conditions, where it is not feasible to try to infer validation constraints.

@jasonchaffee would you be interesting in contributing to your idea?

@dilipkrish
Copy link
Member

I prefer this to be separate module springfox-validations. Should be simple enough to use the plugin models to enrich the service descriptions.

Don't want it to be a concern in the core modules, just because it can be released incrementally and possibly out-of-band.

@jasonchaffee
Copy link

I agree that it makes sense to be in a separate module.

On Apr 13, 2015, at 5:04 PM, Dilip Krishnan notifications@github.com wrote:

I prefer this to be separate module springfox-validations. Should be simple enough to use the plugin models to enrich the service descriptions.

Don't want it to be a concern in the core modules, just because it can be released incrementally and possibly out-of-band.


Reply to this email directly or view it on GitHub #356 (comment).

@jasonchaffee
Copy link

If I can get some extra time I can take a stab at it. It isn’t a high priority for our use right now, so that might limit the time I will have to look into it. If that were to change, I would definitely take it on.

On Apr 13, 2015, at 4:53 PM, Adrian Kelly notifications@github.com wrote:

Relates to this issue: #311 #311
As far as I can tell none of the JSR-303 constraint's are supported by springfox (including @NotNull https://github.com/NotNull).
It should be relatively straight forward to offer rudimentary support for these constraints but we would have to provide a way to disable the feature because there are scenarios, such as using groups https://docs.jboss.org/hibernate/validator/4.0.1/reference/en/html_single/#validator-usingvalidator-validationgroups to conditionally apply validation rules based on runtime conditions, where it is not feasible to try to infer validation constraints.

@jasonchaffee https://github.com/jasonchaffee would you be interesting in contributing to your idea?


Reply to this email directly or view it on GitHub #356 (comment).

@dilipkrish dilipkrish modified the milestones: 2.2.0, 2.3.0 Aug 11, 2015
@NarendraPeddineni
Copy link

is the support for JSR-303 (Java Bean Validation) available ?

@jfiala
Copy link
Contributor

jfiala commented Nov 11, 2015

I added a sample implementation of NotNull and Min/Max to issue #987.

@jfiala
Copy link
Contributor

jfiala commented Jan 30, 2016

As of PR #1159 the annotations @NotNull, @min, @max, @SiZe are supported.

Open questions:

  • Please let me know if I also should add support for the @Email (resolving to type "string").
  • Pls also let me know what is the best way to add test cases for this in the petstore?
    (where should I put them?)
  • The max-value "infinity" is currently not correctly dealt with, if it is the usage of the MAX-values of Integer/Double could be replaced with infinity (commented in BeanvalidationModelPropertyBuilderPlugin)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

7 participants