apiaryio / api-blueprint

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

Ability to reference repeated data #236

Closed foxx closed 9 years ago

foxx commented 9 years ago

Imagine the following scenario;

## Say hello [POST /say/hello]

Say hello to someone

+ Request (application/json)

    + Attributes
        + message (boolean) - Your hello message

    + Body

            {"message": "Japie, start the nederdraad!"}

    + Schema

            {}

+ Response 200 (application/json)
    + Body

            {}
    + Schema

            {}

+ Response 400 (application/json)
    + Body

            {"errors": [
                {"message": "some error", "code": 1},
                {"message": "some error", "code": 1}
            ]}
    + Schema

            {}

Lets assume that 4xx response will always contain the same data structure for all API endpoints. Rather than having to repeat this data structure definition multiple times, it would be useful to define it once in the document and then reference to it. Models do not work for this, because they require that you define a path, which doesn't really make much sense (as a data structure is independent of its resource path, right?).

I've had a read through some other issues which feel related, including #29, #25, #9 and #19, but none of them seem to provide any solution or firm outcome.

Could someone please clarify the correct way of achieving the above goal?

pksunkara commented 9 years ago

I think you are looking for MSON with which you can do the above.

foxx commented 9 years ago

MSON could potentially help, but I don't want to use MSON in this particular situation :) Although that being said, it's unclear how MSON would have actually helped, so any links would be appreciated

pksunkara commented 9 years ago

I don't know why you do not want to use MSON, but MSON was designed exactly with this use case in mind. Could you explain more?

kylef commented 9 years ago

@foxx Just to be clear, @pksunkara is proposing you do something like follows. Where you are defining reusable components you can use in multiple different resources/actions.

## Say hello [POST /say/hello]
+ Response 400 (application/json)
    + Attributes (Errors)

## Data Structures
### Errors
+ errors (array[Error])

### Error
+ message: some error
+ code: 1 (number)

Which would get expanded into the following JSON example in Apiary:

screen shot 2015-07-22 at 14 49 21

foxx commented 9 years ago

Oh interesting, actually yes you're quite right this would solve the issue.

I couldn't see any mentioning of this in the examples or documentation, at least not clearly, did I miss something?

pksunkara commented 9 years ago

There are a few examples in the example folder such as, Attributes, Advanced Attributes and Data Structures

foxx commented 9 years ago

Thanks for the quick reply and additional info, appreciated