Search code examples
swagger-2.0swagger-editor

How to represent custom object as parameter of an api in swagger-editor


I want to represent custom object as parameter of an api in swagger-editor. Lets say we are calling an api /postInfo/data

@RequestMapping(value = "/postInfo/data", method = RequestMethod.POST)
public Info requestProcessing(@RequestBody Info info)
{
    // Implementation
}

Above method contains Info model class as a parameter :

class Info
{
    private String id;
    private String name;
    private String desc;
}

How can this be represented in swagger editor as yaml document?


Solution

  • In Swagger 2.0, object schemas can be defined in the global definitions section:

    definitions:
      Info:
        type: object
        properties:
          id:
            type: string
          name:
            type: string
          desc:
            type: string
    

    and then $ref'ed from other parts of the spec.

    If an object is used an input for an operation, the operation needs to define a body parameter with a schema that corresponds to that object's schema.

    paths:
      /postInfo/data:
        post:
          consumes:
            - application/json
          parameters:
            - in: body
              name: body
              required: true
              schema:
                $ref: '#/definitions/Info'
          responses:
            200:
              description: OK