Skip to content

Commit 05b796a

Browse files
authored
Fix type hint for array in comments. (OpenAPITools#16926)
1 parent 58f058f commit 05b796a

File tree

2 files changed

+19
-13
lines changed

2 files changed

+19
-13
lines changed

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PhpSymfonyServerCodegen.java

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -407,13 +407,13 @@ public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<Mo
407407
for (CodegenParameter param : op.allParams) {
408408
// Determine if the parameter type is supported as a type hint and make it available
409409
// to the templating engine
410-
String typeHint = getTypeHint(param.dataType);
410+
String typeHint = getTypeHint(param.dataType, false);
411411
if (!typeHint.isEmpty()) {
412412
param.vendorExtensions.put("x-parameter-type", typeHint);
413413
}
414414

415415
if (param.isContainer) {
416-
param.vendorExtensions.put("x-parameter-type", getTypeHint(param.dataType + "[]"));
416+
param.vendorExtensions.put("x-parameter-type", getTypeHint(param.dataType + "[]", false));
417417
}
418418

419419
// Create a variable to display the correct data type in comments for interfaces
@@ -636,7 +636,7 @@ protected String toSymfonyService(String name) {
636636
}
637637

638638
protected String getTypeHintNullable(String type) {
639-
String typeHint = getTypeHint(type);
639+
String typeHint = getTypeHint(type, false);
640640
if (!typeHint.equals("")) {
641641
return "?" + typeHint;
642642
}
@@ -645,18 +645,24 @@ protected String getTypeHintNullable(String type) {
645645
}
646646

647647
protected String getTypeHintNullableForComments(String type) {
648-
String typeHint = getTypeHint(type);
648+
String typeHint = getTypeHint(type, true);
649649
if (!typeHint.equals("")) {
650650
return typeHint + "|null";
651651
}
652652

653653
return typeHint;
654654
}
655655

656-
protected String getTypeHint(String type) {
656+
protected String getTypeHint(String type, Boolean forComments) {
657657
// Type hint array types
658658
if (type.endsWith("[]")) {
659-
return "array";
659+
if (forComments) {
660+
//Make type hints for array in comments. Call getTypeHint recursive for extractSimpleName for models
661+
String typeWithoutArray = type.substring(0, type.length() - 2);
662+
return this.getTypeHint(typeWithoutArray, true) + "[]";
663+
} else {
664+
return "array";
665+
}
660666
}
661667

662668
// Check if the type is a native type that is type hintable in PHP
@@ -682,4 +688,4 @@ protected String getTypeHint(String type) {
682688
protected Boolean isModelClass(String type) {
683689
return Boolean.valueOf(type.contains(modelPackage()));
684690
}
685-
}
691+
}

samples/server/petstore/php-symfony/SymfonyBundle-php/Model/Pet.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ class Pet
7070
protected ?string $name = null;
7171

7272
/**
73-
* @var array|null
73+
* @var string[]|null
7474
* @SerializedName("photoUrls")
7575
* @Assert\NotNull()
7676
* @Assert\All({
@@ -81,7 +81,7 @@ class Pet
8181
protected ?array $photoUrls = null;
8282

8383
/**
84-
* @var array|null
84+
* @var Tag[]|null
8585
* @SerializedName("tags")
8686
* @Assert\All({
8787
* @Assert\Type("OpenAPI\Server\Model\Tag")
@@ -192,7 +192,7 @@ public function setName(?string $name): self
192192
/**
193193
* Gets photoUrls.
194194
*
195-
* @return array|null
195+
* @return string[]|null
196196
*/
197197
public function getPhotoUrls(): ?array
198198
{
@@ -202,7 +202,7 @@ public function getPhotoUrls(): ?array
202202
/**
203203
* Sets photoUrls.
204204
*
205-
* @param array|null $photoUrls
205+
* @param string[]|null $photoUrls
206206
*
207207
* @return $this
208208
*/
@@ -216,7 +216,7 @@ public function setPhotoUrls(?array $photoUrls): self
216216
/**
217217
* Gets tags.
218218
*
219-
* @return array|null
219+
* @return Tag[]|null
220220
*/
221221
public function getTags(): ?array
222222
{
@@ -226,7 +226,7 @@ public function getTags(): ?array
226226
/**
227227
* Sets tags.
228228
*
229-
* @param array|null $tags
229+
* @param Tag[]|null $tags
230230
*
231231
* @return $this
232232
*/

0 commit comments

Comments
 (0)