schmunk42/openapi-json-schema-bundle
最新稳定版本:0.1.0
Composer 安装命令:
composer require schmunk42/openapi-json-schema-bundle
包简介
Symfony bundle for integrating JSON Schema validation with API Platform OpenAPI documentation via dynamic schema aggregation
README 文档
README
A Symfony bundle that integrates JSON Schema validation with API Platform's OpenAPI documentation. This bundle allows you to dynamically populate OpenAPI schemas for JSON fields by aggregating schemas from multiple providers.
Features
- Dynamic JSON Schema Integration: Automatically inject JSON schemas into API Platform OpenAPI documentation
- Multiple Schema Providers: Aggregate schemas from multiple sources using a registry pattern
- anyOf Schema Composition: Combine multiple schemas into a unified schema with JSON Schema's
anyOf - PHP 8 Attributes: Use simple
#[JsonSchema]attribute to mark JSON fields for schema integration - Entity-Level Schema Control: Entities can provide dynamic schemas via
JsonSchemaProviderInterface - Auto-Discovery: Schema providers are automatically registered via Symfony's service tagging
- Caching: Built-in caching for unified schemas to improve performance
- Configurable: Schema base path and cache TTL are configurable
Installation
1. Add to composer.json
Since this is an internal bundle in the extensions/ directory, add autoloading in your composer.json:
{
"autoload": {
"psr-4": {
"Schmunk42\\OpenApiJsonSchema\\": "extensions/openapi-json-schema-bundle/src/"
}
}
}
Run composer dump-autoload:
composer dump-autoload
2. Register Bundle
Add the bundle to config/bundles.php:
return [ // ... Schmunk42\OpenApiJsonSchema\Schmunk42OpenApiJsonSchemaBundle::class => ['all' => true], ];
3. Configure (Optional)
Create config/packages/schmunk42_open_api_json_schema.yaml:
schmunk42_open_api_json_schema: schema_base_path: '%kernel.project_dir%/config/schemas' # Default cache_ttl: 3600 # Cache TTL in seconds (default: 1 hour)
Basic Usage
Simple Example: Tags Array
Here's a simple example of adding JSON schema validation to a tags field:
use ApiPlatform\Metadata\ApiResource; use Doctrine\ORM\Mapping as ORM; use Schmunk42\OpenApiJsonSchema\Attribute\JsonSchema; #[ApiResource] #[ORM\Entity] class Article { #[ORM\Id] #[ORM\Column(type: 'string')] private string $id; #[ORM\Column(type: 'string')] private string $title; #[ORM\Column(type: 'json')] #[JsonSchema('article-tags.json')] private array $tags = []; }
Create config/schemas/article-tags.json:
{
"$schema": "https://json-schema.org/draft-07/schema#",
"type": "array",
"items": {
"type": "string",
"minLength": 1,
"maxLength": 50
},
"uniqueItems": true,
"maxItems": 10
}
The Power of the Annotation
Key Concept: #[ORM\Column(type: 'json')] + #[JsonSchema] = Full JSON Schema in OpenAPI
The bundle automatically injects your JSON schema into the OpenAPI documentation. This means:
- Your database column stores validated JSON data
- Your API documentation shows the exact schema structure
- Clients can see what data structure is expected
IMPORTANT: Including schemas this way still produces fully valid JSON schemas, which are fully compatible with OpenAPI 3.1.
OpenAPI Output
With the example above, your OpenAPI schema will include:
{
"Article": {
"type": "object",
"properties": {
"id": {
"type": "string"
},
"title": {
"type": "string"
},
"tags": {
"type": "array",
"items": {
"type": "string",
"minLength": 1,
"maxLength": 50
},
"uniqueItems": true,
"maxItems": 10
}
}
}
}
Dynamic Schemas from Entity Method
For schemas that require runtime logic, use the #[JsonSchema] attribute without a path and implement JsonSchemaProviderInterface:
use ApiPlatform\Metadata\ApiResource; use Doctrine\ORM\Mapping as ORM; use Schmunk42\OpenApiJsonSchema\Attribute\JsonSchema; use Schmunk42\OpenApiJsonSchema\Interface\JsonSchemaProviderInterface; #[ApiResource] #[ORM\Entity] class Report implements JsonSchemaProviderInterface { #[ORM\Id] #[ORM\Column(type: 'string')] private string $id; #[ORM\Column(type: 'json')] #[JsonSchema] // No path - uses getJsonSchema() method private array $filters = []; public static function getJsonSchema(string $fieldName): ?array { if ($fieldName === 'filters') { $firstDayOfMonth = (new \DateTime('first day of this month'))->format('Y-m-d'); return [ 'type' => 'object', 'properties' => [ 'dateFrom' => [ 'type' => 'string', 'format' => 'date', 'default' => $firstDayOfMonth, 'description' => 'Start date for report (defaults to first day of current month)' ], 'dateTo' => [ 'type' => 'string', 'format' => 'date', 'description' => 'End date for report' ] ], 'required' => ['dateFrom', 'dateTo'] ]; } return null; } }
This approach is useful when:
- Schema needs runtime-calculated values (like dates)
- Schema structure depends on application state
- You want to keep schema logic close to the entity
Advanced Usage
For complex scenarios including:
- Creating schema providers for unified schemas
- Schema validation
- Cache management
- Using unified schemas in entities
See Advanced Usage Documentation
Architecture
Components
-
SchemaRegistry (
Service/SchemaRegistry.php)- Central registry for schema providers
- Aggregates schemas into unified
anyOfstructure - Handles caching
-
JsonFieldSchemaDecorator (
OpenApi/JsonFieldSchemaDecorator.php)- Decorates API Platform's schema factory
- Processes
#[JsonSchema]attributes - Injects schemas into OpenAPI documentation
-
Interfaces
SchemaProviderInterface: For external schema sourcesJsonSchemaProviderInterface: For entity-level dynamic schemas
-
Attribute
#[JsonSchema]: Marks fields for schema injection
How It Works
-
Service Registration: Schema providers implementing
SchemaProviderInterfaceare auto-tagged withschmunk42_open_api_json_schema.provider -
Registry Population:
SchemaRegistryreceives all tagged providers via tagged iterator -
Schema Aggregation: When
getUnifiedSchema()is called, registry loads all provider schemas and combines them usinganyOf -
OpenAPI Integration:
JsonFieldSchemaDecoratordecorates API Platform's schema factory and processes entities during OpenAPI generation -
Attribute Processing: Fields with
#[JsonSchema]attribute get their schema from:- File path (if provided in attribute)
- Entity's
getJsonSchema()method (if implementsJsonSchemaProviderInterface) - SchemaRegistry (if entity uses unified schema)
Requirements
- PHP 8.1+
- Symfony 7.0+
- API Platform 3.0+
- opis/json-schema (for validation)
License
Proprietary - herzog kommunikation GmbH
Credits
Developed for the ZA7 (Zentrales Agentursystem Version 7) project.
schmunk42/openapi-json-schema-bundle 适用场景与选型建议
schmunk42/openapi-json-schema-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 11 次下载、GitHub Stars 达 1, 最近一次更新时间为 2025 年 11 月 23 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「symfony」 「validation」 「swagger」 「api-platform」 「openapi」 「json-schema」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 schmunk42/openapi-json-schema-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 schmunk42/openapi-json-schema-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 schmunk42/openapi-json-schema-bundle 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
The bundle for easy using json-rpc api on your project
Adds request-parameter validation to the SLIM 3.x PHP framework
Bundle Symfony DaplosBundle
Swagger integration to Laravel 5
Easy Swagger endpoint using the Zend Expressive routes config
A jQuery augmented PHP library for creating and validating HTML forms
统计信息
- 总下载量: 11
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 23
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-11-23