Skip to content

Commit

Permalink
Merge pull request #142 from nextcloud/fix/openapitype/prevent-non-st…
Browse files Browse the repository at this point in the history
…ring-indexed-arrays

fix(OpenApiType): Prevent arrays indexed by non-strings
  • Loading branch information
nickvergessen authored Jul 23, 2024
2 parents 944c6a6 + 5ca25cb commit e6a1936
Showing 1 changed file with 4 additions and 5 deletions.
9 changes: 4 additions & 5 deletions src/OpenApiType.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,13 +165,12 @@ public static function resolve(string $context, array $definitions, ParamTagValu
return new OpenApiType(type: "object", properties: $properties, required: count($required) > 0 ? $required : null);
}

if ($node instanceof GenericTypeNode && $node->type->name == "array" && count($node->genericTypes) == 2 && $node->genericTypes[0] instanceof IdentifierTypeNode) {
if ($node->genericTypes[0]->name == "array-key") {
Logger::error($context, "Instead of 'array-key' use 'string' or 'int'");
}
if ($node->genericTypes[0]->name == "string" || $node->genericTypes[0]->name == "array-key") {
if ($node instanceof GenericTypeNode && $node->type->name === "array" && count($node->genericTypes) === 2 && $node->genericTypes[0] instanceof IdentifierTypeNode) {
if ($node->genericTypes[0]->name === "string") {
return new OpenApiType(type: "object", additionalProperties: self::resolve($context, $definitions, $node->genericTypes[1]));
}

Logger::panic($context, "JSON objects can only be indexed by 'string' but got '" . $node->genericTypes[0]->name . "'");
}

if ($node instanceof GenericTypeNode && $node->type->name == "int" && count($node->genericTypes) == 2) {
Expand Down

0 comments on commit e6a1936

Please sign in to comment.