Skip to content

Commit

Permalink
add validation based on version
Browse files Browse the repository at this point in the history
  • Loading branch information
qdequippe committed Aug 10, 2023
1 parent d53b09b commit 2340aee
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 21 deletions.
26 changes: 12 additions & 14 deletions Examples/webhook-example/OpenApiSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,21 @@
* title="Webhook Example",
* ),
* webhooks={
* {
* "newPet": @OA\PathItem(
* @OA\Post(
* @OA\RequestBody(
* description="Information about a new pet in the system",
* @OA\MediaType(
* mediaType="application/xml",
* @OA\Schema(ref="#/components/schemas/Pet")
* ),
* "newPet": @OA\PathItem(
* @OA\Post(
* @OA\RequestBody(
* description="Information about a new pet in the system",
* @OA\MediaType(
* mediaType="application/json",
* @OA\Schema(ref="#/components/schemas/Pet")
* ),
* @OA\Response(
* response=200,
* description="Return a 200 status to indicate that the data was received successfully"
* )
* ),
* @OA\Response(
* response=200,
* description="Return a 200 status to indicate that the data was received successfully"
* )
* )
* }
* )
* }
* )
*/
Expand Down
1 change: 1 addition & 0 deletions Examples/webhook-example/webhook-example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ components:
type: string
tag:
type: string
type: object
webhooks:
newPet:
post:
Expand Down
27 changes: 27 additions & 0 deletions src/Annotations/OpenApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,18 @@ public function validate(array $stack = null, array $skip = null, string $ref =
return false;
}

if ($this->openapi === self::VERSION_3_0_0 && Generator::isDefault($this->paths)) {
$this->_context->logger->warning('The OpenAPI document must contain paths field');

return false;
}

if ($this->openapi === self::VERSION_3_1_0 && Generator::isDefault($this->paths) && Generator::isDefault($this->webhooks) && Generator::isDefault($this->components)) {
$this->_context->logger->warning('The OpenAPI document must contain at least one paths field, a components field or a webhooks field');

return false;
}

return parent::validate([], [], '#', new \stdClass());
}

Expand Down Expand Up @@ -237,4 +249,19 @@ private static function resolveRef(string $ref, string $resolved, $container, ar

throw new \Exception('$ref "' . $unresolved . '" not found');
}

/**
* @inheritdoc
*/
#[\ReturnTypeWillChange]
public function jsonSerialize()
{
$data = parent::jsonSerialize();

if (false === $this->_context->isVersion(OpenApi::VERSION_3_1_0)) {
unset($data->webhooks);
}

return $data;
}
}
2 changes: 1 addition & 1 deletion src/Attributes/OpenApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ class OpenApi extends \OpenApi\Annotations\OpenApi
* @param Server[]|null $servers
* @param Tag[]|null $tags
* @param PathItem[]|null $paths
* @param array<string,PathItem>|null $webhooks
* @param array<string,mixed>|null $x
* @param Attachable[]|null $attachables
* @param array<string,PathItem>|null $webhooks
*/
public function __construct(
string $openapi = self::DEFAULT_VERSION,
Expand Down
14 changes: 11 additions & 3 deletions tests/Annotations/OpenApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,18 @@

class OpenApiTest extends OpenApiTestCase
{
public function testValidVersion(): void
public function testValidVersion310(): void
{
$this->assertOpenApiLogEntryContains('Required @OA\Info() not found');
$this->assertOpenApiLogEntryContains('Required @OA\PathItem() not found');
$this->assertOpenApiLogEntryContains('The OpenAPI document must contain at least one paths field, a components field or a webhooks field');

$openapi = new OA\OpenApi(['_context' => $this->getContext()]);
$openapi->openapi = '3.1.0';
$openapi->validate();
}

public function testValidVersion300(): void
{
$this->assertOpenApiLogEntryContains('The OpenAPI document must contain paths field');

$openapi = new OA\OpenApi(['_context' => $this->getContext()]);
$openapi->openapi = '3.0.0';
Expand Down
2 changes: 1 addition & 1 deletion tests/ContextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function testDetect(): void

public function testFullyQualifiedName(): void
{
$this->assertOpenApiLogEntryContains('Required @OA\PathItem() not found');
$this->assertOpenApiLogEntryContains('The OpenAPI document must contain paths field');
$openapi = (new Generator($this->getTrackingLogger()))
->setAnalyser(new TokenAnalyser())
->generate([$this->fixture('Customer.php')]);
Expand Down
3 changes: 1 addition & 2 deletions tests/GeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ public function testScan(string $sourceDir, iterable $sources): void
public function testScanInvalidSource(): void
{
$this->assertOpenApiLogEntryContains('Skipping invalid source: /tmp/__swagger_php_does_not_exist__');
$this->assertOpenApiLogEntryContains('Required @OA\Info() not found');
$this->assertOpenApiLogEntryContains('Required @OA\PathItem() not found');
$this->assertOpenApiLogEntryContains('The OpenAPI document must contain paths field');

(new Generator($this->getTrackingLogger()))
->setAnalyser($this->getAnalyzer())
Expand Down

0 comments on commit 2340aee

Please sign in to comment.