knuckleswtf / scribe

Generate API documentation for humans from your Laravel codebase.✍
https://scribe.knuckles.wtf/laravel/
MIT License
1.7k stars 303 forks source link

Support limitng parameter values (enums) #713

Closed shalvah closed 1 year ago

shalvah commented 1 year ago

Adds support for limiting parameter values. Works with either a PHP 8.1 enum or an array of values.

#[BodyParam("status", enum: ['approved', 'pending', 'closed', 'new'])]
#[BodyParam("status", enum: ArticleStatus::class)]

Also supported in docblocks:

@bodyParam status string Enum: approved, pending, closed, new
@bodyParam status string Enum: approved, pending, closed, new. Example: new

This will:

  1. ensure the generated example is from one of these.
  2. add a line in the docs that says "Must be one of...", and add these values
  3. set the enum key in the OpenAPI spec

It does not currently restrict the values that can be entered in Try It Out, as there may still be some value in seeing how invalid values are responded to.

This may be a breaking change, as it replaces the earlier manual description setting (automatically generated for validation rules) with a structured field.

In the internal Scribe API, this adds a new enumValues field to the parameter types. This is an array of all valid values.

Closes #626

rabol commented 1 year ago

This is a breaking change

After I updated to the latest version i get this error:

 TypeError 

  Cannot assign null to property Knuckles\Camel\Extraction\Parameter::$enumValues of type array

  at vendor/spatie/data-transfer-object/src/Reflection/DataTransferObjectProperty.php:48