validator-badge icon indicating copy to clipboard operation
validator-badge copied to clipboard

Validator throws exception on non-string default values for model properties

Open woodcockjosh opened this issue 9 years ago • 10 comments

This will cause an error in the validator http://online.swagger.io/validator/debug?url=

{
  "properties" : {
    "success" : {
      "type" : "boolean",
      "default" : false
    }
  }
}

But this is ok:

{
  "properties" : {
    "success" : {
      "type" : "boolean",
      "default" : true
    }
  }
}

And so is this:

{
  "properties" : {
    "success" : {
      "type" : "boolean",
      "default" : "false"
    }
  }
}

woodcockjosh avatar Apr 26 '15 03:04 woodcockjosh

Seems to be working now

woodcockjosh avatar Apr 26 '15 03:04 woodcockjosh

If you want to check out my json file its here: http://dev.app.opentimeapp.com/docs/data/1.0.1.json

woodcockjosh avatar Apr 26 '15 03:04 woodcockjosh

The problem is generic with non-string default values for model properties. This is definitely a bug.

webron avatar May 07 '15 15:05 webron

We're now better handling the exception but the arrays are not yet supported.

http://online.swagger.io/validator/debug?url=http://dev.app.opentimeapp.com/docs/data/1.0.1.json

fehguy avatar May 19 '15 18:05 fehguy

Here is the culprit. In /connection/withList:

      {
        "parameters": [ 
          {
            "in": "query",
            "name": "list",
            "required": true,
            "type": "array",
            "default": [
              2,
              3
            ],
            "description": "The user id for the other user"
          }
        ]
      }

It doesn't like the default array. I'm actually not sure how you would set the default value for an array (you're also missing the inner type) but this seems to work fine:

      {
        "parameters": [
          {
            "in": "query",
            "name": "list",
            "required": true,
            "type": "array",
            "items": {
              "type": "integer",
              "format": "int32"
            },
            "description": "The user id for the other user"
          }
        ]
      }

This is something that belongs in swagger-core

fehguy avatar May 19 '15 20:05 fehguy

@webron how does one represent a default value of type array in the spec?

fehguy avatar May 19 '15 20:05 fehguy

@fehguy - https://github.com/swagger-api/swagger-spec/issues/343 I believe it should indeed be [1, 2]. 'default' is an 'Any' property that should be the same type as the type itself. We have similar bug in the pet store sample.

webron avatar May 20 '15 20:05 webron

same with minimum and maximum.. fix should be for all.

+1 to fix it

JathinSanghvi avatar May 19 '16 21:05 JathinSanghvi

@JathinSanghvi please provide a test case and we'll get this fixed.

fehguy avatar May 20 '16 16:05 fehguy

@GET
@Path("/petstore/{storeid}/pet/{petid}")
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public Physician getPet(@ApiParam @DefaultValue(value="0") @Min(0) @PathParam("storeid") int enterpriseid,
            @ApiParam @Min(value=1000000000) @PathParam("petid") int npi,
            @ApiParam @DefaultValue(value="false") @QueryParam("refresh") boolean refresh);

The above jaxrs 2.0 resource method will generate swagger.json

"paths": {
 "/petstore/{storeid}/pet/{petid}": {
    "get": {
"produces": 
[
    "application/json",
    "application/xml"
],
"parameters": 
[
{
    "name": "storeid",
    "in": "path",
    "required": true,
    "type": "integer",
    "default": 0
    "minimum": ​0.0,
    "format": "int32"
},
{

    "name": "petid",
    "in": "path",
    "required": true,
    "type": "integer",
    "minimum": ​1.0E9,
    "format": "int32"
},
    {
        "name": "refresh",
        "in": "query",
        "required": false,
        "type": "boolean",
        "default": false
    }
],

There will be few schema validaiton errors.. from http://editor.swagger.io/#/

  1. default cannot be integer.. needs to be string
  2. mimnum cannot be integer.. needs to be string (swagger-jaxrs is generating minimum as 0.0 even though type is an integer.. that also needs a fix)
  3. minimum cannot be ​1.0E9, .. needs to be string (swagger-jaxrs is generating minimum as 1.0E9 even though type is an integer.. should not be formatted.. as it fails validation)
  4. default cannot be false .. needs to be string

JathinSanghvi avatar May 20 '16 18:05 JathinSanghvi