swagger-api / swagger-api/swagger-codegen

[PHP] Bug generating list of maps results in "Uncaught Error: Class "int[]][" not found"

Open
#12,440 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

The generated PHP client code for the DTO in Java with the Swagger annotation is incorrect. The error occurs due to the improper handling of the nested map and array structure in the ObjectSerializer. This results in a PHP Fatal error: Uncaught Error: Class "int[]][" not found in …/lib/ObjectSerializer.php:299

The server code the nasty data structure comes from:

@ApiModelProperty
private List<Map<String, Set<Long>>> reactions;

The client code generated looks like this:

…
protected static $swaggerTypes = [
    'inserted' => 'int',
    'updated' => 'int',
    'message_id' => 'int',
    'reactions' => 'map[string,int[]][]'
];
…

The code extracted from the ObjectSerializer:

php > echo substr('map[string,int[]][]', 4, -1);
string,int[]][

As you can see its: int [ ] ] [ and not string , int [ ]

Swagger-codegen version

2.4.42

Swagger declaration file content or url

(for YAML code) or

"ReactionDTO" : {
  "type" : "object",
  "properties" : {
    "inserted" : {
      "type" : "integer",
      "format" : "int64",
      "readOnly" : true
    },
    "updated" : {
      "type" : "integer",
      "format" : "int64",
      "readOnly" : true
    },
    "messageId" : {
      "type" : "integer",
      "format" : "int64",
      "readOnly" : true
    },
    "reactions" : {
      "type" : "array",
      "items" : {
        "type" : "object",
        "additionalProperties" : {
          "type" : "array",
          "uniqueItems" : true,
          "items" : {
            "type" : "integer",
            "format" : "int64"
          }
        }
      }
    }
  }
},
Suggest a fix/enhancement

By counting the open and closed brackets, the correct position for splitting can be determined.

$class = 'map[string,int[]][]';
$openBrackets = 0;
$cutPos = 0;

$classWithoutMap = substr($class, 3);
for ($i = 0; $i < strlen($classWithoutMap); $i++) {
    if ($classWithoutMap[$i] === '[') {
        $openBrackets++;
    } elseif ($classWithoutMap[$i] === ']') {
        $openBrackets--;
    }
    if ($openBrackets === 0) {
        $cutPos = $i - 1;
        break;
    }
}
$inner = substr($class, 4, $cutPos);

echo $inner;

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 with modules/swagger-codegen/src/main/resources/php/ObjectSerializer.mustache, especially the nested map and array handling that produces lib/ObjectSerializer.php line 299. Reproduce generation from the provided ReactionDTO schema and inspect the generated type string. Done means the generated PHP client handles map[string,int[]][] without a fatal class-not-found error.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.