raml-spec icon indicating copy to clipboard operation
raml-spec copied to clipboard

How to define a map in RAML 1.0

Open stegmai opened this issue 8 years ago • 5 comments

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.

stegmai avatar Sep 14 '16 12:09 stegmai

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.

eaglemoor avatar Sep 14 '16 13:09 eaglemoor

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.

freddrake avatar Sep 14 '16 14:09 freddrake

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

petrochenko-pavel-a avatar Sep 14 '16 20:09 petrochenko-pavel-a

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.

caiblack avatar Sep 15 '16 00:09 caiblack

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

techpavan avatar Dec 22 '17 10:12 techpavan