fix(generator): error body with no type name emits code that is not Go - #63
Open
giraffesyo wants to merge 1 commit into
Open
fix(generator): error body with no type name emits code that is not Go#63giraffesyo wants to merge 1 commit into
giraffesyo wants to merge 1 commit into
Conversation
An error response whose schema resolves to a type expression rather than a
type name pasted that expression into an identifier position:
type map[string]anyResponse struct {
func parsemap[string]anyResponse(err error) error {
`default: { schema: { type: object } }` and an inline `type: array` both hit
it, and the generated package did not compile at all.
The wrapper now carries its own name in the IR. A named body still yields
<Type>Response, so upgrading renames nothing callers match on. A map, a
slice, or a builtin takes the name of the operation and status code that
produced it, which also fixes a text/plain body landing in an unexported
`stringResponse` that errors.As could not name.
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.
Closes #61.
An error response whose schema resolves to a Go type expression rather than a type name had that expression pasted into an identifier position:
The generated package did not compile.
default: { schema: { type: object } }and an inlinetype: arrayboth trigger it, and the first is a common way to say "some JSON error body".Fix
ir.ResponseDefnow carriesErrorWrapper, the name of the type the templates declare, instead of the templates deriving one from the body's type expression:<Type>Response, byte for byte what is generated today, so upgrading does not rename types callers match on witherrors.As.GetTDefaultResponseError,GetTResponse404Error. That mirrors what an inline object body of the same response is already named.The second case also fixes a
text/plainerror body, which resolved tostringand produced an unexportedstringResponse. A caller could reach the parsed body only by name, and the name was not exported.Tests
internal/generator/e2e_error_wrapper_test.gogenerates a spec with a map body, a slice body, a builtin body, and a named body, then compiles and runs it: each wrapper parses its body intoDetail, matches undererrors.As, still unwraps to*APIError, and the named body keepsValidationErrorsResponse.gofmt,go vet ./..., andgo test ./...pass.