Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to get response headers in case of no content #71

Open
szhajdu opened this issue May 5, 2022 · 3 comments
Open

Unable to get response headers in case of no content #71

szhajdu opened this issue May 5, 2022 · 3 comments
Labels
enhancement New feature or request

Comments

@szhajdu
Copy link
Collaborator

szhajdu commented May 5, 2022

When generating a client where response body is empty I'm not able to get the response object to check headers when needed.

Example

openapi: "3.0.0"
info:
  version: 1.0.0
  title: Swagger Petstore
  license:
    name: MIT
servers:
  - url: http://petstore.swagger.io/v1
paths:
  /pets/{petId}:
    parameters:
      - name: petId
        in: path
        required: true
        description: The id of the pet
        schema:
          type: string
    put:
      summary: Create a pet
      operationId: createPet
      tags:
        - pets
      responses:
        '204':
          description: No content
          headers:
            x-foo:
              description: Something important
              schema:
                type: string

Is there any other way to get response object after executing request in api clients?

@JasonBenett
Copy link
Collaborator

Hello,

There is a "hacky" way indeed. Instead of using the generated method createPet() you have a generic method sendRequest() which does not map the response to an entity but returns directly the ResponseInterface implementation.

@vsouz4
Copy link
Collaborator

vsouz4 commented May 5, 2022

Right now there's no "proper" support for retrieving headers from response, regardless of having body defined. Unless you call "sendRequest" manually and work with the PSR response object. (as suggested by @JasonBenett)

The name of the classes returned by the *ApiClient class are pretty tied to 200/201 (*ResponseBody). 204 meaning no content also means no body 😄 but still you'd need something to be returned so you could ->getHeaders() for example.

Ideally I guess we would need to introduce an extra layer (if we really want to be able to ->getHeaders() calling the operationName methods from *ApiClient). So that you'd call e.g. $myApiClient->getPet()->getBody() and $myApiClient->getPet()->getHeaders(), with getBody() returning the *ResponseBody which is currently returned under ->getPet() directly...

we could also tweak the existent *ResponseBody adding the headers there, although it feels weird to work with a concrete *ResponseBody object for a 204 response which has no body... so maybe we could rename the class, but also we could have issues with mixing something like "getHeaders()" which would return HTTP response headers with a "getHeaders()" from a response that has {"headers": ...} in the payload

So either using sendRequest manually as a workaround or we'd need to introduce an extra layer (even though this breaks backward compatibility, it's I guess safer and more consistent - you get Response and inside you have body and headers).

@szhajdu
Copy link
Collaborator Author

szhajdu commented May 5, 2022

Thank you guys, I’ll go with sendRequest for now.

I could also imagine a getLastResponse method, which would return always the response of the last executed API call. It could be useful in a few test scenarios as well but also would require introducing a state into the client.

@vsouz4 vsouz4 added the enhancement New feature or request label Dec 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants