Repository: nelmio/NelmioApiDocBundle (PHP)

Issue:
[Bug]: Alternative names seem to be processed wrong
### Version

5.6.0

### Description

I'm using the config file, to set alias names for some types, some of them depend on each other, so I first define the type without a dependency, then I define the type with the dependency.

Also the [comment in the constructor](https://github.com/nelmio/NelmioApiDocBundle/blob/5.x/src/Model/ModelRegistry.php#L75) seems to be false, at least in my case, since the last defined name is used, not the first.

It's a bit weird to explain in words, so please see the reproducer:
https://github.com/Sajito/nelmio_api_doc_alternative_names_reproducer

I'd expect to get the schemas `File` and `Folder`.
Instead I get `FileDto` and `FolderFoo`.

### JSON OpenApi

<details><summary>JSON OpenApi</summary>

```json

{
    "openapi": "3.0.0",
    "info": {
        "title": "My App",
        "description": "This is an awesome app!",
        "version": "1.0.0"
    },
    "paths": {
        "/api/folder/{name}": {
            "get": {
                "operationId": "get_app_folder_getfolders",
                "parameters": [
                    {
                        "name": "name",
                        "in": "path",
                        "required": true,
                        "schema": {
                            "type": "string"
                        }
                    }
                ],
                "responses": {
                    "200": {
                        "description": "",
                        "content": {
                            "application/json": {
                                "schema": {
                                    "$ref": "#/components/schemas/FolderFoo"
                                }
                            }
                        }
                    }
                }
            }
        }
    },
    "components": {
        "schemas": {
            "FileDto": {
                "required": [
                    "name",
                    "path"
                ],
                "properties": {
                    "name": {
                        "type": "string"
                    },
                    "path": {
                        "type": "string"
                    }
                },
                "type": "object"
            },
            "FolderFoo": {
                "required": [
                    "name",
                    "files"
                ],
                "properties": {
                    "name": {
                        "type": "string"
                    },
                    "files": {
                        "type": "array",
                        "items": {
                            "$ref": "#/components/schemas/FileDto"
                        }
                    }
                },
                "type": "object"
            }
        }
    }
}

```

</details>


### Additional context

_No response_


Discussion:
Thanks for the report! 😄 



Patch:
diff --git a/src/Model/ModelRegistry.php b/src/Model/ModelRegistry.php
index db54cf4da..ebccb31e1 100644
--- a/src/Model/ModelRegistry.php
+++ b/src/Model/ModelRegistry.php
@@ -72,8 +72,7 @@ public function __construct($modelDescribers, OA\OpenApi $api, array $alternativ
         $this->api = $api;
         $this->logger = new NullLogger();
 
-        // The array is reversed to ensure that the first defined alternative name for a model is used.
-        foreach (array_reverse($alternativeNames) as $alternativeName => $criteria) {
+        foreach ($alternativeNames as $alternativeName => $criteria) {
             $model = new Model(
                 new Type('object', false, $criteria['type']),
                 $criteria['groups'],


Test patch:
diff --git a/tests/Model/ModelRegistryTest.php b/tests/Model/ModelRegistryTest.php
index 5522fc266..34cef0e2c 100644
--- a/tests/Model/ModelRegistryTest.php
+++ b/tests/Model/ModelRegistryTest.php
@@ -262,6 +262,22 @@ public static function getNameAlternatives(): \Generator
                 ],
             ],
         ];
+
+        yield 'ensure first defined alternative name is used' => [
+            '#/components/schemas/Foo1',
+            ['group1'],
+            null,
+            [
+                'Foo1' => [
+                    'type' => self::class,
+                    'groups' => ['group1'],
+                ],
+                'Foo2' => [
+                    'type' => self::class,
+                    'groups' => ['group1'],
+                ],
+            ],
+        ];
     }
 
     public function testMultipleSchemasSameCustomName(): void