fix(discoveryengine): change streamAnswer return type to array#3939
Open
AmariahAK wants to merge 1 commit into
Open
fix(discoveryengine): change streamAnswer return type to array#3939AmariahAK wants to merge 1 commit into
AmariahAK wants to merge 1 commit into
Conversation
streamAnswer REST endpoint returns a JSON array of AnswerQueryResponse frames at runtime, but the generated TypeScript types declared a single object, causing res.data.answerQueryToken and similar fields to be undefined at runtime. - Fix v1, v1beta, and v1alpha generated types (99 total) - Add :stream path detection in method-partial.njk generator template to prevent regression Fixes googleapis#3930 Co-authored-by: atlarix-agent <agent@atlarix.dev>
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
The
streamAnswerREST endpoint returns a JSON array ofAnswerQueryResponseframes at runtime, but the generated TypeScript typesdeclared a single
GoogleCloudDiscoveryengineV1AnswerQueryResponseobject.This caused
res.data.answerQueryToken,res.data.answer?.relatedQuestions,and similar fields to be
undefinedat runtime while TypeScript reportedthem as present — because
res.datais actually an array, not an object.Root cause
The discovery document's
response.$refpoints to a singleAnswerQueryResponse, but the actual REST HTTP response body is a JSONarray. The generator faithfully produces the single-object type from the
discovery doc, which doesn't match reality. The discovery doc description
even says "returns multiple AnswerQueryResponse messages in a stream" —
the
$refjust wasn't declared as an array.Changes
src/apis/discoveryengine/v1.ts— Changed allstreamAnswermethod signatures and
createAPIRequestcalls fromSchema$GoogleCloudDiscoveryengineV1AnswerQueryResponsetoSchema$GoogleCloudDiscoveryengineV1AnswerQueryResponse[](33 occurrences, 3 resource paths)
src/apis/discoveryengine/v1beta.ts— Same changes withV1betaprefix (33 occurrences, 3 resource paths)
src/apis/discoveryengine/v1alpha.ts— Same changes withV1alphaprefix (33 occurrences, 3 resource paths)
src/generator/templates/method-partial.njk— Added detectionfor
:streamin the method path. When the path contains:stream(Google's REST convention for streaming custom methods like
:streamAnswer,:streamGenerate, etc.), the generated response typeis automatically array-ified to prevent this bug from recurring on
the next nightly regeneration.
Non-streaming
answermethods (which correctly return a single object)are not affected — only
streamAnswermethods were changed.Impact
@googleapis/discoveryenginecan now accessres.data[0].answerQueryToken,res.data.filter(...), etc. withoutundefinedsurprises or@ts-expect-errorcasts.an array; this fix only corrects the TypeScript types to match.
:stream*methods across all APIs.Testing
[]per version file (v1/v1beta/v1alpha),99 total across all
streamAnswermethod signatures andcreateAPIRequestcallsanswer(non-stream) methodcreateAPIRequestcallsremain correctly typed as single objects — no accidental changes
npm run compilepasses (9 pre-existing TypeScript errors inunrelated
driveAPI test files exist before and after this change)Additional Information
The discovery document itself should also be updated upstream so the
response.$refcorrectly declares an array. The generator templatechange in this PR acts as a safety net until that happens.
Fixes #3930 🦕
Then fill the checklist as: