IBM / openapi-to-graphql

Translate APIs described by OpenAPI Specifications (OAS) into GraphQL
https://developer.ibm.com/open/projects/openapi-to-graphql/
MIT License
1.61k stars 211 forks source link

fillEmptyResponses does not work for 204 responses that return a content type #448

Closed zbyte64 closed 2 years ago

zbyte64 commented 2 years ago

Describe the bug Unable to use an OpenAPI endpoint that has no return type.

To Reproduce Steps to reproduce the behavior:

  1. Define a swagger schema that contains a schema-less response,
  2. Server has a schema-less endpoint that responds with a 204 and application/json as the content type
  3. Use createGraphQLSchema to export a graphql executor with the option fillEmptyResponses: true
  4. Execute a query or mutation against endpoint
  5. Observe that the operation gets executed but that the endpoint returns with an error.

Expected behavior

Either for the operation to return successfully with text or undefined return type OR for createGraphQLSchema to fail early if responseContentType is undefined

Screenshots

Server will respond with:

{
    "stacktrace": [
        "TypeError: Cannot read property 'includes' of undefined",
        "    at /code/node_modules/dist/openapi-to-graphql/src/resolver_builder.js:472:38",
        "    at processTicksAndRejections (internal/process/task_queues.js:95:5)"
    ]
}

If it's swagger schema looks like:

"put": {
        "description": "Update a building type",
        "operationId": "update_building_type",
        "parameters": [
          {
            "in": "body",
            "name": "building_type",
            "schema": {
              "$ref": "#/definitions/BuildingType"
            }
          }
        ],
        "responses": {
          "204": {
            "description": "Success."
          },
          "401": {
            "$ref": "#/responses/UnauthorizedError"
          },
          "403": {
            "$ref": "#/responses/ForbiddenError"
          },
          "default": {
            "$ref": "#/responses/GenericError"
          }
        },

Additional context From what I've seen this happens when we call responseContentType.includes and responseContentType is undefined. This appears to happen when the source schema does not define a ref or schema.

zbyte64 commented 2 years ago

I looked into this more and I am was right that our API library responds with a content type for NO_CONTENT: https://github.com/spec-first/connexion/pull/322/files

🤦

Alan-Cha commented 2 years ago

Thanks for filing this issue and also providing a fix. Your suggestion is very sensible.