Fix type hint for array in comments. (#16926)

This commit is contained in:
Artem 2023-10-28 14:30:16 +02:00 committed by GitHub
parent 58f058f3e9
commit 05b796aadc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 13 deletions

View File

@ -407,13 +407,13 @@ public class PhpSymfonyServerCodegen extends AbstractPhpCodegen implements Codeg
for (CodegenParameter param : op.allParams) {
// Determine if the parameter type is supported as a type hint and make it available
// to the templating engine
String typeHint = getTypeHint(param.dataType);
String typeHint = getTypeHint(param.dataType, false);
if (!typeHint.isEmpty()) {
param.vendorExtensions.put("x-parameter-type", typeHint);
}
if (param.isContainer) {
param.vendorExtensions.put("x-parameter-type", getTypeHint(param.dataType + "[]"));
param.vendorExtensions.put("x-parameter-type", getTypeHint(param.dataType + "[]", false));
}
// Create a variable to display the correct data type in comments for interfaces
@ -636,7 +636,7 @@ public class PhpSymfonyServerCodegen extends AbstractPhpCodegen implements Codeg
}
protected String getTypeHintNullable(String type) {
String typeHint = getTypeHint(type);
String typeHint = getTypeHint(type, false);
if (!typeHint.equals("")) {
return "?" + typeHint;
}
@ -645,7 +645,7 @@ public class PhpSymfonyServerCodegen extends AbstractPhpCodegen implements Codeg
}
protected String getTypeHintNullableForComments(String type) {
String typeHint = getTypeHint(type);
String typeHint = getTypeHint(type, true);
if (!typeHint.equals("")) {
return typeHint + "|null";
}
@ -653,10 +653,16 @@ public class PhpSymfonyServerCodegen extends AbstractPhpCodegen implements Codeg
return typeHint;
}
protected String getTypeHint(String type) {
protected String getTypeHint(String type, Boolean forComments) {
// Type hint array types
if (type.endsWith("[]")) {
return "array";
if (forComments) {
//Make type hints for array in comments. Call getTypeHint recursive for extractSimpleName for models
String typeWithoutArray = type.substring(0, type.length() - 2);
return this.getTypeHint(typeWithoutArray, true) + "[]";
} else {
return "array";
}
}
// Check if the type is a native type that is type hintable in PHP
@ -682,4 +688,4 @@ public class PhpSymfonyServerCodegen extends AbstractPhpCodegen implements Codeg
protected Boolean isModelClass(String type) {
return Boolean.valueOf(type.contains(modelPackage()));
}
}
}

View File

@ -70,7 +70,7 @@ class Pet
protected ?string $name = null;
/**
* @var array|null
* @var string[]|null
* @SerializedName("photoUrls")
* @Assert\NotNull()
* @Assert\All({
@ -81,7 +81,7 @@ class Pet
protected ?array $photoUrls = null;
/**
* @var array|null
* @var Tag[]|null
* @SerializedName("tags")
* @Assert\All({
* @Assert\Type("OpenAPI\Server\Model\Tag")
@ -192,7 +192,7 @@ class Pet
/**
* Gets photoUrls.
*
* @return array|null
* @return string[]|null
*/
public function getPhotoUrls(): ?array
{
@ -202,7 +202,7 @@ class Pet
/**
* Sets photoUrls.
*
* @param array|null $photoUrls
* @param string[]|null $photoUrls
*
* @return $this
*/
@ -216,7 +216,7 @@ class Pet
/**
* Gets tags.
*
* @return array|null
* @return Tag[]|null
*/
public function getTags(): ?array
{
@ -226,7 +226,7 @@ class Pet
/**
* Sets tags.
*
* @param array|null $tags
* @param Tag[]|null $tags
*
* @return $this
*/