How do I use the swagger models section?

Thomas R. picture Thomas R. · Mar 4, 2013 · Viewed 22k times · Source

Inside the Swagger API Documentation there is inside the json beside the apis array a model object entry but no documentation about it. How can I use this "models" part?

{
   apiVersion: "0.2",
   swaggerVersion: "1.1",
   basePath: "http://petstore.swagger.wordnik.com/api",
   resourcePath: "/pet.{format}"

   ...

   apis: [...]
   models: {...}
}

Answer

CodeGuru picture CodeGuru · Mar 4, 2013

Models are nothing but like your POJO classes in java which have variables and properties. In models section you can define your own custom class and you can refer it as data type.

If you see below

     {
        "path": "/pet.{format}",
        "description": "Operations about pets",
        "operations": [
            {
                "httpMethod": "POST",
                "summary": "Add a new pet to the store",
                "responseClass": "void",
                "nickname": "addPet",
                "parameters": [
                    {
                        "description": "Pet object that needs to be added to the store",
                        "paramType": "body",
                        "required": true,
                        "allowMultiple": false,
                        "dataType": "Pet"
                    }
                ],
                "errorResponses": [
                    {
                        "code": 405,
                        "reason": "Invalid input"
                    }
                ]
            }

Here in parameter section it have one parameter who's dataType is Pet and pet is defined in models as below

{
"models": {
    "Pet": {
        "id": "Pet",
        "properties": {
            "id": {
                "type": "long"
            },
            "status": {
                "allowableValues": {
                    "valueType": "LIST",
                    "values": [
                        "available",
                        "pending",
                        "sold"
                    ]
                },
                "description": "pet status in the store",
                "type": "string"
            },
            "name": {
                "type": "string"
            },
            "photoUrls": {
                "items": {
                    "type": "string"
                },
                "type": "Array"
            }
        }
    }
}}

You can have nested models , for more information see Swagger PetStore example

So models are nothing but like classes.