OpenAPITools / OpenAPITools/openapi-generator

[BUG][PHP] Fix declaration parsing <array(type, type)> in description

Open
#10,184 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Wrong substr number in case of array<....>. There could be or 4 (map[) or 6 (array<) depending which keyword is used in $class.

https://github.com/OpenAPITools/openapi-generator/blob/1d648223e6fdc1adad8967db05d86db9fc49a581/modules/openapi-generator/src/main/resources/php/ObjectSerializer.mustache#L281

Additionally, if space is used between key and type (e.g. <array(string, int)>), the $subClass should be trimmed.

Here is how to do it without calling substr:

if (preg_match('/^(?:array|map)[<\[](.*)[>\]]$/', $class, $matchedValues)) { // for associative array e.g. array<string,int>
    $data = is_string($data) ? json_decode($data) : $data;
    settype($data, 'array');
    $inner = $matchedValues[1];
    $deserialized = [];
    if (false !== strpos($inner, ',')) {
        $subClass_array = explode(',', $inner, 2);
        $subClass = trim($subClass_array[1]);
        foreach ($data as $key => $value) {
            $deserialized[$key] = self::deserialize($value, $subClass, null);
        }
    }
    return $deserialized;
}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at modules/openapi-generator/src/main/resources/php/ObjectSerializer.mustache#L281 and inspect how declaration strings are parsed for map[...] and array<...>. Check the existing deserialization flow against array(string, int) and spacing around the inner type. Done means both declaration forms parse the intended subtype without leaving surrounding whitespace.

Written by the indexing model from the issue text.

Assessment

Tech stack
openapi, php
Domain
api
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.