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

Trying to get property 'objectId' of non-object when referencing Parameters

Open randelsr-vault opened this issue 4 years ago • 1 comments

Compiling header parameters into a DeviceController parameter file as it seems only a single parameter annotation is allowed on the route:

class DeviceParameters extends ParametersFactory
{
    /**
     * @return Parameter[]
     */
    public function build(): array
    {
        return [
            AcceptParameters::ref(),
            ApiVersionParameters::ref(),
        ];
    }
}

And here's the example ApiVersionParameters:

class ApiVersionParameters extends ParametersFactory implements Reusable
{
    /**
     * @return Parameter[]
     */
    public function build(): array
    {
        return [

            Parameter::header('ApiVersion')
                ->name('Api-Version')
                ->description('Api Version')
                ->required(true)
                ->schema(Schema::string()->example('1.0')),

        ];
    }
}

Then receive this error on compile:

 Trying to get property 'objectId' of non-object

  at vendor/vyuldashev/laravel-openapi/src/Concerns/Referencable.php:41
     37▕         } elseif ($instance instanceof SecuritySchemeFactory) {
     38▕             $baseRef = '#/components/securitySchemes/';
     39▕         }
     40▕
  ➜  41▕         return Schema::ref($baseRef.$instance->build()->objectId, $objectId);
     42▕     }
     43▕ }
     44▕

      +1 vendor frames
  2   app/OpenApi/Parameters/DeviceParameters.php:21
      Vyuldashev\LaravelOpenApi\Factories\ParametersFactory::ref()

      +4 vendor frames
  7   [internal]:0
      Vyuldashev\LaravelOpenApi\Builders\PathsBuilder::Vyuldashev\LaravelOpenApi\Builders\{closure}(Object(Illuminate\Support\Col
lection), "/device/upgrade-token")

Which makes sense, the Parameters stub returns an array of parameters, but the static ref() is expecting an object.

Perhaps I'm approaching this wrong, open to alternatives! Thanks

randelsr-vault avatar Apr 13 '21 16:04 randelsr-vault

@randelsr-chamber I tried to reference other parameter classes as well, and I was facing the same issue, the solution for me was to create an instance of other parameter classes and return them, so in your case, it would look like this:

class DeviceParameters extends ParametersFactory
{
    /**
     * @return Parameter[]
     */
    public function build(): array
    {
        return array_merge(
            (new AcceptParameters())->build(),
            (new ApiVersionParameters())->build(),
        );
    }
}

It might not be the most effective solution, but it did solve the problem for now.

AyoobMH avatar Feb 27 '22 19:02 AyoobMH