如何在Symfony 4 API Platform的Swagger导出文件中添加额外应用信息?
Got it! You're using Symfony 4 with API Platform and Redoc, and you want to stop manually adding x-logo and other custom API info to your exported swagger.yaml—instead, you want the php bin/console api:swagger:export --output=openapi.yaml --yaml command to include these details automatically. Here are two reliable approaches to solve this:
1. Static Configuration (Quick & Simple)
If your metadata doesn't need to change dynamically, you can define it directly in API Platform's configuration file. This is the easiest method for static values like logos, fixed descriptions, or server URLs.
Edit your config/packages/api_platform.yaml file and add an openapi section with your custom fields:
api_platform: # Keep your existing configuration here (like mapping, formats, etc.) openapi: info: title: "Your API's Official Title" description: "A detailed description of what your API does" version: "1.2.0" # Add your x-logo extension here x-logo: url: "https://your-domain.com/assets/api-logo.png" backgroundColor: "#FFFFFF" altText: "Your Brand API Logo" # Optional: Add server info, terms of service, or other root-level extensions servers: - url: "https://api.your-domain.com/v1" description: "Production API Server" x-api-custom-meta: contactEmail: "api-support@your-domain.com" termsOfService: "https://your-domain.com/api-terms"
Now when you run the export command, all these fields will be automatically included in the generated openapi.yaml file. Redoc will recognize the x-logo field and display your logo in the documentation header.
2. Dynamic Customization (For Flexible/Context-Dependent Metadata)
If you need metadata that changes based on environment (e.g., different logos for dev vs prod) or pulls values from a database/configuration, use a decorator for API Platform's OpenApiFactory. This lets you modify the OpenAPI document on-the-fly before it's exported or served.
Step 1: Create the Decorator Class
Make a new file src/OpenApi/OpenApiDecorator.php with this code:
<?php namespace App\OpenApi; use ApiPlatform\Core\OpenApi\Factory\OpenApiFactoryInterface; use ApiPlatform\Core\OpenApi\OpenApi; final class OpenApiDecorator implements OpenApiFactoryInterface { private $decorated; // Inject the original OpenApiFactory via constructor injection public function __construct(OpenApiFactoryInterface $decorated) { $this->decorated = $decorated; } public function __invoke(array $context = []): OpenApi { // Get the base OpenAPI document generated by API Platform $openApi = $this->decorated->__invoke($context); $info = $openApi->getInfo(); // Update the info section with your custom extensions $enhancedInfo = $info ->withExtension('x-logo', [ 'url' => $_ENV['API_LOGO_URL'], // Use an environment variable for dynamic values 'backgroundColor' => "#F5F5F5", 'altText' => "Dynamic API Logo" ]) ->withDescription("This description can be pulled from a config or database!"); // Apply the updated info to the OpenAPI document $openApi = $openApi->withInfo($enhancedInfo); // Optional: Add root-level custom extensions $openApi = $openApi->withExtension('x-environment', $_ENV['APP_ENV']); return $openApi; } }
Step 2: Register the Decorator
Edit config/services.yaml to register your decorator so API Platform uses it:
services: App\OpenApi\OpenApiDecorator: decorates: 'api_platform.openapi.factory' arguments: ['@.inner'] # Pass the original factory to your decorator
Now, every time you run the export command or access your API documentation, the decorator will inject your custom metadata dynamically. This is perfect for environment-specific values or dynamic content.
内容的提问来源于stack exchange,提问作者Ruben van der Linde

