Skip to content

Commit

Permalink
OpenApi 3.1.0 updates for Licence (#1038)
Browse files Browse the repository at this point in the history
Also fixes a couple spectral validation issues
  • Loading branch information
DerManoMann authored Dec 22, 2021
1 parent c4b6729 commit c2665e0
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ class ProductController
* @OA\Get(
* tags={"Products"},
* path="/products/{id}",
* @OA\PathParameter(
* name="id",
* required=true
* ),
* @OA\Response(
* response=200,
* description="A single product",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ paths:
tags:
- Products
operationId: 399b71a7672f0a46be1b5f4c120c355d
parameters:
-
name: id
in: path
required: true
responses:
'200':
description: 'A single product'
Expand Down
4 changes: 2 additions & 2 deletions Examples/swagger-spec/petstore-simple/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* description="A sample API that uses a petstore as an example to demonstrate features in the swagger-2.0 specification",
* termsOfService="http://swagger.io/terms/",
* @OA\Contact(name="Swagger API Team"),
* @OA\License(name="MIT")
* @OA\License(name="MIT", identifier="MIT")
* ),
* )
*/
Expand All @@ -39,4 +39,4 @@ class OpenApiSpec
*/
class ErrorModel
{
}
}
1 change: 1 addition & 0 deletions Examples/swagger-spec/petstore-simple/petstore-simple.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ info:
name: 'Swagger API Team'
license:
name: MIT
identifier: MIT
version: 1.0.0
servers:
-
Expand Down
2 changes: 1 addition & 1 deletion Examples/swagger-spec/petstore/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @OA\Info(
* version="1.0.0",
* title="Swagger Petstore",
* @OA\License(name="MIT")
* @OA\License(name="MIT", identifier="MIT")
* ),
* @OA\Server(
* description="Api server",
Expand Down
1 change: 1 addition & 0 deletions Examples/swagger-spec/petstore/petstore.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ info:
title: 'Swagger Petstore'
license:
name: MIT
identifier: MIT
version: 1.0.0
servers:
-
Expand Down
27 changes: 26 additions & 1 deletion src/Annotations/License.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,16 @@ class License extends AbstractAnnotation
public $name = Generator::UNDEFINED;

/**
* A URL to the license used for the API.
* An SPDX license expression for the API. The `identifier` field is mutually exclusive of the `url` field.
*
* @var string
*/
public $identifier = Generator::UNDEFINED;

/**
* A URL to the license used for the API. This MUST be in the form of a URL.
*
* The `url` field is mutually exclusive of the `identifier` field.
*
* @var string
*/
Expand All @@ -36,6 +45,7 @@ class License extends AbstractAnnotation
*/
public static $_types = [
'name' => 'string',
'identifier' => 'string',
'url' => 'string',
];

Expand All @@ -57,4 +67,19 @@ class License extends AbstractAnnotation
public static $_nested = [
Attachable::class => ['attachables'],
];

/**
* @inheritdoc
*/
public function validate(array $parents = [], array $skip = [], string $ref = ''): bool
{
$valid = parent::validate($parents, $skip);

if ($this->url !== Generator::UNDEFINED && $this->identifier !== Generator::UNDEFINED) {
$this->_context->logger->warning('@OA\\License() url and identifier are mutually exclusive');
$valid = false;
}

return $valid;
}
}
12 changes: 7 additions & 5 deletions src/Attributes/License.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,17 @@ class License extends \OpenApi\Annotations\License
{
public function __construct(
string $name = Generator::UNDEFINED,
string $identifier = Generator::UNDEFINED,
string $url = Generator::UNDEFINED,
?array $x = null,
?array $attachables = null
) {
parent::__construct([
'name' => $name,
'url' => $url,
'x' => $x ?? Generator::UNDEFINED,
'value' => $this->combine($attachables),
]);
'name' => $name,
'identifier' => $identifier,
'url' => $url,
'x' => $x ?? Generator::UNDEFINED,
'value' => $this->combine($attachables),
]);
}
}
19 changes: 19 additions & 0 deletions tests/Annotations/LicenseTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php declare(strict_types=1);

/**
* @license Apache 2.0
*/

namespace OpenApi\Tests\Annotations;

use OpenApi\Tests\OpenApiTestCase;

class LicenseTest extends OpenApiTestCase
{
public function testValidation()
{
$annotations = $this->annotationsFromDocBlockParser('@OA\License(name="MIT", identifier="MIT", url="http://localhost")');
$this->assertOpenApiLogEntryContains('@OA\License() url and identifier are mutually exclusive');
$annotations[0]->validate();
}
}

0 comments on commit c2665e0

Please sign in to comment.