apiaryio / dredd

Language-agnostic HTTP API Testing Tool
https://dredd.org
MIT License
4.16k stars 280 forks source link

Warning "The property must be an int64 " even the property has type of integer (and format int64) #1116

Open lenertovalucie opened 5 years ago

lenertovalucie commented 5 years ago

Describe your problem

Dredd returns warning: The id property must be an int64 (current value is 5). The userStatus property must be an int32 (current value is 0).

fail: GET (200) /v2/user/testing duration: 352ms
fail: body: The id property must be an int64 (current value is 5).
body: The userStatus property must be an int32 (current value is 0).

request:
method: GET
uri: /v2/user/testing
headers:
    Accept: application/json
    User-Agent: Dredd/5.1.11 (Windows_NT 10.0.16299; x64)
    Content-Length: 0

body:

expected:
headers:
    Content-Type: application/json

body:
{
  "id": -62671663,
  "username": "adipisicing et",
  "firstName": "quis id cupidatat",
  "lastName": "occaecat sed",
  "email": "cillum officia Lorem",
  "password": "id sed anim sint",
  "phone": "elit magna deserunt qui",
  "userStatus": -18960148
}
statusCode: 200
bodySchema: {"type":"object","properties":{"id":{"type":"integer","format":"int64"},"username":{"type":"string"},"firstName":{"type":"string"},"lastName":{"type":"string"},"email":{"type":"string"},"password":{"type":"string"},"phone":{"type":"string"},"userStatus":{"type":"integer","format":"int32","description":"User Status"}}}

actual:
statusCode: 200
headers:
    date: Mon, 01 Oct 2018 15:17:28 GMT
    access-control-allow-origin: *
    access-control-allow-methods: GET, POST, DELETE, PUT
    access-control-allow-headers: Content-Type, api_key, Authorization
    content-type: application/json
    connection: close
    server: Jetty(9.2.9.v20150224)

body:
{
  "id": 5,
  "username": "testing",
  "firstName": "string",
  "lastName": "string",
  "email": "string",
  "password": "string",
  "phone": "string",
  "userStatus": 0
}

api-description.yml looks like this:

swagger: '2.0'
info:
  description: 'This is a sample server Petstore server.  You can find out more about Swagger at [http://swagger.io](http://swagger.io) or on [irc.freenode.net, #swagger](http://swagger.io/irc/).  For this sample, you can use the api key `special-key` to test the authorization filters.'
  version: 1.0.0
  title: Swagger Petstore
  termsOfService: 'http://swagger.io/terms/'
  contact:
    email: apiteam@swagger.io
  license:
    name: Apache 2.0
    url: 'http://www.apache.org/licenses/LICENSE-2.0.html'
host: petstore.swagger.io
basePath: /v2
tags:
  - name: pet
    description: Everything about your Pets
    externalDocs:
      description: Find out more
      url: 'http://swagger.io'
  - name: store
    description: Access to Petstore orders
  - name: user
    description: Operations about user
    externalDocs:
      description: Find out more about our store
      url: 'http://swagger.io'
schemes:
  - https
  - http
paths:
  '/user/{username}':
    get:
      tags:
        - user
      summary: Get user by user name
      description: ''
      operationId: getUserByName
      produces:
        - application/xml
        - application/json
      parameters:
        - name: username
          in: path
          description: 'The name that needs to be fetched. Use user1 for testing. '
          required: true
          x-example: testing
          type: string
      responses:
        '200':
          description: successful operation
          schema:
            $ref: '#/definitions/User'
definitions:
  User:
    type: object
    properties:
      id:
        type: integer
        format: int64
      username:
        type: string
      firstName:
        type: string
      lastName:
        type: string
      email:
        type: string
      password:
        type: string
      phone:
        type: string
      userStatus:
        type: integer
        format: int32
        description: User Status
    xml:
      name: User
externalDocs:
  description: Find out more about Swagger
  url: 'http://swagger.io'

What's your dredd --version output?

dredd v5.1.11 (Windows_NT 10.0.16299; x64)
honzajavorek commented 5 years ago

@apiaryio/adt Could you please verify what is the problem? 5 is a valid int64 number and 0 is a valid int32 number, right? 🤔

lenertovalucie commented 5 years ago

Thank you for your quick answer. Yes, exactly. Both values ​(5 and 0) are valid integers, so where is the problem?

kylef commented 5 years ago

The value 5 is valid in the following schema tested by some validators I have tried:

{
  "type": "integer",
  "format": "int64"
}

It seems that the validator in Dredd thinks that 5 is not a valid int64. Here is minimal example to reproduce:

$ dredd test.apib https://httpbin.org

fail: body: The json,message property must be an int64 (current value is 5)
HOST: https://httpbin.org

# API Name

## POST /post

+ Request (application/json)
    + Attributes
        + message: 5 (number)

+ Response 200 (application/json)
    + Schema

            {
              "type": "object",
              "properties": {
                "json": {
                  "type": "object",
                  "properties": {
                    "message": {
                      "type": "integer",
                      "format": "int64"
                    }
                  }
                }
              }
            }
kylef commented 5 years ago

Actually, since "int64" is an extension to JSON Schema. It isn't designed the same everywhere. The "int64" format is for the string type in some (https://developers.google.com/discovery/v1/type-format) specs and implementations and I suspect that the validator is treating int64 as a string type whcih here it is a number. NOTE, the Swagger spec says it should be an integer which contradicts other definitions for the int64 format.

kylef commented 5 years ago

I think the best behaviour would be the validator in Dredd using type to know if it should be an integer or a string which should cover both cases.

artem-zakharchenko commented 4 years ago
  1. We should grab the JSON Schema example from the posts above and feed to AJV to see if it can digest int64 properly.
  2. If it does, raise the importance status of apiaryio/gavel.js#90
  3. If not, perhaps, file an issue in AJV.
honzajavorek commented 4 years ago
  1. Then perhaps offer a PR to the AJV to fix the issue.