@@ -80,7 +80,13 @@ public static function generate(string $name, string $namespace, string $classNa
8080 )
8181 );
8282 $ setDefaylt = true ;
83+ $ nullable = '' ;
8384 if (is_string ($ property ->type )) {
85+ if (is_array ($ schema ->required ) && !in_array ($ propertyName , $ schema ->required , false )) {
86+ $ nullable = '? ' ;
87+ $ propertyStmt ->setDefault (null );
88+ }
89+
8490 if ($ property ->type === 'array ' && $ property ->items instanceof OpenAPiSchema) {
8591 if (array_key_exists (spl_object_hash ($ property ->items ), $ schemaClassNameMap )) {
8692 $ methodDocBlock [] = '@return array< \\' . $ rootNamespace . '\\' . $ schemaClassNameMap [spl_object_hash ($ property ->items )] . '> ' ;
@@ -110,36 +116,31 @@ public static function generate(string $name, string $namespace, string $classNa
110116 'bool ' ,
111117 ], $ property ->type );
112118
113- if ($ t !== '' && is_array ($ schema ->required ) && !in_array ($ propertyName , $ schema ->required , false )) {
114- $ t = '? ' . $ t ;
115- $ propertyStmt ->setDefault (null );
116- }
117-
118119 if ($ t !== '' ) {
119- $ propertyStmt ->setType ($ t );
120- $ method ->setReturnType ($ t );
120+ $ propertyStmt ->setType ($ nullable . $ t );
121+ $ method ->setReturnType ($ nullable . $ t );
121122 }
122123 }
123124 }
124125
125126 if (is_array ($ property ->anyOf ) && $ property ->anyOf [0 ] instanceof OpenAPiSchema && array_key_exists (spl_object_hash ($ property ->anyOf [0 ]), $ schemaClassNameMap )) {
126- $ fqcnn = '\\' . $ rootNamespace . '\\' . $ schemaClassNameMap [spl_object_hash ($ property ->anyOf [0 ])];
127- $ propertyStmt ->setType ( $ fqcnn );
128- $ method ->setReturnType ( $ fqcnn );
127+ $ fqcnn = $ nullable . '\\' . $ rootNamespace . '\\' . $ schemaClassNameMap [spl_object_hash ($ property ->anyOf [0 ])];
128+ $ propertyStmt ->setType ($ fqcnn );
129+ $ method ->setReturnType ($ fqcnn );
129130 $ propertyDocBlock [] = '@\WyriHaximus\Hydrator\Attribute\Hydrate( ' . $ fqcnn . '::class) ' ;
130131 $ setDefaylt = false ;
131132 } else if (is_array ($ property ->allOf ) && $ property ->allOf [0 ] instanceof OpenAPiSchema && array_key_exists (spl_object_hash ($ property ->allOf [0 ]), $ schemaClassNameMap )) {
132- $ fqcnn = '\\' . $ rootNamespace . '\\' . $ schemaClassNameMap [spl_object_hash ($ property ->allOf [0 ])];
133- $ propertyStmt ->setType ( $ fqcnn );
134- $ method ->setReturnType ( $ fqcnn );
133+ $ fqcnn = $ nullable . '\\' . $ rootNamespace . '\\' . $ schemaClassNameMap [spl_object_hash ($ property ->allOf [0 ])];
134+ $ propertyStmt ->setType ($ fqcnn );
135+ $ method ->setReturnType ($ fqcnn );
135136 $ propertyDocBlock [] = '@\WyriHaximus\Hydrator\Attribute\Hydrate( ' . $ fqcnn . '::class) ' ;
136137 $ setDefaylt = false ;
137138 }
138139
139140 if ($ property ->type === 'object ' && $ property instanceof OpenAPiSchema && array_key_exists (spl_object_hash ($ property ), $ schemaClassNameMap )) {
140- $ fqcnn = '\\' . $ rootNamespace . '\\' . $ schemaClassNameMap [spl_object_hash ($ property )];
141- $ propertyStmt ->setType ( $ fqcnn );
142- $ method ->setReturnType ( $ fqcnn );
141+ $ fqcnn = $ nullable . '\\' . $ rootNamespace . '\\' . $ schemaClassNameMap [spl_object_hash ($ property )];
142+ $ propertyStmt ->setType ($ fqcnn );
143+ $ method ->setReturnType ($ fqcnn );
143144 $ propertyDocBlock [] = '@\WyriHaximus\Hydrator\Attribute\Hydrate( ' . $ fqcnn . '::class) ' ;
144145 $ setDefaylt = false ;
145146 }
0 commit comments