andreaselia / laravel-api-to-postman

Generate a Postman collection automatically from your Laravel API
MIT License
938 stars 99 forks source link

Routes are not grouped properly into a subdirectory. #109

Open mikevandiepen opened 1 month ago

mikevandiepen commented 1 month ago

When exporting the postman suite my routes are not grouped "correctly", these routes:

ROUTE: api/v1/versions/{version}/equipment/{equipment}/accessories NAME: versions.equipment.equipment-accessories.index

ROUTE: api/v1/versions/{version}/equipment/{equipment}/accessories NAME: versions.equipment.equipment-accessories.sync

ROUTE: api/v1/versions/{version}/equipment/{equipment}/accessories/{accessory} NAME: versions.equipment.equipment-accessories.attach

ROUTE: api/v1/versions/{version}/equipment/{equipment}/accessories/{accessory} NAME: versions.equipment.equipment-accessories.detach

Actual output: image

Expected output: image

I believe this issue is caused by mapping it using the "namespace" instead of the route-name?

This is my controller:


<?php

namespace App\Http\Controllers;

class EquipmentEquipmentAccessoryController extends Controller
{
    public function index(Version $version, Equipment $equipment): Response {}
    public function sync(Version $version, Equipment $equipment, Request $request): Response {}
    public function attach(Version $version, Equipment $equipment, EquipmentAccessory $accessory): Response {}
    public function detach(Version $version, Equipment $equipment, EquipmentAccessory $accessory): Response {}
}
andreaselia commented 1 month ago

Hey @mikevandiepen,

They look grouped correctly to me. Each route is under "equipment-accessories" correctly and them being within folders as per the end of the route name makes it easily readable at a glance which route belongs to which without having to can your eyes across the length of the entire route.

mikevandiepen commented 1 month ago

What I mean is the four separate requests belong to the same controller and are grouped to the same route-name, only a different identifier (..*.{x,y,z}). These requests are put in separate folders instead of grouped inside the parent, but if it is intentional then I understand it, it just threw me off haha

andreaselia commented 1 month ago

Definitely understand, and it is indeed intentional so that at a glance you can see which route request is the "attach" one for example, or the "sync" one and so on, without having to put much thought into the request method (like PUT) or having to take a longer look at the route name.

I'd be open to a PR to add a config option for flattening this structure though if it were something you'd like to take on?

mikevandiepen commented 1 month ago

If you can point me to the right direction I might try this weekend! A little clue could be a nice headstart :)

andreaselia commented 1 month ago

Sure thing, @mikevandiepen.

You'll probably want to add a config option like 'flattened_routes' => false, (default to false), and if it's true, then you'll want to avoid adding the last route name segment as a nested directory.

This should be roughly the right area, as this is where we handle the other config options related to the route folder conventions.

https://github.com/andreaselia/laravel-api-to-postman/blob/main/src/Processors/RouteProcessor.php#L109

I'd recommend starting with a test case (we have existing ones for ensuring outputs are a certain way already), so you can duplicate one and modify it to suit what you expect the output to be, and then do the code changes necessary to get it to pass.