Search code examples
node.jstypescriptswagger-uiswagger-2.0swagger-editor

getting error response for the Update api


I am new to this swagger and i created a small demo project in node-js to see how swagger will really works. I created 5 api's which 4 are working perfectly and when it comes to PUT api I am getting error ,but when i tried in postman it is working. Please look at the code below.

export let updateUser = async(req: Request, resp: Response) => {
        try{
            const use = await User.findById(req.params.id);
            use.name = req.body.name;
            // use.email = req.body.email;

            const a1 = await use.save();
            resp.json("successfully updated");

        } catch(err) {
            resp.send('Error')
        }
}

this is the api which is calling above method in app.ts

//put-request
app.put('/user/update/:id',controller.updateUser);

This is the the swagger json of put API

"/user/update/{id}": {
    "put": {
        "tags": [
            "Update-Api"
        ],
        "summary": "Update-user",
        "description": "To updatre the particular user",
        "operationId": "updateUser",
        "consumes": ["application/json"],
        "parameters":[
            {
                "name":"Id",
                "in":"path",
                "description":"enter the id of the user",
                "required":true,
                "type":"string"
            },
            {
                "name":"body",
                "in":"body",
                "description":"Enter the update value",
                "required":true,
                "$schema": {
                    "type": "#/definations/User"
                }
            }
        ],
        "responses": {
            "400": {
                "description": "Invalid user supplied"
            },
            "404": {
                "description": "User not found"
            }
        }
    }
}

Solution

  • If you paste your API definition into https://editor.swagger.io, it will flag 2 syntax errors in the PUT operation. Make sure to fix these errors before testing your API.

    1. In the parameter definition, change "name":"Id" to "name":"id" (lowercase id) to match the parameter letter case in the path template.

    2. In the body parameter, change $schema to schema.