Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I model a key/value for which the key is not known in Swagger

Tags:

swagger

I have a simple JSON object that can contain key/values for which the exact values are not known upfront. They depend on some server side process. How do I model this in Swagger?

An example of the JSON would be:

... 
,message: "invalid length. Must be in between {min} and {max}" 
,attributes: {
  min: 0
  ,max: 6
}
...

Another example would be:

... 
,message: "You must fill out this field as well because {field} has a value" 
,attributes: {
  field: "age"
}
...
like image 928
Koen Peters Avatar asked Mar 25 '15 15:03

Koen Peters


People also ask

How do you pass multiple parameters in swagger?

If you are trying to send a body with multiple parameters, add an object model in the definitions section and refer it in your body parameter, see below (works with editor.swagger.io):

What is schema in swagger?

OpenAPI 3.0 data types are based on an extended subset JSON Schema Specification Wright Draft 00 (aka Draft 5). The data types are described using a Schema object. To learn how to model various data types, see the following topics: Data Types.


1 Answers

The following solution will only work with Swagger 2.0.

Define the model as described like this:

{
    "type": "object",
    "properties": {
        "message": {
            "type": "string"
        },
        "attributes": {
            "type": "object",
            "additionalProperties": {}
        }
    }
}

This describes attributes as a map of properties, where the value can be anything (string, number, array and even an object).

like image 65
Ron Avatar answered Jan 02 '23 11:01

Ron