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

Support disabling default response rendering #3006

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 14 additions & 0 deletions internal/descriptor/registry.go
Expand Up @@ -132,6 +132,10 @@ type Registry struct {
// disableServiceTags disables the generation of service tags.
// This is useful if you do not want to expose the names of your backend grpc services.
disableServiceTags bool

// disableDefaultResponses disables the generation of default responses.
// Useful if you have to support custom response codes that are not 200.
disableDefaultResponses bool
}

type repeatedFieldSeparator struct {
Expand Down Expand Up @@ -758,3 +762,13 @@ func (r *Registry) SetDisableServiceTags(use bool) {
func (r *Registry) GetDisableServiceTags() bool {
return r.disableServiceTags
}

// SetDisableDefaultResponses setsdisableDefaultResponses
func (r *Registry) SetDisableDefaultResponses(use bool) {
r.disableDefaultResponses = use
}

// GetDisableDefaultResponses returns disableDefaultResponses
func (r *Registry) GetDisableDefaultResponses() bool {
return r.disableDefaultResponses
}
16 changes: 9 additions & 7 deletions protoc-gen-openapiv2/internal/genopenapi/template.go
Expand Up @@ -1369,13 +1369,15 @@ func renderServices(services []*descriptor.Service, paths openapiPathsObject, re

operationObject := &openapiOperationObject{
Parameters: parameters,
Responses: openapiResponsesObject{
"200": openapiResponseObject{
Description: desc,
Schema: responseSchema,
Headers: openapiHeadersObject{},
},
},
Responses: openapiResponsesObject{},
}

if !reg.GetDisableDefaultResponses() {
operationObject.Responses["200"] = openapiResponseObject{
Description: desc,
Schema: responseSchema,
Headers: openapiHeadersObject{},
}
}

if !reg.GetDisableServiceTags() {
Expand Down
11 changes: 11 additions & 0 deletions protoc-gen-openapiv2/internal/genopenapi/template_test.go
Expand Up @@ -3316,6 +3316,17 @@ func TestApplyTemplateProtobufAny(t *testing.T) {
},
wantNumDefinitions: 3,
},
{
// we have a protobufAny in a message but with automatic rendering of responses disabled
name: "protobufAny_referenced_in_message_with_default_responses_disabled",
args: args{
msgContainsAny: true,
regConfig: func(reg *descriptor.Registry) {
reg.SetDisableDefaultResponses(true)
},
},
wantNumDefinitions: 4,
Comment on lines +3320 to +3328
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not exactly sure what this tests but it's here 😄

},
}

for _, tt := range tests {
Expand Down
2 changes: 2 additions & 0 deletions protoc-gen-openapiv2/main.go
Expand Up @@ -41,6 +41,7 @@ var (
outputFormat = flag.String("output_format", string(genopenapi.FormatJSON), fmt.Sprintf("output content format. Allowed values are: `%s`, `%s`", genopenapi.FormatJSON, genopenapi.FormatYAML))
visibilityRestrictionSelectors = utilities.StringArrayFlag(flag.CommandLine, "visibility_restriction_selectors", "list of `google.api.VisibilityRule` visibility labels to include in the generated output when a visibility annotation is defined. Repeat this option to supply multiple values. Elements without visibility annotations are unaffected by this setting.")
disableServiceTags = flag.Bool("disable_service_tags", false, "if set, disables generation of service tags. This is useful if you do not want to expose the names of your backend grpc services.")
disableDefaultResponses = flag.Bool("disable_default_responses", false, "if set, disables generation of default responses. Useful if you have to support custom response codes that are not 200.")
)

// Variables set by goreleaser at build time
Expand Down Expand Up @@ -126,6 +127,7 @@ func main() {
reg.SetOmitEnumDefaultValue(*omitEnumDefaultValue)
reg.SetVisibilityRestrictionSelectors(*visibilityRestrictionSelectors)
reg.SetDisableServiceTags(*disableServiceTags)
reg.SetDisableDefaultResponses(*disableDefaultResponses)
if err := reg.SetRepeatedPathParamSeparator(*repeatedPathParamSeparator); err != nil {
emitError(err)
return
Expand Down