Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Describe response using API Resource #32

Open
pleaz opened this issue Feb 5, 2020 · 5 comments
Open

Describe response using API Resource #32

pleaz opened this issue Feb 5, 2020 · 5 comments

Comments

@pleaz
Copy link
Contributor

pleaz commented Feb 5, 2020

When API Resource exists to use it to generate a response.

@mtrajano
Copy link
Owner

mtrajano commented Feb 6, 2020

That is a great idea, I had this in my TODO list. I'll hopefully get to this soon

@sanasol
Copy link

sanasol commented Apr 13, 2020

@mtrajano Proof Of Concept with shitty code :D

sanasol#1

Problems

  1. How to get any model with filled data
  2. Have no idea how to fill any resource with full data
  3. Have no idea how to correctly extract that structure from resource
  4. Need deep definition extraction/creation to avoid duplication and creating subresource definitions.
  5. Controller methods can return collection/single resource or even some custom response that contain resource deeply.

@mtrajano
Copy link
Owner

@sanasol thank you so much for submitting a pr for this. I need to think about this one a little more. I'm usually hesitant to include another annotation unless necessary (less bootstrapping needed to get the correct output), do you think it would be possible to rely on the return typehint of a specific Api Resource and leave the @model as is which would instruct which model should be passed into the resource? Also would appreciate some tests at least proving that the implementation works. I'm currently working on cleaning up this branch and trying to add as much test coverage as possible in order to do a more stable release. Thanks!

@sanasol
Copy link

sanasol commented Apr 14, 2020

This is very PoC, so it wont work anywhere except my environment.
I tried replace @model with resource until found that need both for make it work somehow.
So with clean implementation it should some kind of @resource + @model.
But now I found out that attaching to resource also is bit useless, since response can be actually with any structure incuding some simple array.

So my current thoughts that need some custom response classes with full response structure written in annotation. i.e. empty classes with response structure and data types.

i.e. it will be just php-written swagger structure with autogeneration or something like.
I didnt see any other possible solutions that will generate swagger with reallife responses automatically.

So in my mind now:

Controller method annotation

/**
* @response App\Responses\ResumeResponse
*/

Response class that linked to some "sub responses"

namespace App\Responses;

class ResumeResponse
{
    public static function generate()
    {
        return [
            'id' => 'number',
            'name' => 'string',
            'experience' => [App\Responses\ExperienceResponse::class], // collection
            'common_skill' => App\Responses\CommonSkillResponse::class, // single object
        ];
    }
}

so ExperienceResponse and CommonSkillResponse have same generate() method inside with some structure.

Actually this is very close to writing full docs manually, but with php syntax.

@mr-feek
Copy link
Collaborator

mr-feek commented Jul 12, 2021

I started prototyping a solution for this involving traversing the AST -- mr-feek@8f3bbad

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants