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-symfony] Fix type hint for array in comments. #16926

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading