knuckleswtf / scribe

Generate API documentation for humans from your Laravel codebase.✍
https://scribe.knuckles.wtf/laravel/
MIT License
1.72k stars 307 forks source link

Headers and env vars not set #80

Closed andyts93 closed 3 years ago

andyts93 commented 4 years ago

What happened? None of the headers in configuration file are added in the responseCalls. I have the Accept and Content-Type headers in configuration, they are shown correctly in the generated documentation but they are not used during the response call. I also have to set authentication but the bearer token isn't in the headers too (I have the @authenticated tag in the controller function). I also noticed the the app.env config isn't set

Screenshots and stack traces: Schermata 2020-08-27 alle 10 44 04

Schermata 2020-08-27 alle 10 52 37

My environment:

My Scribe config (minus the comments):

[
    "type" => "laravel",
    "static" => [
        "output_path" => "public/docs",
    ],
    "laravel" => [
        "add_routes" => false,
        "docs_url" => "/docs",
        "middleware" => [],
    ],
    "auth" => [
        "enabled" => true,
        "in" => "bearer",
        "name" => "token",
        "use_value" => env("SCRIBE_AUTH_KEY"),
        "extra_info" => "",
    ],
    "router" => "laravel",
    "routes" => [
        [
            "match" => [
                "domains" => ["*"],
                "prefixes" => ["api/*"],
                "versions" => ["v1"],
            ],

            "include" => [
            ],
            "exclude" => [
            ],
            "apply" => [
                "headers" => [
                    "Content-Type" => "application/json",
                    "Accept" => "application/json"
                ],
                "response_calls" => [
                    "methods" => ["GET"],
                    "config" => [
                        "app.env" => "documentation",
                    ],
                    "cookies" => [
                    ],
                    "queryParams" => [
                    ],
                    "bodyParams" => [
                    ],
                    "fileParams" => [
                    ],
                ],
            ],
        ],
    ],
    "fractal" => [
        "serializer" => null,
    ],
    "faker_seed" => null,
    "strategies" => [
        "metadata" => [
            \Knuckles\Scribe\Extracting\Strategies\Metadata\GetFromDocBlocks::class,
        ],
        "urlParameters" => [
            \Knuckles\Scribe\Extracting\Strategies\UrlParameters\GetFromUrlParamTag::class,
        ],
        "queryParameters" => [
            \Knuckles\Scribe\Extracting\Strategies\QueryParameters\GetFromQueryParamTag::class,
        ],
        "headers" => [
            \Knuckles\Scribe\Extracting\Strategies\Headers\GetFromRouteRules::class,
            \Knuckles\Scribe\Extracting\Strategies\Headers\GetFromHeaderTag::class,
        ],
        "bodyParameters" => [
            \Knuckles\Scribe\Extracting\Strategies\BodyParameters\GetFromFormRequest::class,
            \Knuckles\Scribe\Extracting\Strategies\BodyParameters\GetFromBodyParamTag::class,
        ],
        "responses" => [
            \Knuckles\Scribe\Extracting\Strategies\Responses\UseTransformerTags::class,
            \Knuckles\Scribe\Extracting\Strategies\Responses\UseResponseTag::class,
            \Knuckles\Scribe\Extracting\Strategies\Responses\UseResponseFileTag::class,
            \Knuckles\Scribe\Extracting\Strategies\Responses\UseApiResourceTags::class,
            \Knuckles\Scribe\Extracting\Strategies\Responses\ResponseCalls::class,
        ],
        "responseFields" => [
            \Knuckles\Scribe\Extracting\Strategies\ResponseFields\GetFromResponseFieldTag::class,
        ],
    ],
    "routeMatcher" => \Knuckles\Scribe\Matching\RouteMatcher::class,
    "continue_without_database_transactions" => [],
]

Additional info:

shalvah commented 4 years ago

Where are you dumping the headers from?

Also, Scribe doesn't set environment variables. It sets Laravel config variables. So if you specify a value for app.env, the proper way to check if that was set would be to dump config('app.env'). If what you want is to set environment variables directly, you can create a .env.docs file and run the generate command with --env docs.

andyts93 commented 4 years ago

I'm dumping from the middleware that checks if the Accept header is application/json

Thanks for the explanation about the config variables!

shalvah commented 4 years ago

I'd suggest you do some more debugging. Take a look at the \Knuckles\Scribe\Extracting\Strategies\Responses\ResponseCalls class and dump the value of $context['headers'] before prepareRequest() is called. And then dump$request->getRequestFormat() after addHeaders() is called.

shalvah commented 4 years ago

Any luck @andyts93 ? Might be able to get on a call sometime.

andyts93 commented 4 years ago

Any luck @andyts93 ? Might be able to get on a call sometime.

Hi shalvah, I'm sorry but I'm not working on this project now and I'm pretty busy at work so I didn't have the time to do some debug. I'll have to work on this in the next month I guess so I will be able to tell you something more.

Thanks for your willingness!

mixdd commented 3 years ago

I've got the same issue. Disclaimer: I'm not a PHP developer and this the first time working with Lumen, so I might be missing the mark horribly. I have managed to narrow it down to the following:

Anyways, (seeing as the abovementioned bug doesn't have a clear solution) I managed to fix this by inserting the following line above line 328 in ResponseCalls.php: $kernel->alias('Illuminate\Http\Request', 'request');

I know this is kind of hackish, but (with my limited knowledge of PHP) the above line seems to allow the make method to find a concrete implementation of the request class, so it doesn't create a new one. I'm also assuming the above line is some kind of abomination which will break a thousand other things :)

Hopefully the above explanation puts someone with a deeper understanding of the root issue on the path to a proper fix.

shalvah commented 3 years ago

PHP developer or not, that's a pretty smart investigation. I'll take a closer look when I have some time.

shalvah commented 3 years ago

I think I've fixed this (2.3.0). Your investigation was pretty similar to another issue: the request wasn't bound in Lumen. Test and let me know if it works now.: