Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Cacodaimon opened this issue Aug 8, 2024 · 0 comments

Comments

@Cacodaimon
Copy link

Cacodaimon commented Aug 8, 2024

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;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant