OpenAPITools / OpenAPITools/openapi-generator
[BUG][PHP] Host config is ignored if operation declares servers in OpenAPI file
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 26.8k
- Forks
- 7.7k
- PR merge metrics
- PR metrics pending
Description
Bug Report Checklist
- Have you provided a full/minimal spec to reproduce the issue?
- Have you validated the input using an OpenAPI validator?
- Have you tested with the latest master to confirm the issue still exists?
- Have you searched for related issues/PRs?
- What's the actual output vs expected output?
- [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description
I am currently facing the issue, that the host that I set in the configuration object, which is passed to the api object, is never used. Instead the server address from the OpenAPI declaration file is used.
Here is a minimal example of the operation:
paths:
'/users/':
get:
responses:
'200':
description: User Found
content:
application/json:
schema:
$ref: '#/components/schemas/User'
servers: # <- this is responsible for the issue
- url: 'http://localhost:3000'
In the generated getUsersRequest method of the api class this results in the following code
$operationHosts = ["http://localhost:3000"];
if ($this->hostIndex < 0 || $this->hostIndex >= sizeof($operationHosts)) {
throw new \InvalidArgumentException("Invalid index {$this->hostIndex} when selecting the host. Must be less than ".sizeof($operationHosts));
}
$operationHost = $operationHosts[$this->hostIndex];
Imho this is a bug. It should always be possible to override the value of the specification file per config object.
If the servers value of an operation is missing, the getUsersRequest method would get the host like this:
$operationHost = $this->config->getHost();
openapi-generator version
v7.19.0
Suggest a fix
The relevant lines in the mustache template are those:
{{^servers.0}}
$operationHost = $this->config->getHost();
{{/servers.0}}
{{#servers.0}}
$operationHosts = [{{#servers}}"{{{url}}}"{{^-last}}, {{/-last}}{{/servers}}];
if ($this->hostIndex < 0 || $this->hostIndex >= sizeof($operationHosts)) {
throw new \InvalidArgumentException("Invalid index {$this->hostIndex} when selecting the host. Must be less than ".sizeof($operationHosts));
}
$operationHost = $operationHosts[$this->hostIndex];
{{/servers.0}}
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with modules/openapi-generator/src/main/resources/php/libraries/psr-18/api.mustache around lines 707-716, then compare the generated getUsersRequest method for an operation with servers against one without. Done means the host supplied through the configuration object can override the operation-level server URL while preserving the existing host-selection behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- php
- Domain
- api, tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 45/100