Search by

symfony / json-schema

fabpot

Generates JSON Schema documents from PHP types.

8.2.x-dev 2026-10-14 10:57 UTC

This package is auto-updated.

Last update: 2026-10-14 16:22:17 UTC


README

The JsonSchema component generates JSON Schema documents from PHP types, for API documentation (OpenAPI) and for describing tool inputs and outputs to AI agents.

This Component is experimental. Experimental features are not covered by Symfony's Backward Compatibility Promise.

Getting Started

composer require symfony/json-schema
use Symfony\Component\JsonSchema\Configuration;
use Symfony\Component\JsonSchema\Dialect;
use Symfony\Component\JsonSchema\SchemaDocument;
use Symfony\Component\JsonSchema\SchemaGenerator;
use Symfony\Component\TypeInfo\Type;

$generator = SchemaGenerator::create();

// {"$schema": "...", "$ref": "#/$defs/Book", "$defs": {"Book": {...}, "Author": {...}}}
$schema = $generator->generate(Type::object(Book::class));

// an OpenAPI 3.1 request body, restricted to the "book:write" serialization group
$schema = $generator->generate(Type::object(Book::class), new Configuration(
    dialect: Dialect::openApi31(),
    groups: ['book:write'],
));
$schema->getRoot();        // ['$ref' => '#/components/schemas/Book-book.write']
$schema->getDefinitions(); // ['Book-book.write' => [...]]

// the schemas of one OpenAPI document, sharing their definitions
$document = new SchemaDocument();
$bookSchema = $generator->generate(Type::object(Book::class), new Configuration(Dialect::openApi31()), $document);
$authorSchema = $generator->generate(Type::object(Author::class), new Configuration(Dialect::openApi31()), $document);
$document->getDefinitions(); // ['Book' => [...], 'Author' => [...]]

// a self-contained schema without any reference, e.g. for an MCP tool
$schema = $generator->generate(Type::object(Book::class))->flatten();

// the input of a tool, described by the parameters of its method
$schema = $generator->generateForParameters((new \ReflectionMethod(SearchBooks::class, '__invoke'))->getParameters());

echo json_encode($schema);

Resources