laravel-openapi icon indicating copy to clipboard operation
laravel-openapi copied to clipboard

Use Primary Key type as path parameter

Open Geist5000 opened this issue 2 years ago • 1 comments

Current Behavior

If a Controller function accesses the path variable by model binding (as described here https://laravel.com/docs/9.x/routing#implicit-binding) and doesn't manually specify a path type, it is typed as string.

Requested Behavior

I would suggest to automatically try to type the path variable like the primary key type of the model

Possible Solution

Change the guessFromReflectionType of the SchemaHelpers class to something like this:

public static function guessFromReflectionType(ReflectionType $reflectionType): Schema
    {
        $typeString = $reflectionType->getName();
        if (is_subclass_of($reflectionType->getName(), 'Illuminate\Database\Eloquent\Model')) {
            $modelInstance = new ($reflectionType->getName())();
            $typeString = $modelInstance->getKeyType();
        }
        switch ($typeString) {
            case 'int':
                return Schema::integer();
            case 'bool':
                return Schema::boolean();
        }

        return Schema::string();
    }

Geist5000 avatar Jul 27 '22 12:07 Geist5000

This functionality is also requested here #74

Geist5000 avatar Jul 27 '22 12:07 Geist5000