Skip to content

Commit

Permalink
Fix type hint for array in comments. (#16926)
Browse files Browse the repository at this point in the history
  • Loading branch information
reznikartem authored Oct 28, 2023
1 parent 58f058f commit 05b796a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -407,13 +407,13 @@ public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<Mo
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
Expand Down Expand Up @@ -636,7 +636,7 @@ protected String toSymfonyService(String name) {
}

protected String getTypeHintNullable(String type) {
String typeHint = getTypeHint(type);
String typeHint = getTypeHint(type, false);
if (!typeHint.equals("")) {
return "?" + typeHint;
}
Expand All @@ -645,18 +645,24 @@ protected String getTypeHintNullable(String type) {
}

protected String getTypeHintNullableForComments(String type) {
String typeHint = getTypeHint(type);
String typeHint = getTypeHint(type, true);
if (!typeHint.equals("")) {
return typeHint + "|null";
}

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
Expand All @@ -682,4 +688,4 @@ protected String getTypeHint(String type) {
protected Boolean isModelClass(String type) {
return Boolean.valueOf(type.contains(modelPackage()));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class Pet
protected ?string $name = null;

/**
* @var array|null
* @var string[]|null
* @SerializedName("photoUrls")
* @Assert\NotNull()
* @Assert\All({
Expand 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")
Expand Down Expand Up @@ -192,7 +192,7 @@ public function setName(?string $name): self
/**
* Gets photoUrls.
*
* @return array|null
* @return string[]|null
*/
public function getPhotoUrls(): ?array
{
Expand All @@ -202,7 +202,7 @@ public function getPhotoUrls(): ?array
/**
* Sets photoUrls.
*
* @param array|null $photoUrls
* @param string[]|null $photoUrls
*
* @return $this
*/
Expand All @@ -216,7 +216,7 @@ public function setPhotoUrls(?array $photoUrls): self
/**
* Gets tags.
*
* @return array|null
* @return Tag[]|null
*/
public function getTags(): ?array
{
Expand All @@ -226,7 +226,7 @@ public function getTags(): ?array
/**
* Sets tags.
*
* @param array|null $tags
* @param Tag[]|null $tags
*
* @return $this
*/
Expand Down

0 comments on commit 05b796a

Please sign in to comment.