Skip to content

Commit

Permalink
Attachables (#995)
Browse files Browse the repository at this point in the history
Introduce new annotation `Attachable`

This allows to attach any data to a given annotation.
Attachables are not part of the spec but they allow to inject custom data to annotations that can then be processed by a custom processor.
  • Loading branch information
DerManoMann authored Nov 8, 2021
1 parent a69e797 commit 8f2c1ff
Show file tree
Hide file tree
Showing 36 changed files with 246 additions and 3 deletions.
3 changes: 2 additions & 1 deletion Examples/misc/misc-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
* securityScheme="bearerAuth",
* type="http",
* scheme="bearer",
* )
* ),
* @OA\Attachable()
* )
*/

Expand Down
8 changes: 7 additions & 1 deletion src/Annotations/AbstractAnnotation.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ abstract class AbstractAnnotation implements \JsonSerializable
*/
public $x = Generator::UNDEFINED;

/**
* Arbitrary attachables for this annotation.
* These will be ignored but can be used for custom processing.
*/
public $attachables = Generator::UNDEFINED;

/**
* @var Context
*/
Expand Down Expand Up @@ -81,7 +87,7 @@ abstract class AbstractAnnotation implements \JsonSerializable
*
* @var array
*/
public static $_blacklist = ['_context', '_unmerged'];
public static $_blacklist = ['_context', '_unmerged', 'attachables'];

public function __construct(array $properties)
{
Expand Down
1 change: 1 addition & 0 deletions src/Annotations/AdditionalProperties.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,6 @@ class AdditionalProperties extends Schema
ExternalDocumentation::class => 'externalDocs',
Xml::class => 'xml',
AdditionalProperties::class => 'additionalProperties',
Attachable::class => ['attachables'],
];
}
72 changes: 72 additions & 0 deletions src/Annotations/Attachable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php declare(strict_types=1);

/**
* @license Apache 2.0
*/

namespace OpenApi\Annotations;

/**
* @Annotation
*
* A container for custom data to be attached to an annotation.
* These will be ignored by swagger-php but can be used for custom processing.
*/
class Attachable extends AbstractAnnotation
{
/**
* @inheritdoc
*/
public static $_parents = [
AdditionalProperties::class,
Components::class,
Contact::class,
Delete::class,
Discriminator::class,
Examples::class,
ExternalDocumentation::class,
Flow::class,
Get::class,
Head::class,
Header::class,
Info::class,
Items::class,
JsonContent::class,
License::class,
Link::class,
MediaType::class,
OpenApi::class,
Operation::class,
Options::class,
Parameter::class,
Patch::class,
PathItem::class,
Post::class,
Property::class,
Put::class,
RequestBody::class,
Response::class,
Schema::class,
SecurityScheme::class,
Server::class,
ServerVariable::class,
Tag::class,
Trace::class,
Xml::class,
XmlContent::class,
];

/**
* Allows to type hint a specific parent annotation class.
*
* Allows to implement custom annotations that are limited to a subset of potential parent
* annotation classes.
* This is most likely going to be a v4 (PHP 8.1) PHP attribute feature only.
*
* @return array List of valid parent annotation classes. If `null`` the default nesting rules apply.
*/
public function allowedParents(): ?array
{
return null;
}
}
1 change: 1 addition & 0 deletions src/Annotations/Components.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,5 +106,6 @@ class Components extends AbstractAnnotation
Header::class => ['headers', 'header'],
SecurityScheme::class => ['securitySchemes', 'securityScheme'],
Link::class => ['links', 'link'],
Attachable::class => ['attachables'],
];
}
7 changes: 7 additions & 0 deletions src/Annotations/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,11 @@ class Contact extends AbstractAnnotation
public static $_parents = [
Info::class,
];

/**
* @inheritdoc
*/
public static $_nested = [
Attachable::class => ['attachables'],
];
}
7 changes: 7 additions & 0 deletions src/Annotations/Discriminator.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,11 @@ class Discriminator extends AbstractAnnotation
JsonContent::class,
XmlContent::class,
];

/**
* @inheritdoc
*/
public static $_nested = [
Attachable::class => ['attachables'],
];
}
7 changes: 7 additions & 0 deletions src/Annotations/Examples.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,11 @@ class Examples extends AbstractAnnotation
JsonContent::class,
XmlContent::class,
];

/**
* @inheritdoc
*/
public static $_nested = [
Attachable::class => ['attachables'],
];
}
7 changes: 7 additions & 0 deletions src/Annotations/ExternalDocumentation.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,11 @@ class ExternalDocumentation extends AbstractAnnotation
JsonContent::class,
XmlContent::class,
];

/**
* @inheritdoc
*/
public static $_nested = [
Attachable::class => ['attachables'],
];
}
7 changes: 7 additions & 0 deletions src/Annotations/Flow.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,13 @@ class Flow extends AbstractAnnotation
SecurityScheme::class,
];

/**
* @inheritdoc
*/
public static $_nested = [
Attachable::class => ['attachables'],
];

/** @inheritdoc */
#[\ReturnTypeWillChange]
public function jsonSerialize()
Expand Down
1 change: 1 addition & 0 deletions src/Annotations/Header.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ class Header extends AbstractAnnotation
*/
public static $_nested = [
Schema::class => 'schema',
Attachable::class => ['attachables'],
];

/**
Expand Down
1 change: 1 addition & 0 deletions src/Annotations/Info.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ class Info extends AbstractAnnotation
public static $_nested = [
Contact::class => 'contact',
License::class => 'license',
Attachable::class => ['attachables'],
];

/**
Expand Down
1 change: 1 addition & 0 deletions src/Annotations/Items.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class Items extends Schema
ExternalDocumentation::class => 'externalDocs',
Xml::class => 'xml',
AdditionalProperties::class => 'additionalProperties',
Attachable::class => ['attachables'],
];

/**
Expand Down
1 change: 1 addition & 0 deletions src/Annotations/JsonContent.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,6 @@ class JsonContent extends Schema
ExternalDocumentation::class => 'externalDocs',
AdditionalProperties::class => 'additionalProperties',
Examples::class => ['examples', 'example'],
Attachable::class => ['attachables'],
];
}
7 changes: 7 additions & 0 deletions src/Annotations/License.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,11 @@ class License extends AbstractAnnotation
public static $_parents = [
Info::class,
];

/**
* @inheritdoc
*/
public static $_nested = [
Attachable::class => ['attachables'],
];
}
1 change: 1 addition & 0 deletions src/Annotations/Link.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ class Link extends AbstractAnnotation
*/
public static $_nested = [
Server::class => 'server',
Attachable::class => ['attachables'],
];

/**
Expand Down
2 changes: 2 additions & 0 deletions src/Annotations/MediaType.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ class MediaType extends AbstractAnnotation
public static $_nested = [
Schema::class => 'schema',
Examples::class => ['examples', 'example'],
Attachable::class => ['attachables'],
];

/**
* @inheritdoc
*/
Expand Down
1 change: 1 addition & 0 deletions src/Annotations/OpenApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ class OpenApi extends AbstractAnnotation
Components::class => 'components',
Tag::class => ['tags'],
ExternalDocumentation::class => 'externalDocs',
Attachable::class => ['attachables'],
];

/**
Expand Down
1 change: 1 addition & 0 deletions src/Annotations/Operation.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ abstract class Operation extends AbstractAnnotation
ExternalDocumentation::class => 'externalDocs',
Server::class => ['servers'],
RequestBody::class => 'requestBody',
Attachable::class => ['attachables'],
];

/**
Expand Down
1 change: 1 addition & 0 deletions src/Annotations/Parameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ class Parameter extends AbstractAnnotation
public static $_nested = [
Schema::class => 'schema',
Examples::class => ['examples', 'example'],
Attachable::class => ['attachables'],
];

/**
Expand Down
1 change: 1 addition & 0 deletions src/Annotations/PathItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ class PathItem extends AbstractAnnotation
Options::class => 'options',
Parameter::class => ['parameters'],
Server::class => ['servers'],
Attachable::class => ['attachables'],
];

/**
Expand Down
1 change: 1 addition & 0 deletions src/Annotations/Property.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,6 @@ class Property extends Schema
ExternalDocumentation::class => 'externalDocs',
Xml::class => 'xml',
AdditionalProperties::class => 'additionalProperties',
Attachable::class => ['attachables'],
];
}
1 change: 1 addition & 0 deletions src/Annotations/RequestBody.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,5 +80,6 @@ class RequestBody extends AbstractAnnotation
*/
public static $_nested = [
MediaType::class => ['content', 'mediaType'],
Attachable::class => ['attachables'],
];
}
1 change: 1 addition & 0 deletions src/Annotations/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ class Response extends AbstractAnnotation
MediaType::class => ['content', 'mediaType'],
Header::class => ['headers', 'header'],
Link::class => ['links', 'link'],
Attachable::class => ['attachables'],
];

/**
Expand Down
1 change: 1 addition & 0 deletions src/Annotations/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,7 @@ class Schema extends AbstractAnnotation
ExternalDocumentation::class => 'externalDocs',
Xml::class => 'xml',
AdditionalProperties::class => 'additionalProperties',
Attachable::class => ['attachables'],
];

/**
Expand Down
1 change: 1 addition & 0 deletions src/Annotations/SecurityScheme.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ class SecurityScheme extends AbstractAnnotation
*/
public static $_nested = [
Flow::class => ['flows', 'flow'],
Attachable::class => ['attachables'],
];

/**
Expand Down
1 change: 1 addition & 0 deletions src/Annotations/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ class Server extends AbstractAnnotation
*/
public static $_nested = [
ServerVariable::class => ['variables', 'serverVariable'],
Attachable::class => ['attachables'],
];

/**
Expand Down
7 changes: 7 additions & 0 deletions src/Annotations/ServerVariable.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,11 @@ class ServerVariable extends AbstractAnnotation
'default' => 'string',
'description' => 'string',
];

/**
* @inheritdoc
*/
public static $_nested = [
Attachable::class => ['attachables'],
];
}
1 change: 1 addition & 0 deletions src/Annotations/Tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,6 @@ class Tag extends AbstractAnnotation
*/
public static $_nested = [
ExternalDocumentation::class => 'externalDocs',
Attachable::class => ['attachables'],
];
}
7 changes: 7 additions & 0 deletions src/Annotations/Xml.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,11 @@ class Xml extends AbstractAnnotation
Items::class,
XmlContent::class,
];

/**
* @inheritdoc
*/
public static $_nested = [
Attachable::class => ['attachables'],
];
}
1 change: 1 addition & 0 deletions src/Annotations/XmlContent.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,6 @@ class XmlContent extends Schema
Xml::class => 'xml',
AdditionalProperties::class => 'additionalProperties',
Examples::class => ['examples', 'example'],
Attachable::class => ['attachables'],
];
}
1 change: 1 addition & 0 deletions src/Serializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class Serializer
{
private static $VALID_ANNOTATIONS = [
OA\AdditionalProperties::class,
OA\Attachable::class,
OA\Components::class,
OA\Contact::class,
OA\Delete::class,
Expand Down
Loading

0 comments on commit 8f2c1ff

Please sign in to comment.