OpenAPITools / OpenAPITools/openapi-generator
[BUG][PHP] DateTime in query param passes DateTime object to Guzzle, causing error
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 (example)?
- Have you tested with the latest master to confirm the issue still exists?
- Have you searched for related issues/PRs?
Description
In this pull request (https://github.com/OpenAPITools/openapi-generator/pull/3984) the way that query params are parsed changed. Specifically this change:
By removing ObjectSerializer::toQueryValue DateTime objects are not converted to a string (toQueryValue calls toString on the input). This results in the DateTime object being passed to Guzzle, which results in this warning being issued (from this call https://github.com/guzzle/psr7/blob/1.6.1/src/functions.php#L600):
rawurlencode() expects parameter 1 to be string, object given
And then null is returned:
I've confirmed that master still contains the same template that was introduced in #3984: https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/php/api.mustache#L474-L501
openapi-generator version
This first shows up in 4.3.0, reverting to 4.2.3 fixes it.
OpenAPI declaration file content or url
A sample param in an OpenAPI 2.0 config like this will trigger this generation:
{
"name": "date",
"in": "query",
"description": "date filter",
"required": false,
"type": "string",
"default": "",
"format": "date"
}
Generation Details
I don't think this is relevant, but generating with this command:
./node_modules/.bin/openapi-generator-cli generate -i ./schema/openapi.json -g php -o ./ -c openapi-generator-config.json
openapi-generator-config.json resembles something like this:
{
"variableNamingConvention": "camelCase",
"srcBasePath": "src",
"packageName": "package",
"invokerPackage": "package",
"apiPackage": "Api",
"modelPackage": "Model"
}
Steps to reproduce
Generating a PHP client from an OpenAPI file (in our case it's OpenAPI 2.0, but as I understand the templates, it will happen with any version that OpenAPI generator supports) that has a "date" or "date-time" formatted query parameter definition will encounter this issue. The petstore example API has some "date" and "date-time" parameters, but they're form parameters, not query parameters, so this issue doesn't show up in those examples.
Suggest a fix
I think that non-collection query parameters still need to be passed through ObjectSerializer::toQueryValue so that toString is called on DateTime objects. I'm not sure how that impacts what #3984 was trying to fix though.
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 in modules/openapi-generator/src/main/resources/php/api.mustache around lines 474-501 and review ObjectSerializer::toQueryValue alongside the change in PR #3984. Generate a PHP client with a date or date-time query parameter using the documented command, then verify the generated client passes a string to Guzzle without the rawurlencode warning.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- php
- Domain
- api
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 45/100