OpenAPI: Support for JSON-LD Only APIs
Maintainer thường phản hồi trong vòng 1 ngày
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 5/5
- Thời gian dự kiến
- Hơn một tuần
- Mức phù hợp với người mới
- 38/100
- Loại issue
- Tính năng
- Độ rõ ràng
- Khá rõ ràng
- Mức độ hoạt động
- Ít trao đổi
- Lĩnh vực
- api, documentation
Hướng nghiên cứu
Bắt đầu với src/Symfony/Bundle/DependencyInjection/Configuration.php và src/OpenApi/Factory/OpenApiFactory.php, đặc biệt là entry point getMimeTypes(). So sánh các cách tiếp cận được đề xuất là prefer_format, formats và tự động loại bỏ trùng lặp trước khi chốt phạm vi. Công việc được xem là hoàn tất khi cấu hình được chọn tạo ra đầu ra OpenAPI chỉ chứa các schema và content type dự kiến, mà không thay đổi các định dạng trong thời gian chạy.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Context
When building a JSON-LD only API (no plain JSON format), the OpenAPI specification still generates duplicate schemas and content types. Users have to create custom OpenApiFactory decorators to filter these out.
Current Behavior
With this configuration:
api_platform:
formats:
jsonld: ['application/ld+json']
error_formats:
jsonld: ['application/ld+json']
jsonproblem: ['application/problem+json']
The OpenAPI export still produces:
- Duplicate schemas:
EmailMessageANDEmailMessage.jsonld - Multiple content types in responses:
application/ld+json,application/problem+json,application/json
Workaround We Implemented
We created an OpenApiFactory decorator that:
1. Filters non-JSON-LD content types from responses
private const NON_JSONLD_MIME_TYPES = [
'text/html',
'application/problem+json',
'application/json',
];
private function filterNonJsonContentTypes(PathItem $pathItem): PathItem
{
foreach (PathItem::$methods as $method) {
$operation = $pathItem->{"get" . ucfirst(strtolower($method))}();
if (null === $operation) continue;
foreach ($operation->getResponses() as $status => $response) {
$content = $response->getContent();
if ($content) {
$filteredContent = new \ArrayObject();
foreach ($content as $mimeType => $mediaType) {
if (!in_array($mimeType, self::NON_JSONLD_MIME_TYPES, true)) {
$filteredContent[$mimeType] = $mediaType;
}
}
// ... update response
}
}
}
return $pathItem;
}
2. Removes duplicate schemas (keeps only .jsonld versions)
private function filterNonJsonSchemas(OpenApi $openApi): OpenApi
{
$schemas = $openApi->getComponents()->getSchemas();
// Collect base names that have .jsonld versions
$jsonldSchemas = [];
foreach ($schemas as $name => $schema) {
if (str_ends_with($name, '.jsonld')) {
$jsonldSchemas[substr($name, 0, -7)] = true;
}
}
$filteredSchemas = new \ArrayObject();
foreach ($schemas as $name => $schema) {
$baseName = preg_replace('/\.jsonld$/', '', $name);
// Skip non-.jsonld version when .jsonld exists
if (!str_ends_with($name, '.jsonld') && isset($jsonldSchemas[$baseName])) {
continue;
}
$filteredSchemas[$name] = $schema;
}
return $openApi->withComponents($components->withSchemas($filteredSchemas));
}
Proposed Framework Enhancement
Option A: openapi.prefer_format Configuration
Add a configuration option to specify the preferred format for OpenAPI documentation:
api_platform:
openapi:
prefer_format: jsonld # Only show jsonld schemas and content types
Implementation:
In OpenApiFactory, when prefer_format is set:
- Only generate schemas for that format (skip others even if configured)
- Only include that format's content type in responses
- Remove duplicate schemas that exist in multiple format variants
Option B: openapi.formats Configuration
Allow explicit control over which formats appear in OpenAPI (separate from runtime formats):
api_platform:
formats:
jsonld: ['application/ld+json']
json: ['application/json'] # Available at runtime
openapi:
formats:
jsonld: ['application/ld+json'] # Only this in docs
Implementation:
In OpenApiFactory::getMimeTypes():
private function getMimeTypes(HttpOperation $operation): array
{
// Use openapi.formats if configured, otherwise fall back to operation formats
$responseFormats = $this->openapiFormats
?? $operation->getOutputFormats()
?? [];
// ... rest of method
}
Option C: Automatic Deduplication
When multiple formats produce equivalent schemas (same structure, different naming), automatically deduplicate:
// In OpenApiFactory::collectPaths() or a new dedicated method
private function deduplicateSchemas(\ArrayObject $schemas): \ArrayObject
{
$dominated = [];
// jsonld dominates json (more specific)
foreach ($schemas as $name => $schema) {
if (str_ends_with($name, '.jsonld')) {
$baseName = substr($name, 0, -7);
if (isset($schemas[$baseName])) {
$dominated[$baseName] = true;
}
}
}
// Remove dominated schemas
foreach ($dominated as $name => $_) {
unset($schemas[$name]);
}
return $schemas;
}
Recommendation
Option A (prefer_format) is the cleanest because:
- Simple, single configuration option
- Clear intent: "I want my OpenAPI docs in this format"
- Doesn't affect runtime behavior
- Easy to understand and document
Example implementation location:
- Configuration:
src/Symfony/Bundle/DependencyInjection/Configuration.php - Logic:
src/OpenApi/Factory/OpenApiFactory.php
Additional Consideration: Accept/Content-Type Headers
Our decorator also adds Accept and Content-Type header parameters to all operations, showing available formats as an enum. This is useful documentation that API Platform could generate automatically.
parameters:
- name: Accept
in: header
schema:
type: string
enum: ['application/ld+json']
default: 'application/ld+json'
This could be another configuration option:
api_platform:
openapi:
document_content_negotiation_headers: true
- Ngôn ngữ chính
- PHP
- Star
- 2.6k
- Fork
- 987
- Merge trung bình
- 1 ngày 7 giờ
- Pull request đã merge (30 ngày)
- 84
Chuẩn bị môi trường
- Không có Dockerfile hay tệp Docker Compose
- Có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của api-platform/core
-
Doctrine\Orm\OrderExtension fails on SortDirectionCó thể đã có người làm Có pull request liên kết đang mở hoặc đã được merge. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
api-platform/core#8660 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
DeserializeProvider calls PartialDenormalizationException::getErrors(), deprecated in Symfony 8.1Có thể đã có người làm Có pull request liên kết đang mở hoặc đã được merge. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
api-platform/core#8650 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
api-platform/core#8649 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
`OrderExtension` and `OrderFilter` pass string sort directions, deprecated since `doctrine/orm` 3.7Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
api-platform/core#8648 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 66/100
api-platform/core#8647 ·
Maintainer thường phản hồi trong vòng 1 ngày
Tất cả issue của api-platform/core
Issue tương tự
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
components-web-app/api-components-bundle#403 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 74/100
mollie/PrestaShop#1566 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
components-web-app/docs#193 ·
-
docs: add Python and PHP examples to docs/api.mdCó thể đã có người làm @gaurika-analyst đã nhận hôm nay. Đang mởdocumentation good first issue
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 85/100
djazairdev/wilayas#12 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Deno Runtime is discontinuedĐang mởbug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
endoflife-date/endoflife.date#11314 ·
Maintainer thường phản hồi trong vòng 1 ngày