OpenAPI: Non-JSON formats should not generate JSON schemas
維護者通常 1 天內回覆
還沒有人認領這個 Issue。
評估
研究方向
從 src/OpenApi/Factory/OpenApiFactory.php 和 src/Symfony/Bundle/Resources/config/openapi.php 開始,接著檢查 ApiPlatformExtension.php 中如何計算 jsonschema_formats。更新非 JSON 回應格式的 OpenAPI 行為,並在 tests/OpenApi/Factory/OpenApiFactoryTest.php 中加入回歸案例;完成的標準是非 JSON 格式不再接收產生的 JSON schema。
由索引模型根據 Issue 內容生成。
描述
Issue Description
When an API Platform operation defines non-JSON output formats (e.g., text/html, text/xml), the OpenAPI factory generates JSON schemas for these formats, which is semantically incorrect.
Example
#[ApiResource(
operations: [
new Get(
uriTemplate: '/unsubscribe/{token}',
formats: [
'jsonld' => ['application/ld+json'],
'json' => ['application/json'],
'html' => ['text/html'], // This causes the issue
],
),
],
)]
class UnsubscribedEmail {}
Current Behavior
The OpenAPI export generates:
paths:
/api/unsubscribe/{token}:
get:
responses:
'200':
content:
application/ld+json:
schema:
$ref: '#/components/schemas/UnsubscribedEmail.jsonld'
application/json:
schema:
$ref: '#/components/schemas/UnsubscribedEmail'
text/html:
schema:
$ref: '#/components/schemas/UnsubscribedEmail.html' # Makes no sense
components:
schemas:
UnsubscribedEmail.html: # JSON schema for HTML format - meaningless
type: object
properties:
status:
type: string
Expected Behavior
Non-JSON formats should either:
- Not have a
schemareference in OpenAPI - Or be excluded from the OpenAPI spec entirely (content negotiation still works at runtime)
Root Cause
In src/OpenApi/Factory/OpenApiFactory.php, the getMimeTypes() method returns ALL output formats, and the schema generation loop creates schemas for each:
// Lines 267-273
foreach ($responseMimeTypes as $operationFormat) {
$operationOutputSchema = $this->jsonSchemaFactory->buildSchema(
$resourceClass,
$operationFormat, // <-- Includes 'html', 'xml', etc.
Schema::TYPE_OUTPUT,
$operation,
$schema,
null,
$forceSchemaCollection
);
$operationOutputSchemas[$operationFormat] = $operationOutputSchema;
}
Proposed Fix
Use the existing jsonschema_formats configuration parameter to filter which formats get JSON schemas in OpenAPI.
Step 1: Inject jsonschema_formats into OpenApiFactory
In src/Symfony/Bundle/Resources/config/openapi.php:
$services->set('api_platform.openapi.factory', OpenApiFactory::class)
->args([
// ... existing args ...
'%api_platform.jsonschema_formats%', // Add this parameter
]);
Step 2: Update OpenApiFactory constructor
public function __construct(
// ... existing parameters ...
private readonly array $jsonSchemaFormats = [],
) {
// ...
}
Step 3: Filter formats in schema generation loop
// Around line 267
foreach ($responseMimeTypes as $mimeType => $operationFormat) {
// Skip formats that don't have JSON schema support
if (!isset($this->jsonSchemaFormats[$operationFormat])) {
continue;
}
$operationOutputSchema = $this->jsonSchemaFactory->buildSchema(
$resourceClass,
$operationFormat,
Schema::TYPE_OUTPUT,
$operation,
$schema,
null,
$forceSchemaCollection
);
$operationOutputSchemas[$operationFormat] = $operationOutputSchema;
}
Step 4: Update buildContent to handle missing schemas
private function buildContent(array $responseMimeTypes, array $operationSchemas): \ArrayObject
{
$content = new \ArrayObject();
foreach ($responseMimeTypes as $mimeType => $format) {
// Only add content entry if we have a schema for this format
if (isset($operationSchemas[$format])) {
$content[$mimeType] = new MediaType(
schema: new \ArrayObject($operationSchemas[$format]->getArrayCopy(false))
);
}
// Non-JSON formats without schemas are simply not included in OpenAPI content
}
return $content;
}
Alternative Consideration
The buildContent method could instead add the content type without a schema reference:
if (isset($operationSchemas[$format])) {
$content[$mimeType] = new MediaType(schema: new \ArrayObject($operationSchemas[$format]->getArrayCopy(false)));
} else {
// Include the content type but without a schema (valid in OpenAPI 3.1)
$content[$mimeType] = new MediaType();
}
This preserves the information that the endpoint supports HTML responses, just without a JSON schema.
Testing
Add a test case to tests/OpenApi/Factory/OpenApiFactoryTest.php:
public function testNonJsonFormatsDoNotGenerateSchemas(): void
{
// Create a resource with html format
// Assert that UnsubscribedEmail.html schema does not exist
// Assert that text/html content type either has no schema or is not present
}
Configuration Reference
The jsonschema_formats is already computed in ApiPlatformExtension.php:
$jsonSchemaFormats = $config['jsonschema_formats'];
if (!$jsonSchemaFormats) {
foreach (array_merge(array_keys($formats), array_keys($errorFormats)) as $f) {
// Only JSON-based formats get schemas by default
if (str_starts_with($f, 'json')) {
$jsonSchemaFormats[$f] = true;
}
}
}
This logic already correctly identifies JSON formats - it just needs to be used in OpenApiFactory.
- 主要語言
- PHP
- 星號
- 2.6k
- 分支
- 984
- 平均合併
- 1 天 14 小時
- 30 天內合併 PR
- 87
環境準備
- 沒有 Dockerfile 或 Docker Compose 檔案
- 有 Pull Request 範本
- 閱讀貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
api-platform/core 的其他 Issue
-
難度 2/5 1-3 小時 新手友好度 78/100
api-platform/core#8626 ·
維護者通常 1 天內回覆
-
難度 2/5 1-3 小時 新手友好度 88/100
api-platform/core#8612 ·
維護者通常 1 天內回覆
-
難度 4/5 3-5 天 新手友好度 38/100
api-platform/core#8591 ·
維護者通常 1 天內回覆
-
難度 4/5 3-5 天 新手友好度 48/100
api-platform/core#8535 · 1 則留言 ·
維護者通常 1 天內回覆
-
難度 3/5 1-2 天 新手友好度 58/100
api-platform/core#8499 · 1 則留言 ·
維護者通常 1 天內回覆
查看 api-platform/core 的全部 Issue
相似的 Issue
-
sync-en
難度 2/5 1-2 天 新手友好度 84/100
維護者通常 2 天內回覆
-
feature-request needs-triage
難度 2/5 1-3 小時 新手友好度 72/100
aws/aws-sdk-php#3365 ·
維護者通常 1 天內回覆
-
難度 1/5 1-3 小時 新手友好度 90/100
-
bug status: unverified
難度 2/5 1-3 小時 新手友好度 78/100
維護者通常 1 天內回覆
-
難度 2/5 1-3 小時 新手友好度 78/100
woocommerce/google-listings-and-ads#3884 ·
維護者通常 1 天內回覆