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

Allow to annotate class constants as properties #1193

Merged
merged 3 commits into from
Apr 12, 2022

Conversation

DerManoMann
Copy link
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 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.

None yet

1 participant