apiaryio / api-blueprint

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

comments on request body #316

Closed Alexorz closed 8 years ago

Alexorz commented 8 years ago

Hello guys, is there any syntax for adding some comments into the request body?

Sometimes I need to make some notes on some params in the JSON of POST request body, just like this:

+ Request (application/json)

        {
            "question": "Favourite programming language?",
            "answer": "JavaScript",     // value of `answer` must be one of these options: "Swift", "Python", "Objective-C", "Ruby", "JavaScript"
        }

But I found it will make the request body becomes an invalid JSON string in apiary.io or in dredd.

Here is the request body which was generated by apiary.io, it didn't cut out the content after //: image

And dredd threw error: fail: body: Can't validate. Expected body Content-Type is application/json but body is not a parseable JSON: Parse error on line 1:

So, Is there any solutions? Or, am I doing right?

Thank you.

kylef commented 8 years ago

We can go one step further and actually document it using MSON and explicitly mark this value as an enum, for example:

+ Request (application/json)
    + Attributes
        + question: `Favourite programming language?` (required)
        + answer: JavaScript (enum, required)
            + Swift
            + JavaScript
            + Ruby

Using MSON, you can attach descriptions and other type-information.

With our new beta for attributes in Apiary (opt-in), you can view this as follows:

screen shot 2016-03-01 at 18 08 53

This will also generate a matching JSON Schema:

screen shot 2016-03-01 at 18 10 51

And render the example JSON payload from the example values:

screen shot 2016-03-01 at 18 10 55

Does this answer your question?

kylef commented 8 years ago

Also want to point out, using the enum MSON syntax, Dredd can then improve it's validation to enforce that values are inside the enum.

Alexorz commented 8 years ago

It works, that is pretty cool. Thank you :) And maybe I should read the doc more attentively next time...