How do I use the swagger models section?

22,376

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.

Share:
22,376
Thomas R.
Author by

Thomas R.

I am Javascript developer with following technologies: Reactjs, Loopbackjs, Nodejs, Express, strong-soap and also interested in Voice User Interfaces and RASA, Amazon Alexa, Google Assistant etc.

Updated on April 19, 2020

Comments

  • Thomas R.
    Thomas R. about 4 years

    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: {...}
    }