Skip to content

Mock API controller function "contacts. updateContactsComplete()" to handle Swagger defined PUT route #9

@nfloersch

Description

@nfloersch

The /contacts/complete/{contact_id}/ route in the YAML is defined here:

/contacts/complete/{contact_id}/:
   x-swagger-router-controller: contacts
    .
    .
    .
    put:
      summary: Update contact with full details
      description: Updates a contact entry with full details
      operationId: updateContactsComplete

The YAML file defines the route (/contacts/complete/), controller (x-swagger-router-controller: contacts), and function name (operationId: updateContactsComplete ) that will be called for the PUT verb.

For the contacts controller, we need to mock up a updateContactsComplete() function that takes in the parameters of the function as defined in the YAML, and responds with a JSON object that reflects those values back to the caller.

In the YAML the params are listed as:

parameters:
        - in: path
          required: true
          type: string
          name: contact_id
          description: The contact id.
        - in: body
          name: body
          schema:
            $ref: '#/definitions/contact_complete'

This can be done fixing the body of this placeholder function located in file:

function updateContactsComplete(req, res) {
  // variables defined in the Swagger document can be referenced using req.swagger.params.{parameter_name}
  var name = req.swagger.params.name.value || 'stranger';
  var hello = util.format('Hello, %s!', name);

  // this sends back a JSON response which is a single string
  res.json(hello);
}

To see an example solution to a similar problem to get you started, view issue #1

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions