55namespace Spiral \JsonSchemaGenerator \Schema ;
66
77use Spiral \JsonSchemaGenerator \Exception \InvalidTypeException ;
8+ use Spiral \JsonSchemaGenerator \Parser \UnionType ;
89
910final readonly class Property implements \JsonSerializable
1011{
1112 public PropertyOptions $ options ;
1213
1314 /**
14- * @param Type|class-string $type
15+ * @param Type|class-string|UnionType $type
1516 * @param array<class-string|Type> $options
1617 */
1718 public function __construct (
18- public Type |string $ type ,
19+ public Type |string | UnionType $ type ,
1920 array $ options = [],
2021 public string $ title = '' ,
2122 public string $ description = '' ,
@@ -44,8 +45,25 @@ public function jsonSerialize(): array
4445 $ property ['default ' ] = $ this ->default ;
4546 }
4647
48+ // Handle UnionType instance
49+ if ($ this ->type instanceof UnionType) {
50+ $ unionOptions = [];
51+ foreach ($ this ->type ->getTypes () as $ unionType ) {
52+ $ typeName = $ unionType ->getName ();
53+ if (\is_string ($ typeName ) && !$ unionType ->isBuiltin ()) {
54+ // Class reference
55+ $ unionOptions [] = ['$ref ' => (new Reference ($ typeName ))->jsonSerialize ()];
56+ } else {
57+ // Primitive type
58+ $ unionOptions [] = ['type ' => $ typeName instanceof Type ? $ typeName ->value : $ typeName ];
59+ }
60+ }
61+ $ property ['oneOf ' ] = $ unionOptions ;
62+ return $ property ;
63+ }
64+
4765 if ($ this ->type === Type::Union) {
48- $ property ['anyOf ' ] = $ this ->options ->jsonSerialize ();
66+ $ property ['oneOf ' ] = $ this ->options ->jsonSerialize ();
4967 return $ property ;
5068 }
5169
@@ -67,7 +85,7 @@ public function jsonSerialize(): array
6785
6886 $ property ['items ' ]['type ' ] = $ this ->options [0 ]->value ->value ;
6987 } else {
70- $ property ['items ' ]['anyOf ' ] = $ this ->options ->jsonSerialize ();
88+ $ property ['items ' ]['oneOf ' ] = $ this ->options ->jsonSerialize ();
7189 }
7290 }
7391
@@ -77,6 +95,17 @@ public function jsonSerialize(): array
7795 public function getDependencies (): array
7896 {
7997 $ dependencies = [];
98+
99+ // Extract dependencies from union types
100+ if ($ this ->type instanceof UnionType) {
101+ foreach ($ this ->type ->getTypes () as $ unionType ) {
102+ $ typeName = $ unionType ->getName ();
103+ if (!$ unionType ->isBuiltin () && \is_string ($ typeName )) {
104+ $ dependencies [] = $ typeName ;
105+ }
106+ }
107+ }
108+
80109 foreach ($ this ->options ->getOptions () as $ option ) {
81110 if (\is_string ($ option ->value )) {
82111 $ dependencies [] = $ option ->value ;
0 commit comments