fdoc icon indicating copy to clipboard operation
fdoc copied to clipboard

Error with date-time in response

Open alandotcom opened this issue 11 years ago • 10 comments

After generating a template for a controller action (and tests passing), I run the specs again. I have some JSON keys 'created-at' and 'updated-at' that point to date-time strings. Fdoc is is giving an error for one of these fields.

I can confirm that the string is in the ISO8601 format. I parsed the response, converted the string to a DateTime object, then back to ISO8601 format and it's the same exact string I started with.

JSON::Schema::ValidationError:
       The property '#/product-permissions/0/created-at' must be a date/time in the ISO-8601 format of YYYY-MM-DDThh:mm:ssZ or YYYY-MM-DDThh:mm:ss.ssZ in schema ae989cdc-e527-5940-9752-73dbd0160fee#

alandotcom avatar Jun 03 '13 21:06 alandotcom

Can you please provide a small minimal example of a spec file and a .fdoc file?

zachmargolis avatar Jun 03 '13 21:06 zachmargolis

I ran into similar issue when mocking my response with DateTime. Apparently iso8601 on DateTime does not format UTC with a 'Z'. You can use ActiveSupport::TimeWithZone instead.

require 'active_support/core_ext'
 => true

ActiveSupport.use_standard_json_time_format
 => true

DateTime.new(2013, 5, 28).to_json
 => ""2013-05-28T00:00:00+00:00""

DateTime.new(2013, 5, 28).in_time_zone('UTC').to_json
 => "\"2013-05-28T00:00:00Z\""

ActiveSupport.use_standard_json_time_format = false
 => false

DateTime.new(2013, 5, 28).in_time_zone('UTC').to_json
 => "\"2013/05/28 00:00:00 +0000\""

rorcraft avatar Jun 20 '13 01:06 rorcraft

Maybe you need to explicitly .iso8601 in your app?

require 'active_support/core_ext'
> DateTime.new(2013, 5, 28).in_time_zone('UTC').iso8601
 => "2013-05-28T00:00:00Z" 

zachmargolis avatar Jun 20 '13 01:06 zachmargolis

ActiveSupport.use_standard_json_time_format is true by default. If it is false, you'll need to call iso8601 But you need to convert it to TimeWithZone because iso8601 on DateTime doesn't replace UTC with 'Z'

DateTime.new(2013, 5, 28).iso8601
 => ""2013-05-28T00:00:00+00:00""

rorcraft avatar Jun 20 '13 16:06 rorcraft

I would try seeing if this worked, but getting another error now:

undefined method `each' for "???":String

This happens after generating the fdoc, then running the test again

alandotcom avatar Jun 25 '13 23:06 alandotcom

I ran in exactly the same problem.

Here is my .fdoc

---
description: Endpoint to get guest movements from a given date up today.
responseCodes:
- status: 404
  successful: false
  description: Request cannot be satisfied due to bad request. In the body, usually,
    there is a more detailed explanation about the reason of failure.
- status: 200
  successful: true
  description: Everything is OK.
requestParameters:
  properties: {}
responseParameters:
  properties: {}
  description: An array guests movements grouped by facility.
  type: array
  items:
    description: Guests movements on a single facility.
    required: true
    type: object
    properties:
      facility:
        description: Represent a facility.
        required: true
        type: object
        properties:
          id:
            description: Facility ID.
            required: true
            type: integer
            example: 852
          description:
            description: Facility description.
            required: false
            type: string
            example: Villa Felice
      guests_movements:
        description: A list of guests movements inside the facility.
        required: true
        type: array
        items:
          description: A list of movements by single guest and ward inside the facility.
          required: true
          type: object
          properties:
            guest_id:
              description: The guest ID.
              required: true
              type: integer
              example: 1650
            movements:
              description: A list of movements of the guest inside the ward.
              required: true
              type: array
              items:
                description: "???"
                required: true
                type: object
                properties:
                  date:
                    description: "???"
                    required: true
                    type: string
                    format: date-time
                    example: '2014-10-24T10:29:00+02:00'

And here is a sample response which is considered to be invalid on date:

[{
    "facility": {
        "id": 852,
        "description": "RESIDENZIALE"
    },
    "guests_movements": [{
        "guest_id": 1650,
        "movements": [{
            "date": "2014-10-24T10:29:00+02:00"
        }, {
            "date": "2014-10-25T10:59:00+02:00"
        }]
    }, {
        "guest_id": 5414,
        "movements": [{
            "date": "2014-10-03T11:12:00+02:00"
        }, {
            "date": "2014-10-03T11:12:00+02:00"
        }]
    }]
}]

The date is valid and validates ok if I try to put it as the only property of the response.

The problem seems related to the position of the date attribute.

knightq avatar Dec 11 '14 10:12 knightq

Moreover, if I run the spec with scaffolding on (FDOC_SCAFFOLD=true) the field is recognised as format: date-time.

knightq avatar Dec 11 '14 10:12 knightq

This will get fixed if you update json-schema in gemspec to >= 2.5.0 per this change https://github.com/ruby-json-schema/json-schema/commit/4e84940d956052d162b89c41104158b073fdb574

sromano avatar Feb 10 '15 18:02 sromano

You might want to look at this changeset https://github.com/sromano/fdoc/commit/70f5ac3a2aab16e007c2fe7551e9e86acc7314c1

sromano avatar Feb 10 '15 19:02 sromano

Unfortunately, this commit 2e5f93a5689740e3df195d5ceebef56a291b9ebc makes the update of json-schema infeasible.

Any chance to get this fixes?

knightq avatar Jun 26 '15 10:06 knightq