scramble icon indicating copy to clipboard operation
scramble copied to clipboard

Issue: Failure to Parse Relationship While Parsing a Child Model

Open Eyadhamza opened this issue 2 years ago • 2 comments

Issue: Failure to Parse Relationship While Parsing a Child Model

Description

When attempting to open the documentation, I encountered a "failure to parse" error. Upon further investigation, I discovered that the issue stems from the package's inability to identify a specific relationship within a model. The model in question extends a parent model, which contains the relevant relationship. As a result, the package is unable to recognize the relationship due to its presence in the parent model.

To provide additional context, I have a model named Role which extends the SpatieRole model. The error message triggering this issue is as follows:

Dedoc\Scramble\Support\Type\UnknownType {#4859 ▼
    -attributes: []
    -comment: "Cannot get a property type [permissions] on type [App\Models\Role]"
}

However, I was able to resolve the problem by introducing the relationship directly within the child model. After adding the permissions relationship to the child model, the issue no longer persisted.

Steps to Reproduce

  1. Attempt to open the documentation.
  2. Observe the "failure to parse" error triggered by the package.
  3. Investigate the error and identify that the root cause is the package's inability to recognize a relationship in a model that extends a parent model.
  4. Verify that adding the same relationship directly to the child model resolves the issue.

Expected Behavior

The package's documentation should be able to accurately parse and identify relationships within models, even when they extend parent models. Users should not encounter "failure to parse" errors due to this relationship recognition limitation.

Actual Behavior

The package is currently unable to identify a relationship in a child model that extends a parent model, resulting in a "failure to parse" error when attempting to access the documentation.

Additional Information

  • Model: Role (child model)
    // Role.php
    use Spatie\Permission\Models\Role as SpatieRole;
    
    class Role extends SpatieRole
    {
        // Permissions relationship missing in this child model
    }
    
  • Model: SpatieRole (parent model)
    // SpatieRole.php
    use Spatie\Permission\Models\Role as BaseRole;
    
    class SpatieRole extends BaseRole
    {
       public function permissions(): BelongsToMany
    {
        return $this->belongsToMany(
            config('permission.models.permission'),
            config('permission.table_names.role_has_permissions'),
            PermissionRegistrar::$pivotRole,
            PermissionRegistrar::$pivotPermission
        );
    }
    }
    
  • Any other relevant details or context that might help to resolve this issue.

Eyadhamza avatar Aug 13 '23 11:08 Eyadhamza

Hey @Eyadhamza

Thanks for the reporting. It is very detailed and I really appreciate it.

I sort of understood the issue, but I'm wondering where exactly you see the error? In UI? If so, do you mind sharing a screenshot?

If in UI, does '/docs/api.json' endpoint work? If not, what is the exception?

Thanks!

romalytvynenko avatar Aug 13 '23 12:08 romalytvynenko

I really appreciate your timely response, and this great and useful package : )

Here are the errors:

Error in /docs/api image

Error in /docs/api.json Dump: \vendor\dedoc\scramble\src\Generator.php: 61"Error when analyzing route 'GET api/activity-logs' (App\Http\Controllers\ActivityLogs\ActivityLogController@index): Undefined array key 0

image

When I dd $newType in line 101 in JsonResourceTypeToSchema image

Eyadhamza avatar Aug 14 '23 09:08 Eyadhamza

@Eyadhamza does the issue persists on v0.10.5?

romalytvynenko avatar May 19 '24 08:05 romalytvynenko

@Eyadhamza I can reproduce an UnknownType with a comment Cannot get property [permissions] type on [App\Models\Role], but this should not result in an error preventing documentation from being generated. This is only resulting in a not accurate documentation (permissions documented as string).

This is definitely something to be fixed, but in the same time this doesn't result in a documentation generation failure.

I would appreciate repro repo so I can fix that. Thanks!

romalytvynenko avatar May 19 '24 08:05 romalytvynenko

@Eyadhamza fixed that UnknownType with a comment Cannot get property [permissions] type on [App\Models\Role] in v0.10.6.

Feel free to create a new issue if generation fails.

romalytvynenko avatar May 19 '24 09:05 romalytvynenko