swagger-api / validator-badge

Validate your Swagger JSON/YAML today!
http://swagger.io
Apache License 2.0
210 stars 85 forks source link

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

Open woodcockjosh opened 9 years ago

woodcockjosh commented 9 years ago

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 commented 9 years ago

Seems to be working now

woodcockjosh commented 9 years ago

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

webron commented 9 years ago

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

fehguy commented 9 years ago

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 commented 9 years ago

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 commented 9 years ago

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

webron commented 9 years ago

@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.

JathinSanghvi commented 8 years ago

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

+1 to fix it

fehguy commented 8 years ago

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

JathinSanghvi commented 8 years ago
@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