1414use Nelmio \ApiDocBundle \Describer \ModelRegistryAwareInterface ;
1515use Nelmio \ApiDocBundle \ModelDescriber \ModelDescriberInterface ;
1616use Nelmio \ApiDocBundle \OpenApiPhp \Util ;
17+ use Nelmio \ApiDocBundle \Util \LegacyTypeConverter ;
1718use OpenApi \Annotations as OA ;
1819use Psr \Log \LoggerAwareTrait ;
1920use Psr \Log \NullLogger ;
20- use Symfony \Component \PropertyInfo \Type ;
21+ use Symfony \Component \PropertyInfo \Type as LegacyType ;
22+ use Symfony \Component \TypeInfo \Type ;
2123
2224final class ModelRegistry
2325{
@@ -74,7 +76,7 @@ public function __construct($modelDescribers, OA\OpenApi $api, array $alternativ
7476
7577 foreach ($ alternativeNames as $ alternativeName => $ criteria ) {
7678 $ model = new Model (
77- new Type ( ' object ' , false , $ criteria ['type ' ]),
79+ LegacyTypeConverter:: createType ( $ criteria ['type ' ]),
7880 $ criteria ['groups ' ],
7981 $ criteria ['options ' ] ?? [],
8082 $ criteria ['serializationContext ' ] ?? [],
@@ -170,9 +172,13 @@ public function registerSchemas(): void
170172 $ schema = $ this ->describeSchema ($ model , $ name );
171173
172174 if (null === $ schema ) {
173- $ errorMessage = \sprintf ('Schema of type "%s" can \'t be generated, no describer supports it. ' , $ this ->typeToString ($ model ->getType ()));
174- if (Type::BUILTIN_TYPE_OBJECT === $ model ->getType ()->getBuiltinType () && !class_exists ($ className = $ model ->getType ()->getClassName ())) {
175- $ errorMessage .= \sprintf (' Class " \\%s" does not exist, did you forget a use statement, or typed it wrong? ' , $ className );
175+ $ type = class_exists (Type::class)
176+ ? $ model ->getTypeInfo ()
177+ : $ model ->getType ();
178+
179+ $ errorMessage = \sprintf ('Schema of type "%s" can \'t be generated, no describer supports it. ' , $ this ->typeToString ($ type ));
180+ if (method_exists ($ type , 'getClassName ' ) && null !== $ type ->getClassName () && !class_exists ($ className = $ type ->getClassName ())) {
181+ $ errorMessage .= \sprintf (' Class "%s" does not exist, did you forget a use statement, or typed it wrong? ' , $ className );
176182 }
177183 throw new \LogicException ($ errorMessage );
178184 }
@@ -194,8 +200,12 @@ private function determineModelName(Model $model): string
194200 return $ model ->name ;
195201 }
196202
203+ $ type = class_exists (Type::class)
204+ ? $ model ->getTypeInfo ()
205+ : $ model ->getType ();
206+
197207 // 3. Generate from the type
198- return $ this ->getTypeShortName ($ model -> getType () );
208+ return $ this ->getTypeShortName ($ type );
199209 }
200210
201211 private function generateUniqueModelName (Model $ model ): string
@@ -225,32 +235,45 @@ private function generateUniqueModelName(Model $model): string
225235 */
226236 private function modelToArray (Model $ model ): array
227237 {
228- $ getType = function (Type $ type ) use (&$ getType ): array {
229- return [
230- 'class ' => $ type ->getClassName (),
231- 'built_in_type ' => $ type ->getBuiltinType (),
232- 'nullable ' => $ type ->isNullable (),
233- 'collection ' => $ type ->isCollection (),
234- 'collection_key_types ' => $ type ->isCollection () ? array_map ($ getType , $ type ->getCollectionKeyTypes ()) : null ,
235- 'collection_value_types ' => $ type ->isCollection () ? array_map ($ getType , $ type ->getCollectionValueTypes ()) : null ,
236- ];
237- };
238+ $ type = class_exists (Type::class)
239+ ? $ model ->getTypeInfo ()
240+ : $ model ->getType ();
241+
242+ $ dataType = $ this ->typeToString ($ type );
238243
239244 return [
240- 'type ' => $ getType ( $ model -> getType ()) ,
245+ 'type ' => $ dataType ,
241246 'options ' => $ model ->getOptions (),
242247 'groups ' => $ model ->getGroups (),
243248 'serialization_context ' => $ model ->getSerializationContext (),
244249 ];
245250 }
246251
247- private function getTypeShortName (Type $ type ): string
252+ private function getTypeShortName (LegacyType | Type $ type ): string
248253 {
249- if (null !== $ collectionType = $ this ->getCollectionValueType ($ type )) {
254+ if ($ type instanceof Type) {
255+ if ($ type instanceof Type \CollectionType) {
256+ return $ this ->getTypeShortName ($ type ->getCollectionValueType ()).'[] ' ;
257+ }
258+
259+ if ($ type instanceof Type \ObjectType) {
260+ $ parts = explode ('\\' , $ type ->getClassName ());
261+
262+ return end ($ parts );
263+ }
264+
265+ if ($ type instanceof Type \NullableType) {
266+ return $ this ->getTypeShortName ($ type ->getWrappedType ());
267+ }
268+
269+ return $ type ->__toString ();
270+ }
271+
272+ if (null !== $ collectionType = ($ type ->getCollectionValueTypes ()[0 ] ?? null )) {
250273 return $ this ->getTypeShortName ($ collectionType ).'[] ' ;
251274 }
252275
253- if (Type ::BUILTIN_TYPE_OBJECT === $ type ->getBuiltinType ()) {
276+ if (LegacyType ::BUILTIN_TYPE_OBJECT === $ type ->getBuiltinType ()) {
254277 $ parts = explode ('\\' , $ type ->getClassName ());
255278
256279 return end ($ parts );
@@ -259,23 +282,22 @@ private function getTypeShortName(Type $type): string
259282 return $ type ->getBuiltinType ();
260283 }
261284
262- private function typeToString (Type $ type ): string
285+ private function typeToString (LegacyType | Type $ type ): string
263286 {
264- if (Type::BUILTIN_TYPE_OBJECT === $ type ->getBuiltinType ()) {
265- return '\\' .$ type ->getClassName ();
287+ if ($ type instanceof Type) {
288+ return $ type ->__toString ();
289+ }
290+
291+ if (LegacyType::BUILTIN_TYPE_OBJECT === $ type ->getBuiltinType ()) {
292+ return $ type ->getClassName ();
266293 } elseif ($ type ->isCollection ()) {
267- if (null !== $ collectionType = $ this -> getCollectionValueType ( $ type )) {
294+ if (null !== $ collectionType = ( $ type -> getCollectionValueTypes ()[ 0 ] ?? null )) {
268295 return $ this ->typeToString ($ collectionType ).'[] ' ;
269296 } else {
270297 return 'mixed[] ' ;
271298 }
272- } else {
273- return $ type ->getBuiltinType ();
274299 }
275- }
276300
277- private function getCollectionValueType (Type $ type ): ?Type
278- {
279- return $ type ->getCollectionValueTypes ()[0 ] ?? null ;
301+ return $ type ->getBuiltinType ();
280302 }
281303}
0 commit comments