chrishubert / whatsapp-api

This project is a REST API wrapper for the whatsapp-web.js library, providing an easy-to-use interface to interact with the WhatsApp Web platform.
https://www.christophehubert.com
Other
720 stars 341 forks source link

swagger.json "/client/getContacts/{sessionId}" response 200 fix #247

Open kaua-alves-queiros opened 1 month ago

kaua-alves-queiros commented 1 month ago

I noticed that for the endpoint /client/getContacts/{sessionId}, the Swagger JSON is generating the following response for status 200: I have no idea why the swagger.json is being generated incorrectly or if generating the client SDK with NSwag will create another functional endpoint

generated swagger.json

"200": {
  "description": "OK"
}

This prevents the correct SDK generation via NSwag.

The solution I found is quite crude (completely ChatGPT), but for now, it works.

fix swagger.json

{
  "/client/getContacts/{sessionId}": {
    "get": {
      "tags": [
        "Client"
      ],
      "description": "",
      "parameters": [
        {
          "name": "sessionId",
          "in": "path",
          "required": true,
          "schema": {
            "type": "string"
          },
          "description": "Unique identifier for the session (alphanumeric and - allowed)",
          "example": "f8377d8d-a589-4242-9ba6-9486a04ef80c"
        }
      ],
      "responses": {
        "200": {
          "description": "OK",
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {
                  "success": {
                    "type": "boolean"
                  },
                  "contacts": {
                    "type": "array",
                    "items": {
                      "type": "object",
                      "properties": {
                        "id": {
                          "type": "object",
                          "properties": {
                            "server": {
                              "type": "string"
                            },
                            "user": {
                              "type": "string"
                            },
                            "_serialized": {
                              "type": "string"
                            }
                          }
                        },
                        "number": {
                          "type": "string",
                          "nullable": true
                        },
                        "isBusiness": {
                          "type": "boolean"
                        },
                        "isEnterprise": {
                          "type": "boolean"
                        },
                        "name": {
                          "type": "string"
                        },
                        "pushname": {
                          "type": "string"
                        },
                        "shortName": {
                          "type": "string"
                        },
                        "type": {
                          "type": "string"
                        },
                        "isMe": {
                          "type": "boolean"
                        },
                        "isUser": {
                          "type": "boolean"
                        },
                        "isGroup": {
                          "type": "boolean"
                        },
                        "isWAContact": {
                          "type": "boolean"
                        },
                        "isMyContact": {
                          "type": "boolean"
                        },
                        "isBlocked": {
                          "type": "boolean"
                        }
                      }
                    }
                  }
                }
              },
              "example": {
                "success": true,
                "contacts": [
                  {
                    "id": {
                      "server": "c.us",
                      "user": "123456789",
                      "_serialized": "123456789@c.us"
                    },
                    "number": "123456789",
                    "isBusiness": false,
                    "isEnterprise": false,
                    "name": "John Doe",
                    "pushname": "John",
                    "shortName": "John",
                    "type": "in",
                    "isMe": false,
                    "isUser": true,
                    "isGroup": false,
                    "isWAContact": true,
                    "isMyContact": true,
                    "isBlocked": false
                  },
                  {
                    "id": {
                      "server": "lid",
                      "user": "987654321",
                      "_serialized": "987654321@lid"
                    },
                    "number": "987654321",
                    "isBusiness": false,
                    "isEnterprise": false,
                    "name": "Jane Smith",
                    "pushname": "Jane",
                    "shortName": "Jane",
                    "type": "in",
                    "isMe": false,
                    "isUser": true,
                    "isGroup": false,
                    "isWAContact": true,
                    "isMyContact": true,
                    "isBlocked": false
                  }
                ]
              }
            }
          }
        },
        "403": {
          "description": "Forbidden.",
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/ForbiddenResponse"
              }
            }
          }
        },
        "404": {
          "description": "Not Found.",
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/NotFoundResponse"
              }
            }
          }
        },
        "422": {
          "description": "Unprocessable Entity.",
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/ErrorResponse"
              }
            }
          }
        }
      },
      "security": [
        {
          "apiKeyAuth": []
        }
      ]
    }
  }
}

bash command to build sdk


nswag openapi2csclient /input:.\swagger.json /output:WhatsappClient.cs /namespace:{YourNameSpace}.WhatsappClient /classname:WhatsappClient /OperationGenerationMode:SingleClientFromPathSegments
kaua-alves-queiros commented 1 month ago

I noticed that the same thing happens with the /contact/getChat/{sessionId} endpoint.

In the 200 response, the same issue is present in the swagger.json

"200": { "description": "OK" }

If I change it to something similar to the issue, that is, manually map everything, the SDK is generated normally.