@@ -80,7 +80,13 @@ public static function generate(string $name, string $namespace, string $classNa
80
80
)
81
81
);
82
82
$ setDefaylt = true ;
83
+ $ nullable = '' ;
83
84
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
+
84
90
if ($ property ->type === 'array ' && $ property ->items instanceof OpenAPiSchema) {
85
91
if (array_key_exists (spl_object_hash ($ property ->items ), $ schemaClassNameMap )) {
86
92
$ methodDocBlock [] = '@return array< \\' . $ rootNamespace . '\\' . $ schemaClassNameMap [spl_object_hash ($ property ->items )] . '> ' ;
@@ -110,36 +116,31 @@ public static function generate(string $name, string $namespace, string $classNa
110
116
'bool ' ,
111
117
], $ property ->type );
112
118
113
- if ($ t !== '' && is_array ($ schema ->required ) && !in_array ($ propertyName , $ schema ->required , false )) {
114
- $ t = '? ' . $ t ;
115
- $ propertyStmt ->setDefault (null );
116
- }
117
-
118
119
if ($ t !== '' ) {
119
- $ propertyStmt ->setType ($ t );
120
- $ method ->setReturnType ($ t );
120
+ $ propertyStmt ->setType ($ nullable . $ t );
121
+ $ method ->setReturnType ($ nullable . $ t );
121
122
}
122
123
}
123
124
}
124
125
125
126
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 );
129
130
$ propertyDocBlock [] = '@\WyriHaximus\Hydrator\Attribute\Hydrate( ' . $ fqcnn . '::class) ' ;
130
131
$ setDefaylt = false ;
131
132
} 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 );
135
136
$ propertyDocBlock [] = '@\WyriHaximus\Hydrator\Attribute\Hydrate( ' . $ fqcnn . '::class) ' ;
136
137
$ setDefaylt = false ;
137
138
}
138
139
139
140
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 );
143
144
$ propertyDocBlock [] = '@\WyriHaximus\Hydrator\Attribute\Hydrate( ' . $ fqcnn . '::class) ' ;
144
145
$ setDefaylt = false ;
145
146
}
0 commit comments