Skip to content

Problems generating controllers/resources in PascalCase #171

@axit-joost

Description

@axit-joost

Seems to be a problem with case-conversion.

Given the contents of the following draft.yaml:

models:
  Certificate:
    name: string
    certificate_type_id: id
    reference: string
    document: string
    expiry_date: date
    remarks: nullable text
  CertificateType:
    name: string
    relationships:
      hasMany: Certificate

controllers:
  Certificate:
    resource: api
  CertificateType:
    resource: api

Running blueprint:build will throw the following error:

TypeError: Argument 1 passed to Blueprint\Generators\Statements\ResourceGenerator::visibleColumns() must be an instance of Blueprint\Models\Model, null given, called in /Users/joostjacobs/Code/blueprint/vendor/laravel-shift/blueprint/src/Generators/Statements/ResourceGenerator.php on line 98

A .blueprint file is not created due to this crash.

It ultimately crashed upon trying to create a Resource in app/Http/Resources/CertificateType.php.

Examining the partially generated code, the following issues surface:

  1. In the generated app/Http/Controllers/CertificateTypeController.php, we see two imports, one with the correct PascalCase, one with only the first letter capitalized:
<?php

namespace App\Http\Controllers;

use App\CertificateType;
use App\Certificatetype;
  1. The generated DocBlocks as well as the method body, refer to an incorrectly cased Collection and Resource: CertificatetypeCollection should be CertificateTypeCollection and the Resource respectively should not be CertificatetypeResource but CertificateTypeResource.
    /**
     * @param \Illuminate\Http\Request $request
     * @return \App\Http\Resources\CertificatetypeCollection
     */
    public function index(Request $request)
    {
        $certificatetypes = Certificatetype::all();

        return new CertificatetypeCollection($certificatetypes);
    }

    /**
     * @param \App\Http\Requests\CertificateTypeStoreRequest $request
     * @return \App\Http\Resources\Certificatetype
     */
    public function store(CertificateTypeStoreRequest $request)
    {
        $certificatetype = Certificatetype::create($request->all());

        return new CertificatetypeResource($certificatetype);
    }
  1. While the method signatures for show, update, and destroy show a correct model binding of CertificateType $certificateType, with the variable properly camelCased, the method body uses an all lowercase variable name:
    /**
     * @param \Illuminate\Http\Request $request
     * @param \App\CertificateType $certificateType
     * @return \App\Http\Resources\Certificatetype
     */
    public function show(Request $request, CertificateType $certificateType)
    {
        return new CertificatetypeResource($certificatetype);
    }
  1. All the generated Request classes for validation are incorrect. They do not feature any of the model's columns, and generates a plain wrong rule instead. I think this is unrelated to the PascalCase problem, but it should be raised as an entirely new issue all together:
    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'certificatetype' => 'required',
        ];
    }

I'll raise a separate issue for this.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions