Skip to content

Commit

Permalink
Fix Parameter Required
Browse files Browse the repository at this point in the history
  • Loading branch information
byjg committed Nov 27, 2024
1 parent 8309699 commit fb52d3d
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 4 deletions.
7 changes: 5 additions & 2 deletions src/Base/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,11 @@ public function getPathDefinition(string $path, string $method): mixed
throw new PathNotFoundException('Path "' . $path . '" not found');
}

/**
* @throws DefinitionNotFoundException
* @throws NotMatchedException
* @throws InvalidDefinitionException
*/
protected function prepareToValidateArguments(string $path, string $method, string $parameterIn, $matches): void
{
$pathDef = $this->jsonFile[self::SWAGGER_PATHS][$path];
Expand All @@ -142,11 +147,9 @@ protected function prepareToValidateArguments(string $path, string $method, stri
* @param string $method
* @param int $status
* @return Body
* @throws DefinitionNotFoundException
* @throws HttpMethodNotFoundException
* @throws InvalidDefinitionException
* @throws InvalidRequestException
* @throws NotMatchedException
* @throws PathNotFoundException
*/
public function getResponseParameters(string $path, string $method, int $status): Body
Expand Down
2 changes: 1 addition & 1 deletion src/OpenApi/OpenApiSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ protected function validateArguments(string $parameterIn, array $parameters, arr
$parameter = $this->jsonFile[self::SWAGGER_COMPONENTS][self::SWAGGER_PARAMETERS][$paramParts[3]];
}
if ($parameter['in'] === $parameterIn) {
$parameterMatch = new Parameter($this, $parameter['name'], $parameter["schema"] ?? [], true);
$parameterMatch = new Parameter($this, $parameter['name'], $parameter["schema"] ?? [], !($parameter["required"] ?? false));
$parameterMatch->match($arguments[$parameter['name']] ?? null);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Swagger/SwaggerSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ protected function validateArguments(string $parameterIn, array $parameters, arr
{
foreach ($parameters as $parameter) {
if ($parameter['in'] === $parameterIn) {
$parameterMatch = new Parameter($this, $parameter['name'], $parameter ?? []);
$parameterMatch = new Parameter($this, $parameter['name'], $parameter ?? [], !($parameter["required"] ?? false));
$parameterMatch->match($arguments[$parameter['name']] ?? null);
}
}
Expand Down
24 changes: 24 additions & 0 deletions tests/OpenApiRequestBodyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,30 @@ public function testMatchParameterInQuery()
$this->assertTrue(true);
}

public function testMatchParameterInQueryRequired()
{
$this->expectException(NotMatchedException::class);
$this->expectExceptionMessage("Value of property 'status' is null, but should be of type 'array'");

self::openApiSchema()->getRequestParameters('/v2/pet/findByStatus', 'get');
}

public function testMatchParameterInQueryRequired2()
{
$this->expectException(NotMatchedException::class);
$this->expectExceptionMessage("Value of property 'username' is null, but should be of type 'string");

self::openApiSchema()->getRequestParameters('/user/login', 'get');
}

public function testMatchParameterInQueryRequired3()
{
$this->expectException(NotMatchedException::class);
$this->expectExceptionMessage("Value of property 'password' is null, but should be of type 'string");

self::openApiSchema()->getRequestParameters('/user/login?username=test', 'get');
}

public function testMatchParameterInQueryNotValid()
{
$this->expectException(NotMatchedException::class);
Expand Down

0 comments on commit fb52d3d

Please sign in to comment.