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

fix: set consumes definition per operation #2995

Merged
merged 3 commits into from Nov 8, 2022
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
5 changes: 5 additions & 0 deletions protoc-gen-openapiv2/internal/genopenapi/template.go
Expand Up @@ -1505,6 +1505,11 @@ func renderServices(services []*descriptor.Service, paths openapiPathsObject, re
operationObject.extensions = exts
}

if len(opts.Consumes) > 0 {
operationObject.Consumes = make([]string, len(opts.Consumes))
copy(operationObject.Consumes, opts.Consumes)
}

if len(opts.Produces) > 0 {
operationObject.Produces = make([]string, len(opts.Produces))
copy(operationObject.Produces, opts.Produces)
Expand Down
10 changes: 9 additions & 1 deletion protoc-gen-openapiv2/internal/genopenapi/template_test.go
Expand Up @@ -1633,7 +1633,7 @@ func TestApplyTemplateOpenAPIConfigFromYAML(t *testing.T) {
}
}

func TestApplyTemplateOverrideOperationID(t *testing.T) {
func TestApplyTemplateOverrideWithOperation(t *testing.T) {
newFile := func() *descriptor.File {
msgdesc := &descriptorpb.DescriptorProto{
Name: proto.String("ExampleMessage"),
Expand Down Expand Up @@ -1717,6 +1717,12 @@ func TestApplyTemplateOverrideOperationID(t *testing.T) {
if want, is := "MyExample", result.Paths["/v1/echo"].Get.OperationID; !reflect.DeepEqual(is, want) {
t.Errorf("applyTemplate(%#v).Paths[0].Get.OperationID = %s want to be %s", *file, is, want)
}
if want, is := []string{"application/xml"}, result.Paths["/v1/echo"].Get.Consumes; !reflect.DeepEqual(is, want) {
t.Errorf("applyTemplate(%#v).Paths[0].Get.Consumes = %s want to be %s", *file, is, want)
}
if want, is := []string{"application/json", "application/xml"}, result.Paths["/v1/echo"].Get.Produces; !reflect.DeepEqual(is, want) {
t.Errorf("applyTemplate(%#v).Paths[0].Get.Produces = %s want to be %s", *file, is, want)
}

// If there was a failure, print out the input and the json result for debugging.
if t.Failed() {
Expand All @@ -1727,6 +1733,8 @@ func TestApplyTemplateOverrideOperationID(t *testing.T) {

openapiOperation := openapi_options.Operation{
OperationId: "MyExample",
Consumes: []string{"application/xml"},
Produces: []string{"application/json", "application/xml"},
}

t.Run("verify override via method option", func(t *testing.T) {
Expand Down
1 change: 1 addition & 0 deletions protoc-gen-openapiv2/internal/genopenapi/types.go
Expand Up @@ -126,6 +126,7 @@ type openapiOperationObject struct {
Parameters openapiParametersObject `json:"parameters,omitempty" yaml:"parameters,omitempty"`
Tags []string `json:"tags,omitempty" yaml:"tags,omitempty"`
Deprecated bool `json:"deprecated,omitempty" yaml:"deprecated,omitempty"`
Consumes []string `json:"consumes,omitempty" yaml:"consumes,omitempty"`
Produces []string `json:"produces,omitempty" yaml:"produces,omitempty"`

Security *[]openapiSecurityRequirementObject `json:"security,omitempty" yaml:"security,omitempty"`
Expand Down