[BUG][php] json_decode missing when response is object
Created by: ybelenko
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? -
What's the actual output vs expected output? -
[Optional] Sponsorship to speed up the bug fix or feature request (example)
Description
When I call API via generated PHP client I got encoded json string instead of decoded object. Response looks like:
["{\"foobar\":\"foobaz\"}"]
openapi-generator version
5.0.0-beta
OpenAPI declaration file content or url
paths:
'/objects/{id}':
parameters:
- name: id
in: path
required: true
get:
operationId: getSingleObjectById
responses:
'200':
description: ok
content:
application/json:
schema:
$ref: '#/components/schemas/FreeFormObject'
components:
schemas:
FreeFormObject:
type: object
Generation Details
generate -i \"spec.yaml\" -g php -o \"api-client-sdk\"
Steps to reproduce
Send request via generated SDK to staged server /objects/1
where response is correct JSON item. The response will be json encoded string instead of object.
Related issues/PRs
#6566 Seems close but not really. I'm ok with response as object instead of model. It's just json_decode
execution missed.
Suggest a fix
diff --git a/modules/openapi-generator/src/main/resources/php/ObjectSerializer.mustache b/modules/openapi-generator/src/main/resources/php/ObjectSerializer.mustache
index 58200ac713..42fd2658d1 100644
--- a/modules/openapi-generator/src/main/resources/php/ObjectSerializer.mustache
+++ b/modules/openapi-generator/src/main/resources/php/ObjectSerializer.mustache
@@ -294,6 +294,7 @@ class ObjectSerializer
}
if ($class === 'object') {
+ $data = is_string($data) ? json_decode($data) : $data;
settype($data, 'array');
return $data;
}
I don't know why json_decode
executes based on string check and doesn't check Content-Type
header but it's a quick fix.