scramble icon indicating copy to clipboard operation
scramble copied to clipboard

Attempt to read property "methods" on null

Open dmason30 opened this issue 1 year ago • 4 comments

Laravel 10.x | Scramble 0.8.5 | PHP 8.3.x

Error occurs when requesting docs/api.json and breaks the loading of the whole docs site:

image

The getClassDefinition method has a return type of ?ClassDefinition yet the MethodAnalyzer::analyze method does not accommodate for this.

I did some debugging and it seems to be unable to find the ExchangeRateResourceCollection ClassDefinition which is a standard Laravel class that extends Illuminate\Http\Resources\Json\ResourceCollection:

/** @tags Rates */
class ExchangeRatesController extends Controller
{
    /**
     * Search Rates
     *
     * @param ?ExchangeRateBook $book Find the current rates for this rate book.
     */
    public function __invoke(ExchangeRatesRequest $request, ExchangeRateBook $book = null)
    {
        $book ??= app(SessionRateBook::class);

        /** @var ExchangeRateCollection $rates */
        $rates = ExchangeRate::book($book->id)
            ->base($book->base_currency_id)
            ->with(['baseCurrency', 'targetCurrency'])
            ->get()
            ->sortByTargetCurrencyRanking();

        abort_if($rates->isEmpty(), 404);

        return new ExchangeRateResourceCollection($rates);
    }
}

dmason30 avatar Feb 08 '24 17:02 dmason30

Hey @dmason30

Can you share the implementation of ExchangeRateResourceCollection?

romalytvynenko avatar Mar 03 '24 18:03 romalytvynenko

@romalytvynenko Here it is, it is just used to add some meta data to the collection. i should say this was working fine in a previous version of this package and rendering the docs without issue. This route has not been changed in a couple years.

namespace App\Http\Resources;

use App\Facades\RateConfig;
use Illuminate\Http\Resources\Json\ResourceCollection;

class ExchangeRateResourceCollection extends ResourceCollection
{
    public function with($request)
    {
        $config = RateConfig::driver();

        return [
            'meta' => [
                'operators' => [
                    'calculation' => [
                        'charge' => $config->chargeOperator(),
                        'quantity' => $config->quantityOperator(),
                    ],
                    'comparison' => [
                        'gt' => $config->greaterThanOperator(),
                        'gte' => $config->greaterThanOrEqualToOperator(),
                        'lt' => $config->lessThanOperator(),
                        'lte' => $config->lessThanOrEqualToOperator(),
                        'percentage' => $config->percentage(),
                    ],
                ],
                'templates' => $config->translator()->templates()->toArray(),
            ],
        ];
    }
}

dmason30 avatar Mar 03 '24 20:03 dmason30

@dmason30 cannot reproduce. Any chance you can create a simple Laravel project repo with reproduction of this issue?

romalytvynenko avatar Mar 04 '24 06:03 romalytvynenko

I have the same problem mentioned here: https://github.com/dedoc/scramble/issues/352

rcerljenko avatar Mar 28 '24 07:03 rcerljenko

@dmason30 can you please check if the issue persists on v0.10.5?

romalytvynenko avatar May 19 '24 08:05 romalytvynenko

I tried it and it's still happening... only under octane :(

rcerljenko avatar May 19 '24 08:05 rcerljenko

@rcerljenko yeah, on Octane it still persists. The question here is if it's still happening for @dmason30 as I'm not sure Dan's case is about Octane.

romalytvynenko avatar May 19 '24 08:05 romalytvynenko

Unfortunately I don't have access to that project anymore (I have changed jobs). There is a chance this was running on octane locally when I encountered the error as we definitely ran it on octane on prod.

dmason30 avatar May 19 '24 12:05 dmason30

@romalytvynenko I can be your production tester if you want. I currently have a project that is active in dev and deployed to Linux Octane prod server

rcerljenko avatar May 20 '24 05:05 rcerljenko

@rcerljenko Nice, thanks! If you can create a repro repo that I can simply install and get this issue – this also would be great 😊

romalytvynenko avatar May 20 '24 05:05 romalytvynenko

@dmason30 should be fixed in v0.10.7

romalytvynenko avatar May 20 '24 12:05 romalytvynenko