zircote/swagger-php
swagger-php generates OpenAPI 3.0/3.1/3.2 documentation from your PHP 8.2+ code using attributes (preferred) or optional Doctrine annotations. Use it via CLI or programmatically, with helpful error reporting and a full documentation site.
swagger-php supports both basic and backed enums out of the box.
OA\SchemaThe simples way of using enums is to annotate them as OA\Schema. This allows you to reference them like any other schema in your spec.
<<< @/snippets/guide/enums/enum_as_schema_at.php
<<< @/snippets/guide/enums/enum_as_schema_an.php
Results in:
openapi: 3.0.0
components:
schemas:
Colour:
type: string
enum:
- GREEN
- BLUE
- RED
Product:
properties:
colour:
$ref: '#/components/schemas/Colour'
type: object
Enum cases can be used as value in an enum list just like a string, integer or any other primitive type.
Basic enum:
<<< @/snippets/guide/enums/enum_as_values_at.php
<<< @/snippets/guide/enums/enum_as_values_an.php
Results in:
openapi: 3.0.0
components:
schemas:
Model:
properties:
someSuits:
type: array
enum:
- Hearts
- Diamonds
type: object
:::tip Backed enums If the enum is a backed enum, the case backing value is used instead of the name. :::
For backed enums there exist two rules that determine whether the name or backing value is used:
By default, the name of a backed enum case is used.
Example:
<<< @/snippets/guide/enums/backed_enum_names_as_schema_at.php
<<< @/snippets/guide/enums/backed_enum_names_as_schema_an.php
Results in:
openapi: 3.0.0
components:
schemas:
Colour:
type: string
enum:
- GREEN
- BLUE
- RED
Product:
properties:
colour:
$ref: '#/components/schemas/Colour'
type: object
To force swagger-php to use the backing value of an enum, you need to set the schema type to match the enum backing type.
Using the backing value (integer:
<<< @/snippets/guide/enums/backed_enum_values_as_schema_at.php
<<< @/snippets/guide/enums/backed_enum_values_as_schema_an.php
Results in:
openapi: 3.0.0
components:
schemas:
Colour:
type: integer
enum:
- 1
- 2
- 3
Product:
properties:
colour:
$ref: '#/components/schemas/Colour'
type: object
How can I help you explore Laravel packages today?