Configure Swagger javax validation Constraints on DTOs

11,084

Latest, as of now, Swagger-Core version 1.5.19 perfectly supports this:

DTO object similar to this:

public class SampleDTO {

    @Min(value = 5)
    @Max(value = 10)
    @NotNull
    private Integer integer;

    @NotNull
    private String string;

    //...

}

Will generate swagger.json similar to this:

...

 "definitions" : {
    "SampleDTO" : {
      "type" : "object",
      "required" : [ "integer", "string" ],
      "properties" : {
        "integer" : {
          "type" : "integer",
          "format" : "int32",
          "minimum" : 5,
          "maximum" : 10
        },
        "string" : {
          "type" : "string"
        },

...
Share:
11,084

Related videos on Youtube

JJ Zabkar
Author by

JJ Zabkar

Senior / Lead / Principal software engineer with tendencies including: Java, Spring Framework (MVC, Boot, Batch, Security, Data, Cloud, Reactor, Flux), Python, Vue.js, Angular, Docker, Node.js, MongoDB, Amazon Web Services (EC2, ECS, ELB, IAM, RDS, S3, others), Heroku, Google App Engine, Vuetify, Bootstrap, Distributed Systems, Arduino, Raspberry Pi, and single-malt highlands/Speyside scotch.

Updated on July 05, 2022

Comments

  • JJ Zabkar
    JJ Zabkar almost 2 years

    I'm using Swagger (1.5.8). I want my swagger.json definition to detect the javax.validation JSR-303 annotations on my DTOs, so that I can document my API's validation constraints.

    I would expect a @Min annotation (like this example) would show something about the minimum value (44), but it does not.

    @POST
    @ApiOperation(value = "post", httpMethod = "POST")
    public Response post(
            @QueryParam("id") @NotNull @Min(44) Integer id) {...}
    

    The resulting swagger.json for this is:

    "/foo": {
      "post": {
        "operationId": "post",
        ...
        "parameters": [
          {
            "in": "body",
            "name": "id",
            "description": "id",
            "required": false,
            "schema": {
              "type": "integer",
              "format": "int32"
            }
          }
    

    Swagger has closed the pull request for this functionality, but I'm unclear where/how it is able to be consumed in the Swagger definition.

    I want to be able to do things like:

    FooController

    @POST
    public void postFoo(@Valid @RequestBody FooDTO fooDto) {...}
    

    FooDTO

    public class FooDTO {
        @NotNull
        @Size(min = 1, max = 100)
        private Integer myInt;
    }
    

    Desired/Expected swagger.json Output:

    "FooDTO": {
      "type": "object",
      "required": [
        "myInt"
      ],
      "properties": {
        "myInt": {
          "type": "number",
          "format": "integer",
          "minimum": "1",
          "maximum": "100",
     ...
    

    What's the preferred way to configure Swagger Modules/Plugins to enable things like ModelResolver and BeanValidator, so that they'll inspect the annotations on my DTO's?

    • chrishern
      chrishern
      Did you ever find a resolution to this?
  • MiguelMunoz
    MiguelMunoz over 5 years
    That's cool, but what if I want to use one of the other annotations. Is there a reference you can give me? I couldn't find it in the docs.
  • Ungeheuer
    Ungeheuer almost 4 years
    If you were to generate a DTO from the example swagger.json you show, would that DTO actually enforce those constraints?