andreaselia/laravel-api-to-postman

Error when 'enable_formdata' => true

rcerljenko opened this issue · 11 comments

Hey @rcerljenko, thanks for the report. Can you post the full exception message please? If you would also be so kind as to post the code/function which is causing the exception from your routes/controller.

Hi @andreaselia . I could but this is all I get. It seems to me that you considering the first parameter to be the request instance which is not always the case, eg:

public function update(User $user, Request $request)
{
}

Ah, I see! I'll look into this today.

You could try something like this (maybe tweak it a little):

$rules = collect($route->signatureParameters())->where('name', 'request')->first() ?? [];

if($rules)
{
	$rules = $rules->getType()->getName();
	$rules = new $rules;
	$rules = method_exists($rules, 'rules') ? $rules->rules() : [];
}

Some people may not name the request "request", so what I'll likely do is loop through the parameters and check which one extends FormRequest, this should give us a good indication of which one is the request class.

@andreaselia that should do it!

@rcerljenko I'm having a little trouble reproducing your issue, even swapping the request to not be the first parameter in a function. Could you provide me with some steps to reproduce the issue you're having, please?

@andreaselia I can only provide you with the api.php routes file, here:

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Api\TagController;
use App\Http\Controllers\Api\AuthController;
use App\Http\Controllers\Api\BlogController;
use App\Http\Controllers\Api\ChartController;
use App\Http\Controllers\Api\MediaController;
use App\Http\Controllers\Api\ConfigController;
use App\Http\Controllers\Api\ProfileController;
use App\Http\Controllers\Api\PushDeviceController;
use App\Http\Controllers\Api\NotificationController;
use App\Http\Controllers\Auth\ResetPasswordController;

/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/

Route::name('api.')->group(function ($route) {

	// AUTH - POST
	$route->post('register', [AuthController::class, 'register'])->name('register');

	// AUTH LOGIN ROUTES
	$route->prefix('login')->name('login.')->group(function ($route) {
		$c = AuthController::class;

		// GET
		$route->post('', [$c, 'login'])->name('standard');
		$route->post('{driver}', [$c, 'oauthLogin'])->name('oauth');
	});

	// PASSWORD ROUTES
	$route->prefix('password')->name('password.')->group(function ($route) {

		// POST
		$route->post('reset', [AuthController::class, 'passwordReset'])->name('reset');
		$route->post('change', [ResetPasswordController::class, 'reset'])->name('change');
	});

	// CONFIG ROUTE
	$route->get('config', [ConfigController::class, 'config'])->name('config');

	// BLOG ROUTES
	$route->prefix('blogs')->name('blogs.')->group(function ($route) {
		$c = BlogController::class;

		// GET
		$route->get('', [$c, 'index'])->name('list');
		$route->get('{id}/activities', [$c, 'activities'])->name('activities');
		$route->middleware('record_read_activity')->get('{id}', [$c, 'single'])->name('single');
	});

	// TAG ROUTES
	$route->prefix('tags')->name('tags.')->group(function ($route) {
		$c = TagController::class;

		// GET
		$route->get('', [$c, 'index'])->name('list');
	});

	// JWT PROTECTED ROUTES
	$route->middleware(['check_role_permissions'])->group(function ($route) {
		// PROFILE ROUTES
		$route->prefix('me')->name('me.')->group(function ($route) {
			$c = ProfileController::class;

			// GET
			$route->get('', [$c, 'me'])->name('get');
			$route->get('sessions', [$c, 'sessions'])->name('sessions');

			// POST
			$route->post('', [$c, 'update'])->name('update');
			$route->post('password', [$c, 'updatePassword'])->name('update-password');
			$route->post('avatar', [$c, 'updateAvatar'])->name('update-avatar');

			// DELETE
			$route->delete('', [$c, 'remove'])->name('remove');
			$route->delete('sessions/{id}', [$c, 'removeSession'])->name('remove-session');

			// OAUTH ROUTES
			$route->prefix('oauth')->name('oauth.')->group(function ($route) use ($c) {
				// POST
				$route->post('{driver}', [$c, 'connectOAuth'])->name('connect');

				// DELETE
				$route->delete('{driver}', [$c, 'disconnectOAuth'])->name('disconnect');
			});
		});

		// NOTIFICATION ROUTES
		$route->prefix('notifications')->name('notifications.')->group(function ($route) {
			$c = NotificationController::class;

			// GET
			$route->get('', [$c, 'index'])->name('list');
			$route->get('countries', [$c, 'countries'])->name('countries');
			$route->get('{id}', [$c, 'single'])->name('single');

			// POST
			$route->post('seen', [$c, 'seenAll'])->name('seen-all');
			$route->post('{id}/seen', [$c, 'seen'])->name('seen');

			// DELETE
			$route->delete('{id}', [$c, 'remove'])->name('remove');
		});

		// MEDIA ROUTES
		$route->prefix('media')->name('media.')->group(function ($route) {
			$c = MediaController::class;

			// GET
			$route->get('{id}', [$c, 'getMediaUrl'])->name('url');
		});

		// USER DEVICES ROUTES
		$route->prefix('devices')->name('devices.')->group(function ($route) {
			$c = PushDeviceController::class;

			// GET
			$route->get('', [$c, 'index'])->name('list');

			// PUT
			$route->put('', [$c, 'store'])->name('store');

			// DELETE
			$route->delete('{device_id?}', [$c, 'remove'])->name('remove');
		});

		// CHART ROUTES
		$route->prefix('charts')->name('charts.')->group(function ($route) {
			$c = ChartController::class;

			// POST
			$route->post('usage-stats', [$c, 'usageStats'])->name('usage-stats');
			$route->post('notifications', [$c, 'notifications'])->name('notifications');
			$route->post('devices', [$c, 'devices'])->name('devices');
		});
	});
});

@andreaselia this appears to be happening if the parameter is not type hinted.

Okay, now able to reproduce - I'll push a fix shortly.

@rcerljenko This should be fixed in v1.6.3.