Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions configuration-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,10 @@
"type": "boolean",
"description": "Disable the generation of `GetBody()` and `Get<TypeName>()` getter methods on response objects for `ClientWithResponses`, which are otherwise generated by default."
},
"default-response-error": {
"type": "boolean",
"description": "Whether the generated response parsing function adds a `default:` case that returns an error for a response whose status code and content type are not matched by any declared case. When false (the default) such a response is returned with no error."
},
"prefer-skip-optional-pointer": {
"type": "boolean",
"description": "Allows defining at a global level whether to omit the pointer for a type to indicate that the field/type is optional. This is the same as adding `x-go-type-skip-optional-pointer` to each field (manually, or using an OpenAPI Overlay). A field can set `x-go-type-skip-optional-pointer: false` to still require the optional pointer.",
Expand Down
5 changes: 5 additions & 0 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,11 @@ output-options:
client-response-bytes-function: false
skip-client-response-content-type: false
skip-response-body-getters: false
# When true, the generated response parsing function adds a `default:` case
# that returns an error for a response whose status code and content type are
# not matched by any declared case. Off by default, which preserves the
# existing behaviour of returning such a response with no error.
default-response-error: false
streaming-content-types: []
# Short names for media types, used in generated type names. Keys are the
# tag spliced into type names (e.g. the JSON in FindPetsJSONRequestBody),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# yaml-language-server: $schema=../../../../../configuration-schema.json
package: optionsdefaultresponseerrordefault
output: default_response_error.gen.go
# default-response-error is left UNSET, so the default behaviour applies: an
# unmatched response is returned with no error.
generate:
client: true
models: true

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package optionsdefaultresponseerrordefault

import (
"io"
"net/http"
"strings"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func newResponse(statusCode int, body string) *http.Response {
return &http.Response{
StatusCode: statusCode,
Header: http.Header{"Content-Type": []string{"application/json"}},
Body: io.NopCloser(strings.NewReader(body)),
}
}

// outputoptions/default-response-error/default: with the option unset, an
// unmatched response is returned with no error, which is the pre-existing
// behaviour the option is opt-in to.
func TestUnmatchedResponseIsNotAnErrorByDefault(t *testing.T) {
resp, err := ParseGetThingResponse(newResponse(http.StatusInternalServerError, `{"message":"boom"}`))

require.NoError(t, err)
require.NotNil(t, resp)
assert.Nil(t, resp.JSON200)
assert.Nil(t, resp.JSON404)
}
9 changes: 9 additions & 0 deletions internal/test/options/default_response_error/default/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Package optionsdefaultresponseerrordefault checks that, with
// default-response-error left at its default, the generated Parse<Op>Response
// function returns an unmatched response with no error, preserving the existing
// behaviour.
//
// outputoptions/default-response-error/default
package optionsdefaultresponseerrordefault

//go:generate go run github.com/oapi-codegen/oapi-codegen/v2/cmd/oapi-codegen --config=config.yaml ../spec.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# yaml-language-server: $schema=../../../../../configuration-schema.json
package: optionsdefaultresponseerrorenabled
output: default_response_error.gen.go
generate:
client: true
models: true
output-options:
# outputoptions/default-response-error/enabled: an unmatched response is an error.
default-response-error: true
Loading
Loading