adrenalinkin/swagger-resolver-bundle
Symfony bundle that validates request/response data against Swagger 2 (OpenAPI) specs. Generates a SwaggerResolver based on Symfony OptionsResolver, with warmed caching and debug auto-refresh. Loads docs via NelmioApiDocBundle, swagger-php, or JSON/YAML files.
Installation
composer require adrenalinkin/swagger-resolver-bundle
Add to config/app.php under providers:
Adrenalinkin\SwaggerResolverBundle\SwaggerResolverBundle::class,
Publish Configuration
php artisan vendor:publish --provider="Adrenalinkin\SwaggerResolverBundle\SwaggerResolverBundle" --tag="config"
This generates config/swagger-resolver.php. Configure your Swagger JSON/YAML file path:
'swagger' => [
'path' => base_path('swagger.json'),
],
First Use Case: Validating Request Data Inject the resolver into a controller or service:
use Adrenalinkin\SwaggerResolverBundle\SwaggerResolver;
public function __construct(private SwaggerResolver $resolver) {}
public function store(Request $request)
{
$data = $request->all();
$this->resolver->validate($data, 'CreateUser'); // 'CreateUser' is the operation ID from Swagger
}
Request Validation Use the resolver to validate incoming requests against Swagger schemas:
$resolver->validate($request->json()->all(), 'UpdateProfile');
operationId in your Swagger spec (e.g., getUserById).parameters (query/path), requestBody, or responses validation.Form Request Integration
Extend Laravel’s FormRequest to auto-validate against Swagger:
use Adrenalinkin\SwaggerResolverBundle\Traits\SwaggerValidatable;
class StoreUserRequest extends FormRequest
{
use SwaggerValidatable;
protected $operationId = 'CreateUser';
}
Override validateSwagger() to customize behavior.
API Response Validation Validate responses before sending:
$responseData = ['id' => 1, 'name' => 'John'];
$this->resolver->validate($responseData, 'getUser', 'responses');
'cache' => [
'enabled' => true,
'ttl' => 3600, // 1 hour
],
Adrenalinkin\SwaggerResolverBundle\Resolver\ResolverInterface for custom validation logic.swagger.resolver.validated or swagger.resolver.failed events for post-validation actions.Operation ID Mismatch
validate() fails with "Operation not found".operationId in your Swagger spec matches exactly (case-sensitive).php artisan swagger:dump to inspect parsed operations.Schema Path Errors
address.city).properties:
address:
type: object
properties:
city:
type: string
Caching Quirks
php artisan cache:clear
'cache.enabled' => false).Array Validation
items in Swagger:
type: array
items:
type: string
enum: [admin, user]
Log Validation Errors: Enable debug mode in config:
'debug' => true,
Errors will log to storage/logs/laravel.log.
Dump Swagger Structure: Use the Artisan command to inspect parsed operations:
php artisan swagger:dump
Custom Validators Override the default resolver for specific schemas:
$resolver->setCustomValidator('User', function ($data) {
// Custom logic for 'User' schema
});
Middleware Integration Create middleware to auto-validate all API requests:
public function handle($request, Closure $next)
{
$operationId = $request->route()->getAction('operationId');
$this->resolver->validate($request->all(), $operationId);
return $next($request);
}
Testing Mock the resolver in tests:
$this->app->instance(SwaggerResolver::class, Mockery::mock(SwaggerResolver::class));
How can I help you explore Laravel packages today?