Skip to content

[PHP] lib/Api constructors ignore configuration and set host to localhost  #4518

@philip

Description

@philip
Description

The constructors within lib/Api/*.php ignore the host definition in lib/Configuration.php, whether it's set within lib/Configuration.php itself or by using Swagger\Client\Configuration::getDefaultConfiguration()->setHost(https://example.org/); in the code.

Either way, the likes of $foo = new Swagger\Client\Api\Bar() always resets host to https://localhost.

Swagger-codegen version

2.2.1

Command line used for generation
swagger-codegen generate \
 --input-spec ./swagger.json \
 --lang php \
 --output /tmp/foo
Steps to reproduce
  1. Use swagger_codegen to generate client, see above
  2. Edit lib/Configuration.php by setting the $host there
  3. Copy and edit example code from generated README.md
  4. Optionally set host in code using Swagger\Client\Configuration::getDefaultConfiguration()->setHost('https://example.org/); -- generated code already does this for other stuff like username / password, which is why it's expected that this would work fine
  5. Execute
  6. Fails, as it attempts to connect to localhost instead of my desired host
Suggest a Fix

Code in the generated constructors looks like this:

        if ($apiClient == null) {
            $apiClient = new ApiClient();
            $apiClient->getConfig()->setHost('https://localhost');
        }

One option is to change it like so:

        if ($apiClient == null) {
            $apiClient = new ApiClient();
            if (empty($apiClient->getConfig()->getHost())) {
                $apiClient->getConfig()->setHost('https://localhost');
            }
        }

With that change, host can be set in lib/Configuration.php or within the code itself via the aforementioned setHost(), and not reset to localhost.

That said, I'm not familiar enough with the codebase to know if that's ideal, but it's working for me. But, defining the default host like this in multiple files feels odd so another solution is to define it somewhere else (like lib/Configuration.php) instead of within each constructor.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions