apiaryio / api-blueprint

API Blueprint
https://apiblueprint.org
MIT License
8.63k stars 2.14k forks source link

JSON Schema Array Of Anything #391

Closed garytaylor closed 7 years ago

garytaylor commented 7 years ago

Whist this problem originally showed up in drafter, the same can be seen in drafter.js or the apiary web service.

Given the following api blueprint document


FORMAT: 1A

# Data Structures API

# Group Coupons

## Coupon [/coupons/{id}]
A coupon contains information about a percent-off or amount-off discount you
might want to apply to a customer.

+ Parameters
    + id (string)
        The ID of the desired coupon.

### Retrieve a Coupon [GET]
Retrieves the coupon with the given ID.

+ Response 200 (application/json)
    + Attributes
        + id: 250FF (string, required)
        + created: 1415203908 (number) - Time stamp
        + tags (array)
            + id: 12345 (string, required)
            + name: "Tag1"

(which is taken from the api blueprint example 10 - but simplified to demonstrate the issue without distractions.)

The "tags" attribute - which is specified as an array - is output as an array in the json-schema, but the inner details are lost - so it is effectively an array of anything.

See the data below - which is taken straight from the apiary service web site - using "SHOW JSON SCHEMA" in the response.

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "properties": {
    "id": {
      "type": "string"
    },
    "created": {
      "type": "number",
      "description": "Time stamp"
    },
    "tags": {
      "type": "array"
    }
  },
  "required": [
    "id"
  ]
}

It can be seen that "tags" is valid if it is an array - it doesn't specify the types that are valid in the array or anything.

The same applies even if I use something simple like


+ Response 200 (application/json)
    + Attributes
        + id: 250FF (string, required)
        + created: 1415203908 (number) - Time stamp
        + tags (array[string])

This doesnt seem to be anything silly that I am doing wrong - ive even tried it with example 10 from the api blueprint repo - without editing it - and we get the same thing for the endpoint for the list of coupons.

This is literally a show stopper for us using API Blueprint as we cannot validate against it reliably and the project I am working on is an rspec matcher to allow us to do exactly that - so that our documentation is always the source of truth - for our customers and our internal tests. According to the MSON specifications, this is supposed to work, but it is not doing so for me at the moment.

Please can someone help ?

Thanks

Gary

w-vi commented 7 years ago

You can use fixed-type if you want to specify and array of something. https://github.com/apiaryio/mson/blob/master/MSON%20Specification.md#353-type-attribute Example, Array of Note(s): https://github.com/apiaryio/mson/blob/master/examples/api-blueprint.md

Hope that it solves your problem.

garytaylor commented 7 years ago

That has solved it - thank you @w-vi

Gary