Skip to content

Commit

Permalink
Add scratch test using custom attributes (#1423)
Browse files Browse the repository at this point in the history
  • Loading branch information
DerManoMann authored Mar 8, 2023
1 parent 33960e0 commit 0299edc
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 0 deletions.
94 changes: 94 additions & 0 deletions tests/Fixtures/Scratch/CustomAttributes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php declare(strict_types=1);

/**
* @license Apache 2.0
*/

namespace OpenApi\Tests\Fixtures\Scratch;

use OpenApi\Attributes as OAT;

// ======== custom attributes =======================

#[\Attribute(\Attribute::TARGET_CLASS)]
class CustomInfo extends OAT\Info
{
}

#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::TARGET_METHOD | \Attribute::TARGET_PROPERTY)]
class CustomSchema extends OAT\Schema
{
}

#[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::TARGET_PARAMETER)]
class CustomProperty extends OAT\Property
{
}

#[\Attribute(\Attribute::TARGET_METHOD | \Attribute::TARGET_PROPERTY | \Attribute::TARGET_PARAMETER | \Attribute::TARGET_CLASS_CONSTANT | \Attribute::IS_REPEATABLE)]
class CustomItem extends OAT\Property
{
/** @param class-string $of */
public function __construct(
string $of,
?string $description = null
) {
parent::__construct(
ref: $of,
title: (new \ReflectionClass($of))->getShortName(),
description: $description,
);
}
}

#[\Attribute(\Attribute::TARGET_METHOD | \Attribute::TARGET_PROPERTY | \Attribute::TARGET_PARAMETER | \Attribute::TARGET_CLASS_CONSTANT | \Attribute::IS_REPEATABLE)]
class CustomList extends OAT\Property
{
/** @param class-string $of */
public function __construct(string $of, ?string $description = null)
{
parent::__construct(
title: (new \ReflectionClass($of))->getShortName(),
description: $description,
items: new OAT\Items(ref: $of)
);
}
}

#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::TARGET_METHOD)]
class CustomGet extends OAT\Get
{
}

// ======== application code =======================

#[CustomSchema()]
class CAItemModel
{
}

#[CustomSchema()]
class CAModel
{
#[CustomProperty]
public ?string $name;

#[CustomItem(of: CAItemModel::class)]
public readonly CAItemModel $item;

#[CustomList(of: CAItemModel::class)]
public readonly array $items;
}

#[CustomInfo(
title: 'Extended Attributes Scratch',
version: '1.0'
)]
#[CustomGet(
path: '/api/endpoint',
description: 'An endpoint',
responses: [new OAT\Response(response: 200, description: 'OK')]
)]
class CAEndpoint
{
}
27 changes: 27 additions & 0 deletions tests/Fixtures/Scratch/CustomAttributes.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
openapi: 3.0.0
info:
title: 'Extended Attributes Scratch'
version: '1.0'
paths:
/api/endpoint:
get:
description: 'An endpoint'
responses:
'200':
description: OK
components:
schemas:
CAItemModel: { }
CAModel:
properties:
name:
type: string
nullable: true
item:
$ref: '#/components/schemas/CAItemModel'
items:
title: CAItemModel
type: array
items:
$ref: '#/components/schemas/CAItemModel'
type: object

0 comments on commit 0299edc

Please sign in to comment.