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

[Feature Request]auto gen swagger comment for handler #766

Open
Rainshaw opened this issue May 6, 2023 · 5 comments
Open

[Feature Request]auto gen swagger comment for handler #766

Rainshaw opened this issue May 6, 2023 · 5 comments
Assignees
Labels
enhancement New feature or request

Comments

@Rainshaw
Copy link

Rainshaw commented May 6, 2023

Is your feature request related to a problem? Please describe.

when using idl for generate code, we currently add some simple comments for each handler

func (m *HttpMethod) InitComment() {
text := strings.TrimLeft(strings.TrimSpace(m.Comment), "/")
if text == "" {
text = "// " + m.Name + " ."
} else if strings.HasPrefix(text, m.Name) {
text = "// " + text
} else {
text = "// " + m.Name + " " + text
}
text = strings.Replace(text, "\n", "\n// ", -1)
if !strings.Contains(text, "@router ") {
text += "\n// @router " + m.Path
}
m.Comment = text + " [" + m.HTTPMethod + "]"
}

but we have fully information for this endpoint, e.g. http method, serializer type, url path, request params, response params, and we have full info for request struct and response struct, like which field is used in path/query/body. So we can add a flag for hz to control generating swagger style comment for each method.

previous relates issues #28, #538, #558, #692

Describe the solution you'd like

In HttpMethod.InitComment, generate swagger style comment for using https://github.com/hertz-contrib/swagger

Describe alternatives you've considered

maybe we could provider a template like layout and package for the swagger comments.

Additional context

Add any other context or screenshots about the feature request here.

@FGYFFFF
Copy link
Contributor

FGYFFFF commented May 6, 2023

Good idea, I'll add a responsive comment feature after I've researched it clearly

@Rainshaw Rainshaw changed the title auto gen swagger comment for handler [Feature Request]auto gen swagger comment for handler May 6, 2023
@li-jin-gou li-jin-gou added the enhancement New feature or request label May 6, 2023
@Rainshaw
Copy link
Author

Rainshaw commented May 8, 2023

                comment := "// " + m.Name + "\n"
		comment += "// @Summary " + text + "\n"
		comment += "// @Description \n"
		comment += "// @Tags " + strings.Join(strings.Split(m.OutputDir, "/"), " ") + "\n"
		if m.Serializer == "JSON" {
			comment += "// @Accept json\n// @Produce json\n"
		}
		comment += "// @Response 200 {object} " + m.ReturnTypeName + "\n"
		comment += "// @Router " + m.Path + " [" + m.HTTPMethod + "]"
		m.Comment = comment

I find some pain points:

  1. router in hertz use ':' for path params but in openapi it uses {}
  2. m.RequestTypeName can not simply used in comment
  3. text is always blank string, as for thriftgo now lack support for comment.

@FGYFFFF
Copy link
Contributor

FGYFFFF commented May 9, 2023

good idea~

@ringsaturn
Copy link

I have been working around on another approach, an example here: https://github.com/ringsaturn/protoc-gen-go-hertz/tree/main/_example/hertz-blog-service

@li-jin-gou
Copy link
Member

I have been working around on another approach, an example here: https://github.com/ringsaturn/protoc-gen-go-hertz/tree/main/_example/hertz-blog-service

👍🏻,可以联系我下,加入到社区开发者群中。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Development

No branches or pull requests

4 participants