dawen/api-doc-property-bundle
Installation:
composer require dawen/api-doc-property-bundle
Register the bundle in config/bundles.php (Laravel) or AppKernel.php (Symfony):
Dawen\Bundle\ApiDocPropertyBundle\ApiDocPropertyBundle::class => ['all' => true],
First Use Case:
Add the @ApiDocProperty annotation to a property in your Symfony Controller/Entity to expose it in NelmioApiDoc:
use Dawen\Bundle\ApiDocPropertyBundle\Annotation\ApiDocProperty;
/**
* @ApiDocProperty(type="string", description="User's full name")
*/
private $fullName;
Verify Integration:
Ensure NelmioApiDoc is configured and running (e.g., via nelmio_api_doc:debug). Check the generated API docs (e.g., /api/doc) to confirm the property appears.
Documenting Custom Properties:
Use @ApiDocProperty for properties not covered by JMS Serializer, Symfony Validator, or JSON:
/**
* @ApiDocProperty(
* type="array<string>",
* description="List of user roles",
* name="roles"
* )
*/
private $customRoles;
Integration with NelmioApiDoc:
NelmioApiDocBundle is installed and configured.Laravel-Specific Setup:
spatie/laravel-package-tools to auto-register the bundle in config/app.php:
'providers' => [
Dawen\Bundle\ApiDocPropertyBundle\ApiDocPropertyServiceProvider::class,
],
php artisan vendor:publish --provider="Dawen\Bundle\ApiDocPropertyBundle\ApiDocPropertyServiceProvider"
Dynamic Property Documentation:
Use the name attribute to override the property name in docs:
/**
* @ApiDocProperty(type="integer", name="user_id")
*/
private $id; // Documented as "user_id" in API docs
@ApiDocProperty for metadata while letting JMS handle serialization./**
* @ApiDocProperty(type="string", description="Email address")
* @Assert\Email()
*/
private $email;
@ApiDoc annotations to group documented properties logically.Annotation Parsing Order:
parser config includes the bundle’s parser (check config/nelmio_api_doc.yaml).@ApiDocProperty annotation is placed above the property (not in PHPDoc block for some parsers).Type System Quirks:
type attribute is not validated—incorrect types (e.g., "foobar") will appear as-is in docs. Use standard PHP types (e.g., "string", "array<int>") for consistency.@ApiDocProperty(type="App\Entity\User[]")
Laravel-Specific Issues:
php artisan config:clear && php artisan cache:clear
Overriding Default Behavior:
@Groups (from API Platform) or JMS @SerializedName for runtime changes.Check Parsed Annotations: Use Nelmio’s debug command to inspect parsed properties:
php bin/console nelmio_api_doc:debug
Look for your @ApiDocProperty entries under the "properties" section.
Log Parser Events:
Enable debug mode in nelmio_api_doc.yaml:
parser:
debug: true
Check logs for parsing errors (e.g., monolog logs).
Custom Property Types:
Extend the bundle by creating a custom parser for @ApiDocProperty. Override the Dawen\Bundle\ApiDocPropertyBundle\Parser\ApiDocPropertyParser class and register it in Nelmio’s parser chain.
Dynamic Property Generation:
Use the bundle’s ApiDocPropertyExtension to add properties programmatically (e.g., from database metadata):
$extension = new ApiDocPropertyExtension();
$extension->addProperty($entity, 'dynamic_field', 'string', 'Generated dynamically');
Integration with API Platform:
Combine with api-platform/core for automatic doc generation:
# config/packages/api_platform.yaml
api_platform:
formats:
jsonld:
mime_types: ['application/ld+json']
patch_formats:
json: true
swagger:
versions: [3]
How can I help you explore Laravel packages today?