dedoc/scramble

Type to schema extension: Incorrect object inference

malandles opened this issue · 1 comments

Trying to implement the following package https://laraveljsonapi.io. Was able to implement the OpenAPI operation extension somewhat easily.

Although, when trying to implement the type to schema one, the type is not correctly inferred and the attributes are empty.

Composer require:

{
    "dedoc/scramble": "^0.10.13" 
    "laravel/framework": "^11.0",
    "laravel-json-api/laravel": "^4.0"
}

Routes api.php:

use LaravelJsonApi\Laravel\Facades\JsonApiRoute;
use LaravelJsonApi\Laravel\Routing\ResourceRegistrar;
use LaravelJsonApi\Laravel\Http\Controllers\JsonApiController;

JsonApiRoute::server('cms-v1')
    ->prefix('cms/v1')
    ->resources(function (ResourceRegistrar $server) {
        $server->resource('users', JsonApiController::class)->readOnly();
    });
  • server here is not the same as an OpenAPI server. The server in this package is a way to separate different APIs on the same server;
  • resources and resource create JSON:API resources, which ressembles the Laravel resource system but with a stricter structure;
  • readOnly will use the index and show of the JsonApiController which are generated with traits:
    • index: LaravelJsonApi\Laravel\Http\Controllers\Actions\FetchMany
    • show: LaravelJsonApi\Laravel\Http\Controllers\Actions\FetchOne

The JsonApiController will either return a Responsable, a Response or a null response:

  • Illuminate\Contracts\Support\Responsable
  • Illuminate\Http\Response

The PHPDoc in the package use the shorthand version in their @return, might be the source of the confusion?

Related files:

When I try to verify the type of the Dedoc\Scramble\Support\Type\Type, I get only the shorthand version instead of the FQN of those classes.

<?php

namespace App\Scramble;

use Dedoc\Scramble\Extensions\TypeToSchemaExtension;
use Dedoc\Scramble\Support\Type\Type;
use Dedoc\Scramble\Support\Type\ObjectType;

class JsonApiTypeToSchemaExtension extends TypeToSchemaExtension
{
    public function shouldHandle(Type $type)
    {
       dump($type->toString());
    }

    /**
     * @param  ObjectType  $type
     */
    public function toSchema(Type $type)
    {
        //
    }
}

Results in:

Screenshot 2024-06-25 at 8 10 04 AM

If I use $type->instanceOf the proper classes, I get false everytime.

Let me know if I can be of any help in fixing this.

Closing the issue as the amount of work to implement third-party package support is really huge. Feel free to dump types around in Scramble's code to see which types are getting inferred.