raml-spec
raml-spec copied to clipboard
How to define a map in RAML 1.0
Hi,
i want to model an API with RAML 1.0. Within this API i have a map where the included objects have dynamic key values. It should look like the following:
"map" : {
"key1" : {
...
}
"key2" : {
...
}
"key3" : {
...
}
}
In this stackoverflow article i found a solution for the dynamic keys but now i am stuck. How can i create the map? For sure i can do a workaround with array, but this is not what i am looking for. Thanks in advance!
Cheers.
Don't do this. It's hard to parsing on all clients. You can write like this:
{
"map": [
{
"key": "key1"
....
},
{
"key": "key2"
....
},
{
"key": "key3"
....
}
]
}
It's simple for read and parsing.
What's hard with any particular interface library depends on the library's API (whether client or server).
What @stegmai is asking about isn't unreasonable, and may make the most sense for the application.
I'm not really sure what @stegmai's question is, though; it sounds like the question is about the application-side data objects.
According to https://github.com/raml-org/raml-spec/blob/master/versions/raml-10/raml-10.md/#property-declarations and to https://github.com/raml-org/raml-spec/blob/master/versions/raml-10/raml-10.md/#the-any-type
following should work:
#%RAML 1.0 Library
types:
TypeWithMap:
properties:
name: string
map:
properties:
//: any
Regards, Pavel
I solved a similar problem by creating a simple type named kevValuePair that has two properties: "key", "value". Then I create another type called "map" which has its type as "keyValuePair[]". I think that's what I did... I'll have to go back to look at it. 😀
Christopher (Cai) Black Web Application Architect VFX Artist [email protected] 832-439-8134
Sent from my iPhone, so, please, pardon any obviously ridiculous statements.
On Sep 14, 2016, at 3:01 PM, petrochenko-pavel-a [email protected] wrote:
According to https://github.com/raml-org/raml-spec/blob/master/versions/raml-10/raml-10.md/#property-declarations and to https://github.com/raml-org/raml-spec/blob/master/versions/raml-10/raml-10.md/#the-any-type
following should work:
#%RAML 1.0 Library types: TypeWithMap: properties: name: string map: properties: //: any Regards, Pavel
— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or mute the thread.
I think a plain object
without any properties defined works like a map. Because additionalProperties
is allowed by default.
Ref: https://github.com/raml-org/raml-spec/blob/master/versions/raml-10/raml-10.md/#additional-properties