This repository was archived by the owner on Sep 25, 2025. It is now read-only.
Add class-level Definition and AdditionalProperty attributes#1
Open
butschster wants to merge 5 commits into2.xfrom
Open
Add class-level Definition and AdditionalProperty attributes#1butschster wants to merge 5 commits into2.xfrom
butschster wants to merge 5 commits into2.xfrom
Conversation
- Add Definition attribute for class-level schema metadata - Add AdditionalProperty attribute for custom schema properties - Update Schema class to support metadata and additional properties - Update Generator to process both new attributes - Add comprehensive unit tests - Add usage examples
…ool`) in the JSON Schema Generator. Union types are now properly represented in JSON Schema using the `oneOf` keyword.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR adds support for class-level schema definition through two new attributes:
New Features
Definition Attribute: A class-level attribute that provides core schema metadata:
title: Schema titledescription: Schema descriptionid: Schema $idschemaVersion: Schema version ($schema)AdditionalProperty Attribute: A repeatable class-level attribute for adding any custom schema properties:
name: Property namevalue: Property value (supports scalar, array, and object values)Schema Class Updates:
Benefits
Example Usage
#[Definition( title: 'Product Schema', description: 'A product in the catalog', id: 'https://example.com/schemas/product.json', schemaVersion: 'http://json-schema.org/draft-07/schema#' )] #[AdditionalProperty(name: 'additionalProperties', value: false)] #[AdditionalProperty(name: 'examples', value: [ [ 'id' => 123, 'name' => 'Sample Product', 'price' => 99.99, 'inStock' => true ] ])] class Product { // Class properties with Field attributes }Support for PHP union types using oneOf in JSON Schema
This PR adds support for PHP 8.0+ union types (like
string|int|bool) to the JSON Schema Generator. The implementation represents union types in JSON Schema using the standardoneOfconstruct.Problem
Previously, the library couldn't properly handle PHP union types (
string|int), which are a key feature of modern PHP typing. When generating JSON schemas from classes with union types, the schema wouldn't accurately represent these polymorphic types.Solution
UnionTypeclass to represent PHP union typesTypeParserto extract and parse PHP reflection typesPropertyclass to serialize union types usingoneOfGeneratorproperly handles all union type scenariosExamples
PHP class with union types:
Generated JSON Schema:
{ "properties": { "stringOrInt": { "oneOf": [ { "type": "string" }, { "type": "integer" } ] }, "multiType": { "oneOf": [ { "type": "string" }, { "type": "integer" }, { "type": "boolean" }, { "type": "null" } ] }, "objectUnion": { "oneOf": [ { "$ref": "#/definitions/ClassA" }, { "$ref": "#/definitions/ClassB" }, { "type": "null" } ] } }, "required": ["stringOrInt"] }