kleijnweb/swagger-bundle
Unmaintained Symfony bundle for contract-first REST APIs using OpenAPI/Swagger. Generates routing from specs, validates requests, and transforms input/output with minimal config and no FOSRestBundle/Twig. Includes security integration; 4.0 beta targets PHP 7.
Installation
composer require kleijnweb/swagger-bundle
Add to config/bundles.php (Symfony) or config/app.php (Laravel via Symfony bridge):
Kleijnweb\SwaggerBundle\KleijnwebSwaggerBundle::class => ['all' => true],
Configuration
Place your OpenAPI/Swagger YAML file (e.g., swagger.yaml) in config/packages/swagger.yaml or define the path in config/packages/kleijnweb_swagger.yaml:
kleijnweb_swagger:
resource: '%kernel.project_dir%/config/packages/swagger.yaml'
First Use Case Define a route in your OpenAPI spec:
paths:
/api/users:
get:
summary: "Get all users"
responses:
200:
description: "OK"
Run:
php bin/console swagger:generate
This generates Symfony routes and validation rules from your OpenAPI spec.
API-First Development
swagger:generate to auto-generate route definitions in config/routes/swagger.yaml.Validation
# swagger.yaml
parameters:
- in: query
name: limit
schema:
type: integer
minimum: 1
The bundle auto-generates validation constraints (e.g., @Assert\Min(1)).Documentation-Driven Testing
$client = static::createClient();
$client->request('GET', '/api/users?limit=10'); // Matches OpenAPI spec
Partial Adoption
swagger:generate flags:
php bin/console swagger:generate --exclude=/admin/*
Schema Reuse
components/schemas:
components:
schemas:
User:
type: object
properties:
id: { type: integer }
name: { type: string }
responses:
200:
content:
application/json:
schema: { $ref: '#/components/schemas/User' }
Outdated Package
SwaggerGenerator for newer Symfony components).Route Name Conflicts
swagger_get_api_users) may clash with manual routes.swagger:generate --prefix=api/ to namespace routes or manually override in routes.yaml.Validation Gaps
@Assert\Valid) may not generate correctly.Circular References
$ref schemas may cause generation errors.Laravel-Specific Quirks
kernel.project_dir points to Laravel’s root.KLEINJWEB_SWAGGER_RESOURCE in .env:
KLEINJWEB_SWAGGER_RESOURCE=/path/to/swagger.yaml
Dry Run
Use --dry-run to preview generated routes without writing to disk:
php bin/console swagger:generate --dry-run
Log Generation Enable debug mode to log generation steps:
# config/packages/kleijnweb_swagger.yaml
kleijnweb_swagger:
debug: true
Partial Generation Generate only specific sections (e.g., routes or validation):
php bin/console swagger:generate --only=routes
Custom Generators
Extend Kleijnweb\SwaggerBundle\Generator\SwaggerGenerator to add custom logic (e.g., API platform metadata).
Post-Generation Hooks Use Symfony’s event system to modify generated routes/validation:
# config/packages/kleijnweb_swagger.yaml
kleijnweb_swagger:
events:
post_generate: App\EventListener\SwaggerPostGenerateListener
Integration with API Platform
Combine with api component to auto-generate CRUD operations from OpenAPI:
# config/packages/api_platform.yaml
api_platform:
formats:
jsonld:
mime_types: ['application/ld+json']
json:
mime_types: ['application/json', 'application/vnd.api+json']
How can I help you explore Laravel packages today?