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

empty response if using streamJson #956

Closed
1 task done
vrgblzs opened this issue Feb 24, 2025 · 2 comments
Closed
1 task done

empty response if using streamJson #956

vrgblzs opened this issue Feb 24, 2025 · 2 comments
Labels
bug Something isn't working

Comments

@vrgblzs
Copy link

vrgblzs commented Feb 24, 2025

Scribe version

4.39.0

PHP version

8.3

Laravel version

11.41.3

Scribe config

strategies.metadata => removed Knuckles\Scribe\Extracting\Strategies\Metadata\GetFromMetadataAttributes
strategies.urlParameters => removed Knuckles\Scribe\Extracting\Strategies\UrlParameters\GetFromUrlParamAttribute
strategies.queryParameters => removed Knuckles\Scribe\Extracting\Strategies\QueryParameters\GetFromFormRequest, Knuckles\Scribe\Extracting\Strategies\QueryParameters\GetFromInlineValidator, Knuckles\Scribe\Extracting
\Strategies\QueryParameters\GetFromQueryParamAttribute
strategies.headers => added Knuckles\Scribe\Extracting\Strategies\Headers\GetFromRouteRules: removed Knuckles\Scribe\Extracting\Strategies\Headers\GetFromHeaderAttribute, [
    'override',
    [
        'Content-Type' => 'application/json',
        'Accept' => 'application/json',
    ],
]
strategies.bodyParameters => removed Knuckles\Scribe\Extracting\Strategies\BodyParameters\GetFromInlineValidator, Knuckles\Scribe\Extracting\Strategies\BodyParameters\GetFromBodyParamAttribute
strategies.responses => added Knuckles\Scribe\Extracting\Strategies\Responses\ResponseCalls: removed Knuckles\Scribe\Extracting\Strategies\Responses\UseResponseAttributes, [
    'Knuckles\\Scribe\\Extracting\\Strategies\\Responses\\ResponseCalls',
    [
        'only' => [
            'GET *',
        ],
        'config' => [
            'app.debug' => false,
        ],
    ],
]
strategies.responseFields => removed Knuckles\Scribe\Extracting\Strategies\ResponseFields\GetFromResponseFieldAttribute

What happened?

I rewrote some endpoints on my laravel app to use the streamJson function to generate the API response (to preserve some memory on large responses). After the changes the scribe generate command on these GET endpoints results with status: 200, but empty content: ''
With curl/postman the endpoint works as before (same content with the same content-type header, etc.)

Eg. code changes:

old:
return $query->get();

new:

return response()->streamJson($this->yieldResults(query));

function yieldResults($query): Generator {
  foreach ($query->lazy() as $item) yield $item;
}

I dig into the scribe source and found that in Extracting\Strategies\Responses\ResponseCalls.php the response of the $kernel->handle($request) call with the old code is a Illuminate\Http\JsonResponse with the content as expected. However with my new code the response is Symfony\Component\HttpFoundation\StreamedJsonResponse with #content: ? string and #streamed: false. The details of the response object is attached in the file.

response.txt

What can be the cause? Did I miss some config settings?

Docs

@vrgblzs vrgblzs added bug Something isn't working triage labels Feb 24, 2025
@shalvah
Copy link
Contributor

shalvah commented Feb 24, 2025

I don't think there's any "cause" from your end. A streamed response needs to be handled differently from a regular response. A regular response returns the entire contents at once, but a streamed response does not immediately render its content. The response calls strategy probably needs to check if the response was streamed and force all the content to be rendered. PRs welcome.

@shalvah shalvah removed the triage label Feb 24, 2025
@shalvah
Copy link
Contributor

shalvah commented Feb 25, 2025

Support added in 5.1.0

@shalvah shalvah closed this as completed Feb 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants