A
A
Anton Ivanov2016-03-22 00:50:50
API
Anton Ivanov, 2016-03-22 00:50:50

How to describe an object with unknown fields in RAML?

Hello.
I am writing api documentation on raml, there is an object of the following type:
{id: 1, name: 'Example', additional: {some_key: some_value, one_more_key: one_more_value}}
it is clear that id has an integer type, name has a string, but how to describe additional field? Like and object, but at objects it is necessary to register fields. And here the fields can be absolutely any. The only limitation is that the values ​​are strings.
That is, it is such a "simple" object. Values ​​cannot contain arrays or other objects.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Stanislav Makarov, 2016-03-22
@Fly3110

https://github.com/raml-org/raml-spec/blob/raml-10...
Maps (aka Dictionaries) can be declared by creating an object type and declaring a special property called "[]":

#%RAML 1.0
title: My API With Types
types:
  MapOfNumbers:
    type: object
    properties:
      []:
        type: number

In your case, I think it will be enough to replace type: numberwith type: string. Look at additionalProperties, from the same opera.

A
Alexander Malikov, 2017-01-06
@malikov_a

#%RAML 1.0
title: Example API
version: v1

types: 
  Dictonary_1:
    type: array
    items: string
      
  Dictonary_item:
    type: object
    properties: 
      name:
        type: string
      value:
        type: string
        
  Dictonary_2:
    type: array
    items: Dictonary_item

/Dictonary_1:
  post:
    body: 
      application/json:
        type: Dictonary_1
        example: |
          [
            "item1",
            "item2",
            "item3"
          ]
          
/Dictonary_2:
  post:
    body: 
      application/json:  
        type: Dictonary_2
        example: |
          [
            {
              "name": "name1",
              "value": "item1"
            },
            {
              "name": "name2",
              "value": "item2"
            }
          ]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question