Skip to content

feat(generator): emit typed JSON parsing and handle void/non-JSON responses#66

Open
cstrnt wants to merge 2 commits into
grafana:mainfrom
cstrnt:feat/use_proper_generated_types
Open

feat(generator): emit typed JSON parsing and handle void/non-JSON responses#66
cstrnt wants to merge 2 commits into
grafana:mainfrom
cstrnt:feat/use_proper_generated_types

Conversation

@cstrnt
Copy link
Copy Markdown
Contributor

@cstrnt cstrnt commented Oct 29, 2025

Only JSON-parse when the response content-type is application/json and emit an explicit typed const data = response.json() as .... Fall back to response.body for non-JSON responses and omit the data field for void responses. Update generated examples, fixtures and generator tests (add empty_response sample) to match the new output.

fixes #55

…ponses

Only JSON-parse when the response content-type is application/json and emit
an explicit typed `const data = response.json() as ...`. Fall back to
`response.body` for non-JSON responses and omit the `data` field for void
responses. Update generated examples, fixtures and generator tests (add
empty_response sample) to match the new output.
@cstrnt cstrnt requested review from a team, Blinkuu and facundobatista October 29, 2025 13:56
@terev
Copy link
Copy Markdown
Contributor

terev commented Jan 4, 2026

@cstrnt Thanks so much for this, it looks like a much more correct way to handle decoding response data. Its too bad this hasn't been given a look yet. I wonder if it'd help to try requesting another review or tagging the pending reviewers?

@cstrnt
Copy link
Copy Markdown
Contributor Author

cstrnt commented Jan 5, 2026

@terev I mean sure, that would be cool @facundobatista @Blinkuu @DefCon-007

@terev
Copy link
Copy Markdown
Contributor

terev commented Jan 20, 2026

I noticed that this pr does actually change the runtime behaviour of the generated client slightly. The behaviour change exists when using a client generated from this branch to perform a request, from which a response is returned that contains an unhandled status code. In the case that the response contains valid json, there is no difference in the runtime execution. However, if invalid json is returned the operation method throws a parsing error. Whereas, with a client generated with 0.4.1, the parsing error is swallowed and the body value is returned as is.

The parse error seems like desirable behaviour when the status code is included in the handled set of status codes where the body is expected to be json encoded.

@Blinkuu Blinkuu removed their request for review February 27, 2026 15:18
@facundobatista facundobatista requested review from 2Steaks and removed request for facundobatista April 17, 2026 12:52
Copy link
Copy Markdown

@2Steaks 2Steaks left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this PR!

We have requested changes to avoid changing the response signature too much.

Comment on lines -49 to -55
let data
const data = response.json() as GetExample200

try {
data = response.json()
} catch {
data = response.body
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we look at keeping the try/catch fallback to the response.body to avoid breaking existing scripts.

let data: GetExample200 | string

try {
  data = response.json() as GetExample200
} catch {
  data = response.body as string
}
return {
  response,
  data,
  operationId: 'GetExample',
}

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

Successfully merging this pull request may close these issues.

Incorrect Typescript Types for generated Output

3 participants