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

protoc-gen-swagger map type filed doesn't support options.openapiv2_field #1601

Closed
yangxikun opened this issue Aug 15, 2020 · 2 comments · Fixed by #1602
Closed

protoc-gen-swagger map type filed doesn't support options.openapiv2_field #1601

yangxikun opened this issue Aug 15, 2020 · 2 comments · Fixed by #1602

Comments

@yangxikun
Copy link
Contributor

🐛 Bug Report

message GetSubjectPermissions2DReply{
  map<string, ListOfString> permissions = 1 [(grpc.gateway.protoc_gen_swagger.options.openapiv2_field) = {title: "展开的二级权限列表", description:"展开的二级权限列表"}];
}

generated swagger json:

    "HodorGetSubjectPermissions2DReply": {
      "type": "object",
      "properties": {
        "permissions": {
          "type": "object",
          "additionalProperties": {
            "$ref": "#/definitions/HodorListOfString"
          }
        }
      },
      "title": "GetSubjectPermissions2DReply"
    },

permissions.title and permissions.description is not set.

Your Environment

grpc-gateway v1.14.6

Other Info

I dig into the source code of protoc-gen-swagger, found the problem is in template.go:schemaOfField

// schemaOfField returns a swagger Schema Object for a protobuf field.
func schemaOfField(f *descriptor.Field, reg *descriptor.Registry, refs refMap) swaggerSchemaObject {
	const (
		singular = 0
		array    = 1
		object   = 2
	)
	var (
		core      schemaCore
		aggregate int
	)

	fd := f.FieldDescriptorProto
	if m, err := reg.LookupMsg("", f.GetTypeName()); err == nil {
		if opt := m.GetOptions(); opt != nil && opt.MapEntry != nil && *opt.MapEntry {
			fd = m.GetField()[1]
			aggregate = object
		}
	}
	if fd.GetLabel() == pbdescriptor.FieldDescriptorProto_LABEL_REPEATED {
		aggregate = array
	}
        ……
}

When the field is map type, fd = m.GetField()[1] use the value's FieldDescriptorProto. Because the mapentry is auto generate, so the fd.Options is always nil.

@johanbrandhorst
Copy link
Collaborator

Hi, thanks for the bug report. Really nice work debugging this, would you be willing to submit a fix? I'd start by adding a failing test, and then try and modify the code.

@yangxikun
Copy link
Contributor Author

@johanbrandhorst The master branch can reproduce this bug, I will submit a PR in the next few days.

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 a pull request may close this issue.

2 participants