Dynamic collection of json example in RAML

I like how RAML can dynamically refer to different schemas when declaring a resourceType type:

resourceTypes: - collection: get: responses: 200: body: application/json: schema: <<schema>> post: body: application/json: schema: <<schema>>Create responses: 200: body: application/json: schema: <<schema>> 

Here I can use it as

 /users: type: { collection: { schema: user } } 

and RAML will give me user schema responses from GET and POST, as well as a userCreate schema for sending POST requests. Cool! Now I can reuse the definition of my collection with many different schemas.

But now that I want to have a json example for everything, I was hoping to use <<schema>> var in another way to use "code reuse". I was hoping I could do

 resourceTypes: - collection: get: responses: 200: body: application/json: schema: <<schema>> example: examples/v1-<<schema>>.json post: body: application/json: schema: <<schema>>Create example: examples/v1-<<schema>>-create.json responses: 200: body: application/json: schema: <<schema>> example: examples/v1-<<schema>>.json 

but unfortunately this does not work. I get an error

 error: File with path "/examples/v1-%3C%3Cschema%3E%3E.json" does not exist 

So now I resorted to manually adding this to all my collections, and the /users example above was

 /users: type: { collection: { schema: user } } get: responses: 200: body: application/json: example: !include examples/v1-user.json post: body: application/json: example: !include examples/v1-user-create.json responses: 200: body: application/json: example: !include examples/v1-user.json 

For me, this is too much overhead to add examples. Especially when I want to repeat the pattern across many resources.

Question: Is there a way to do this?

+7
raml
source share
2 answers

No, this is not allowed in RAML 0.8 according to the specification. This may be permitted in future versions.

+2
source share

Since RAML is just a standard, I would first ask: Who / What throws this error? (I mean, which tool are you using?) Plus: Are you sure of the example (first)? It does not use !, therefore, it should not even intend to reach this non-existent file (I assume that you are using it! It is included in your original script, but it is not specified what to copy here).

In addition, and I know that you are not asking for this, but just in case: - You can pass 2 parameters (as a workaround), one for the circuit and the other for example (it is still overhead, but not THAT hardcoded) . - Do you know about the reserved parameters? Using this + "singular" or "pluralization" as the case may also help you in your enterprise reuse;) See http://raml.org/docs-200.html#parameters

0
source share

All Articles