Skip to content

Allow to annotate class constants as properties#1193

Merged
DerManoMann merged 3 commits into
zircote:masterfrom
DerManoMann:class-const-annotations
Apr 12, 2022
Merged

Allow to annotate class constants as properties#1193
DerManoMann merged 3 commits into
zircote:masterfrom
DerManoMann:class-const-annotations

Conversation

@DerManoMann

Copy link
Copy Markdown
Collaborator

Thanks to @cdaguerre for the idea and initial work; replaces #1147.

1. Attributes on class constants

use OpenApi\Attributes as OA;

#[OA\Schema()]
class Airport
{
    #[OA\Property(property='kind')]
    public const KIND = 'Airport';
}

2. Support the const keyword in attributes

use OpenApi\Attributes as OA;

#[OA\Schema()]
class Airport
{
    #[OA\Property(
        property: 'kind',
        type: 'string',
        const: 'Airport'
    )]
    public string $kind = 'Airport';
}

This ^^code is equivalent to the example above in terms of generated documentation.

3. Backport const type properties for versions < 3.1

The const keyword was introduced in OAS 3.1, eg.

components:
  schemas:
    Airport:
        properties:
          kind:
            type: string
            const: Airport

In OAS < 3.1, the recommended way of doing this was to declare a single-valued enum instead, eg.

components:
  schemas:
    Airport:
        properties:
          kind:
            type: string
            enum: 
              - Airport

@DerManoMann DerManoMann force-pushed the class-const-annotations branch from 6b74504 to 1848e63 Compare April 12, 2022 05:10
@DerManoMann DerManoMann merged commit 2f66ec8 into zircote:master Apr 12, 2022
@DerManoMann DerManoMann deleted the class-const-annotations branch April 12, 2022 05:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant