From c86ea65e903a013f33dc660269d7fff5e2376490 Mon Sep 17 00:00:00 2001 From: shalvah Date: Tue, 8 Jun 2021 10:16:40 +0100 Subject: [PATCH] Don't crash on unrecognized validation rule formats --- camel/Output/OutputEndpointData.php | 8 +++++--- src/Extracting/ParsesValidationRules.php | 4 ++++ .../Strategies/BodyParameters/GetFromBodyParamTag.php | 1 + 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/camel/Output/OutputEndpointData.php b/camel/Output/OutputEndpointData.php index 7490a885..72604bf8 100644 --- a/camel/Output/OutputEndpointData.php +++ b/camel/Output/OutputEndpointData.php @@ -106,8 +106,10 @@ public function __construct(array $parameters = []) [$files, $regularParameters] = collect($this->cleanBodyParameters) ->partition( function ($example) { - return $example instanceof UploadedFile - || (is_array($example) && ($example[0] ?? null) instanceof UploadedFile); + // We'll only check two levels: a file, or an array of files + return is_array($example) && isset($example[0]) + ? $example[0] instanceof UploadedFile + : $example instanceof UploadedFile; } ); if (count($files)) { @@ -142,7 +144,7 @@ public static function fromExtractedEndpointArray(array $endpoint): OutputEndpoi public function endpointId(): string { - return $this->httpMethods[0].str_replace(['/', '?', '{', '}', ':'], '-', $this->uri); + return $this->httpMethods[0] . str_replace(['/', '?', '{', '}', ':'], '-', $this->uri); } public function hasResponses(): bool diff --git a/src/Extracting/ParsesValidationRules.php b/src/Extracting/ParsesValidationRules.php index 7ae2eb27..0c0f5b39 100644 --- a/src/Extracting/ParsesValidationRules.php +++ b/src/Extracting/ParsesValidationRules.php @@ -143,6 +143,10 @@ protected function normaliseRules(array $rules): array */ protected function parseRule($rule, array &$parameterData, bool $independentOnly, array $allParameters = []): bool { + if (!(is_string($rule) || $rule instanceof Rule)) { + return true; + } + // Convert string rules into rule + arguments (eg "in:1,2" becomes ["in", ["1", "2"]]) $parsedRule = $this->parseStringRuleIntoRuleAndArguments($rule); [$rule, $arguments] = $parsedRule; diff --git a/src/Extracting/Strategies/BodyParameters/GetFromBodyParamTag.php b/src/Extracting/Strategies/BodyParameters/GetFromBodyParamTag.php index 4a0daf81..97dc9c95 100644 --- a/src/Extracting/Strategies/BodyParameters/GetFromBodyParamTag.php +++ b/src/Extracting/Strategies/BodyParameters/GetFromBodyParamTag.php @@ -75,6 +75,7 @@ public function getBodyParametersFromDocBlock(array $tags): array $type = $this->normalizeTypeName($type); [$description, $example] = $this->parseExampleFromParamDescription($description, $type); + $example = is_null($example) && !$this->shouldExcludeExample($tagContent) ? $this->generateDummyValue($type) : $example;