openapi-generator icon indicating copy to clipboard operation
openapi-generator copied to clipboard

[BUG][PHP] Serializer incorrectly tries to set 'mixed' as a type whilst deserializing.

Open jras-gh opened this issue 5 years ago • 4 comments

Bug Report Checklist

  • [x] Have you provided a full/minimal spec to reproduce the issue?
  • [x] Have you validated the input using an OpenAPI validator (example)?
  • [x] Have you tested with the latest master to confirm the issue still exists?
  • [x] Have you searched for related issues/PRs?
  • [x] What's the actual output vs expected output?
  • [ ] [Optional] Sponsorship to speed up the bug fix or feature request (example)

Description

ObjectSerializer::deserialize incorrectly tries to set the type of data to mixed when it's handling a property with mixed type. This results in a warning by the PHP runtime because 'mixed' is not supported by settype.

OpenAPI Generator Version

5.0.0

OpenAPI declaration file content or url

openapi: 3.0.3

info:
    title: Foo
    version: 1.0.0


paths:
    /foo:
        get:
            tags:
                - foo
            operationId: foo
            responses:
                "200":
                    description: OK
                    content:
                        application/json:
                            schema:
                                $ref: "#/components/schemas/Foo"
components:
    schemas:
        Foo:
            type: object
            properties:
                bar: {}

Generation Details

java -jar openapi-generator-cli.jar generate -g php -i foo.yaml -o foo

Steps to reproduce

Run the following script:

<?php

include __DIR__.'/foo/lib/ObjectSerializer.php';
include __DIR__.'/foo/lib/Model/ModelInterface.php';
include __DIR__.'/foo/lib/Model/Foo.php';

use \OpenAPI\Client\ObjectSerializer;
use \OpenAPI\Client\Model\Foo;

$foo      = (new Foo())->setBar([123, '123']);
$foo_json = \json_encode($foo);

echo $foo_json;

// Error will occur trying to deserialize from JSON into Foo.
$footwo = ObjectSerializer::deserialize($foo_json, Foo::class);

var_dump($footwo->getBar());

Will output

➜  ~ php test.php                    

{"bar":[123,"123"]}PHP Warning:  settype(): Invalid type in /home/jras/foo/lib/ObjectSerializer.php on line 335
PHP Stack trace:
PHP   1. {main}() /home/jras/test.php:0
PHP   2. OpenAPI\Client\ObjectSerializer::deserialize() /home/jras/test.php:18
PHP   3. OpenAPI\Client\ObjectSerializer::deserialize() /home/jras/foo/lib/ObjectSerializer.php:385
PHP   4. settype() /home/jras/foo/lib/ObjectSerializer.php:335
/home/jras/test.php:20:
array(2) {
  [0] =>
  int(123)
  [1] =>
  string(3) "123"
}

After applying the fix, the warning should be gone, e.g;

➜  ~ php test.php

{"bar":[123,"123"]}/home/jras/test.php:20:
array(2) {
  [0] =>
  int(123)
  [1] =>
  string(3) "123"
}

Related issues/PRs

None, that I could find.

Suggest a fix

Modify the default template for the ObjectSerializer to handle 'mixed' data early and let it just return the $data untyped.

As a side note; it seems that languageSpecificPrimitves array contains a lot of "primitives" that arent' really primitives, including but not limited to 'mixed'. This is confusing, but might be a BC-break to change because custom templates may be depending on it..

jras-gh avatar Mar 10 '21 12:03 jras-gh

Is there anyone looking at this? we've just stumbled upon this same issue, are you guys open for a PR to fix it?

emilianozublena avatar Aug 10 '21 15:08 emilianozublena

Same issue here, Object normalizer checks for type "mixed" before calling settype, but settype doesn't accept mixed as type.

ObjectNormalizer line 332

 /** @psalm-suppress ParadoxicalCondition */
        if (in_array($class, ['DateTime', 'array', 'bool', 'boolean', 'byte', 'double', 'float', 'int', 'integer', 'mixed', 'number', 'object', 'string', 'void'], true)) {
            settype($data, $class);
            return $data;
        }

PHP Set type

/**
 * Set the type of a variable
 * @link https://php.net/manual/en/function.settype.php
 * @param mixed &$var <p>
 * The variable being converted.
 * </p>
 * @param string $type <p>
 * Possibles values of <b>type</b> are:
 * </p><ul>
 * <li>
 * "boolean" (or, since PHP 4.2.0, "bool")
 * </li>
 * <li>
 * "integer" (or, since PHP 4.2.0, "int")
 * </li>
 * <li>
 * "float" (only possible since PHP 4.2.0, for older versions use the
 * deprecated variant "double")
 * </li>
 * <li>
 * "string"
 * </li>
 * <li>
 * "array"
 * </li>
 * <li>
 * "object"
 * </li>
 * <li>
 * "null" (since PHP 4.2.0)
 * </li>
 * </ul>
 * @return bool true on success or false on failure.
 */
function settype(mixed &$var, #[ExpectedValues(["bool", "boolean", "int", "integer", "float", "double", "string", "array", "object", "null"])] string $type): bool {}

davidrojo avatar Sep 13 '21 11:09 davidrojo

Beside the already mentioned pseudo type mixed, the following "types" in the list are also not acceptable by settype():

  • DateTime
  • byte
  • mixed (see above)
  • void

Pittiplatsch avatar Jul 19 '22 07:07 Pittiplatsch

Per our tests it seems to have been fixed in version 6 of OpenAPI generator. We'll be moving forward to that version ourselves. For anyone not able to upgrade to v6 (yet), we had simply patched the ObjectSerializer.mustache template to generate the following:

image

jras-gh avatar Jul 22 '22 10:07 jras-gh