swagger-api / swagger-api/swagger-codegen

[PHP] Bug with headers

Open
#9,989 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

You should not overwrite the headers

Here is the JSON: https://github.com/dansleboby/ubity-sdk-php/blob/master/swagger.json#L307

Suggest a fix/enhancement

The elseif for application/json overwrite the wanted headers in my YAML

I use

'application/json;version=1'

FIX:
Remove elseif and content

In: https://github.com/swagger-api/swagger-codegen/blob/47f355a24100dbf8f7cfa242a0ab8462774ac0a1/samples/client/petstore/php/SwaggerClient-php/lib/HeaderSelector.php

 /**
     * Return the header 'Accept' based on an array of Accept provided
     *
     * @param string[] $accept Array of header
     *
     * @return string Accept (e.g. application/json)
     */
    private function selectAcceptHeader($accept)
    {
        if (count($accept) === 0 || (count($accept) === 1 && $accept[0] === '')) {
            return null;
        } elseif (preg_grep("/application\/json/i", $accept)) {
            return 'application/json';
        } else {
            return implode(',', $accept);
        }
    }

    /**
     * Return the content type based on an array of content-type provided
     *
     * @param string[] $contentType Array fo content-type
     *
     * @return string Content-Type (e.g. application/json)
     */
    private function selectContentTypeHeader($contentType)
    {
        if (count($contentType) === 0 || (count($contentType) === 1 && $contentType[0] === '')) {
            return 'application/json';
        } elseif (preg_grep("/application\/json/i", $contentType)) {
            return 'application/json';
        } else {
            return implode(',', $contentType);
        }
    }

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the PHP HeaderSelector.php referenced in the issue and compare selectAcceptHeader and selectContentTypeHeader with the supplied Swagger JSON at line 307. Verify how an Accept value such as application/json;version=1 is handled, then confirm that the generated client preserves the requested header rather than replacing it with application/json.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
api
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.