kiota icon indicating copy to clipboard operation
kiota copied to clipboard

Stream response for HEAD endpoint

Open bkoelman opened this issue 1 year ago • 5 comments

When a HEAD endpoint contains only responses without a body (which should always be the case), Kiota generates Task<Stream?> as the response type. Is that intentional? I would have expected the return type to be Task.

Repro steps:

{
  "openapi": "3.0.1",
  "info": {
    "title": "OpenApiTests",
    "version": "1.0"
  },
  "paths": {
    "/countries": {
      "head": {
        "tags": [
          "countries"
        ],
        "operationId": "headCountryCollection",
        "responses": {
          "200": {
            "headers": {
              "ETag": {
                "required": true,
                "schema": {
                  "type": "string"
                }
              },
              "Content-Length": {
                "required": true,
                "schema": {
                  "type": "integer",
                  "format": "int64"
                }
              }
            }
          },
          "304": {
            "headers": {
              "ETag": {
                "required": true,
                "schema": {
                  "type": "string"
                }
              }
            }
          },
          "400": {
            "description": "The query string is invalid."
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
    }
  }
}

Used command line:

dotnet kiota generate --language CSharp --class-name HeadersClient --namespace-name OpenApiKiotaEndToEndTests.Headers.GeneratedCode --output ./Headers/GeneratedCode --backing-store --exclude-backward-compatible --clean-output --clear-cache --log-level Error --openapi ../OpenApiTests/Headers/GeneratedSwagger/swagger.g.json

Generated client usage: image

bkoelman avatar Feb 27 '24 01:02 bkoelman

Thanks for raising this @bkoelman

Stream is used as a default if the response code is not in noContentStatusCodes. What we would need to do is update the handling code here to have HEAD requests have a void return.

https://github.com/microsoft/kiota/blob/437045d8adfb403972ca57a84626f34b7f57594f/src/Kiota.Builder/KiotaBuilder.cs#L1314

andrueastman avatar Feb 27 '24 09:02 andrueastman

I've just updated to v1.14, which changes the return type to Task. Can you confirm this was fixed as intended?

bkoelman avatar May 18 '24 01:05 bkoelman

Thanks for confirming. We'll close this one for now.

I believe this was resolved via https://github.com/microsoft/kiota/pull/4367/

andrueastman avatar May 20 '24 12:05 andrueastman

I was wrong about this, the bug is still there. But the situation got worse than before.

My OAS looks like this for the HEAD request:

        "responses": {
          "200": {
            "description": "The operation completed successfully.",
          },
          "304": {
            "description": "The fingerprint of the HTTP response matches one of the ETags from the incoming If-None-Match header.",
          },
          "400": {
            "description": "The query string is invalid."
          }

Today kiota makes the return type void because one of the statuses is 304 (which is obviously wrong). If I change my OAS to:

        "responses": {
          "200": {
            "description": "The operation completed successfully."
          }
        }

it generates Stream as return type again. So please reopen.

bkoelman avatar May 20 '24 13:05 bkoelman

Re-opening. The fix here would involve checking the request method(HEAD) rather than the responses.

andrueastman avatar May 20 '24 14:05 andrueastman