Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set schema property only on actual Schema instances #1379

Merged
merged 1 commit into from
Jan 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions src/Processors/AugmentSchemas.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

use OpenApi\Analysis;
use OpenApi\Annotations as OA;
use OpenApi\Attributes as OAT;
use OpenApi\Context;
use OpenApi\Generator;

Expand All @@ -20,11 +21,8 @@ class AugmentSchemas
{
public function __invoke(Analysis $analysis)
{
/** @var OA\Schema[] $schemas */
$schemas = $analysis->getAnnotationsOfType(OA\Schema::class);

// Use the class names for @OA\Schema()
foreach ($schemas as $schema) {
/** @var OA\Schema $schema */
foreach ($analysis->getAnnotationsOfType([OA\Schema::class, OAT\Schema::class], true) as $schema) {
if (Generator::isDefault($schema->schema)) {
if ($schema->_context->is('class')) {
$schema->schema = $schema->_context->class;
Expand All @@ -38,6 +36,9 @@ public function __invoke(Analysis $analysis)
}
}

/** @var OA\Schema[] $schemas */
$schemas = $analysis->getAnnotationsOfType(OA\Schema::class);

// Merge unmerged @OA\Property annotations into the @OA\Schema of the class
$unmergedProperties = $analysis->unmerged()->getAnnotationsOfType(OA\Property::class);
foreach ($unmergedProperties as $property) {
Expand Down
43 changes: 43 additions & 0 deletions tests/Fixtures/Scratch/NestedSchema.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php declare(strict_types=1);

/**
* @license Apache 2.0
*/

namespace OpenApi\Tests\Fixtures\Scratch;

use OpenApi\Attributes as OAT;

#[OAT\Info(
title: 'Parameter Content Scratch',
version: '1.0'
)]
#[OAT\Get(
path: '/api/endpoint',
responses: [new OAT\Response(response: 200, description: 'OK')]
)]
#[OAT\Schema(
required: ['errors'],
properties: [
new OAT\Property(
property: 'errors',
description: 'Validation errors',
type: 'object',
minItems: 1,
uniqueItems: true,
additionalProperties: new OAT\AdditionalProperties(
description: 'Array of error messages for property',
type: 'array',
items: new OAT\Items(
type: 'string',
),
minItems: 1,
uniqueItems: true,
),
),
],
type: 'object'
)]
class NestedSchema
{
}
29 changes: 29 additions & 0 deletions tests/Fixtures/Scratch/NestedSchema.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
openapi: 3.0.0
info:
title: 'Parameter Content Scratch'
version: '1.0'
paths:
/api/endpoint:
get:
responses:
'200':
description: OK
components:
schemas:
NestedSchema:
required:
- errors
properties:
errors:
description: 'Validation errors'
type: object
minItems: 1
uniqueItems: true
additionalProperties:
description: 'Array of error messages for property'
type: array
items:
type: string
minItems: 1
uniqueItems: true
type: object