swaggo / swag

Automatically generate RESTful API documentation with Swagger 2.0 for Go.
MIT License
10.24k stars 1.17k forks source link

Add suport for parsing comments inside of function bodies #1809

Open j-d-ha opened 2 months ago

j-d-ha commented 2 months ago

Problem

Hello all!

As it currently stands, comments inside of a function body are not parsed by swag. As an example, given an anonymous handler inside of a function with a comment and another handler outside, also with a comment:

package main

// @Router /api/outside [get]
func outside() {}

func main(){
    // @Router /api/inside [get]
    _ = func() {}
}

// @Router /api/inside [get] will not be parsed while // @Router /api/outside [get] will get parsed. This behavior means that the use of anonymous function handlers is not possible if you also want to use swag.

Solution

I would like to add support for parsing comments inside of function bodies. This could be achieved by adding an optional boolean flag to control parsing all comments as opposed to only doc comments as it is today. The ast.File object that is currently used for parsing doc comments already contains all comments from a given source file. As such, adding this feature would require only minimal changes to the package. Additionally, by adding a flag to control this behavior, it would be opt-in only and would not disrupt users who are expecting the current behavior.

j-d-ha commented 1 month ago

I have created a PR here for this suggested feature: https://github.com/swaggo/swag/pull/1824.