-
Notifications
You must be signed in to change notification settings - Fork 8k
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
Automatically generate RESTful API documentation with Swagger #155
Comments
+1 |
1 similar comment
+1 |
+1, swagger is very useful for API development |
👍 |
- Reorganize roadmap for v1.0, done in the bottom, to do at top. - Added request for #155, swagger support.
@phalt @redstrike @matejkramny @rogeriomarques @ndbroadbent I'll add support for it with v0.6 in mind. Thank you for submitting this request. |
+1 |
I'm gonna raise the biddings on this issue, Ok for Swagger, but why not RAML? I'll explore what can be done with my limited golang skills, and keep you posted |
One of the main concerns in thinking this feature out, is design this swagger/raml feature producing a base json/raml document that can be extended to let the user exploit the full-feature set of their API specifications. I hardly seen that in other frameworks, but I'm hoping someone could propose some input regarding this |
+1 for swagger support |
I'm surprised there isn't someone who has created a parser that looks at an AST + attached routes for net/http to generate Swagger/RAML style documentation. If it isn't a project, perhaps that would be the better method of implementation for this? Rather than having it part of Gin core, if it was a separate binary that scanned a package? |
I think to externalize API documentation, gin.router should be exported (so Router). |
Noticed Swagger support was on the README checklist for V1. That seems to be gone now. Should we not have our hopes up for this being released? |
@dustinwtf We do not need swagger support in 1.0. Once 1.0 final is reached, we can focus in features. Also, I do not think the swagger support should be part of Gin core. Probably a different package. |
Agreed! That's definitely a good focus. Just wondered considering it was there and disappeared. |
Is there an easy way to get all the registered routes now? Like a map of url -> handlers or something. |
@nazwa no, we need to implement it. Two solutions:
While the second way is the easiest to implement it adds a unneeded overhead and complexity all over the place. So I would try to iterate the trees, it can not be that hard.
|
Also, since the router is not longer dependent of the HttpRouter package, we have access to all the info. |
something like this, I do not have time to do it just now. If anybody is interested, please create a PR!! func (engine *Engine) Routes() (routes []RouteInfo) {
for _, root := range engine.trees {
routes = iterate(routes, root)
}
return routes
}
func iterate(routes []RouteInfo, root *node) []RouteInfo {
for _, node := range root.children {
routes = iterate(routes, node)
}
if root.handlers != nil {
routes = append(routes,root.path)
}
return routes
} |
Yea, something like this would be perfect. I'll try to have a proper play with it in the evening, as it definitely would be useful in the long run. |
@nazwa @dustinwtf @ndbroadbent 45dd777 |
router := gin.New()
router.GET("/favicon.ico", handler)
router.GET("/", handler)
group := router.Group("/users")
group.GET("/", handler)
group.GET("/:id", handler)
group.POST("/:id", handler)
router.Static("/static", ".")
fmt.Println(router.Routes())
|
Here's a proposal for auto API documentation:
The swagger generator will use the information from the comments + engine.Routes() to generate the documentation. |
This is perfect, @manucorporat! When do you see this being merged into master? |
@dustinwtf done! 451f3b9 |
@manucorporat Looks great, thanks! Is there any reason to return the handler function name as a string, not as a |
@zserge A pointer to a function is too low level, in my opinion, a high level API (like Gin) should hide low level details, it makes everybody's code safer. For example: if the middleware API would allow to jump between handlers and do crazy things, the whole community of middleware would be more error-prone and unsecure. I like the idea of providing the filename and line, that's why I created the |
+1 |
1 similar comment
+1 |
I'm interested in this idea, and I made a middleware using @savaki 's swag library, that adds a validation middleware. This ensures that api calls have valid params based on the defined spec. https://github.com/miketonks/swag-validator Feedback welcome. |
+1 |
2 similar comments
+1 |
+1 |
any updates on this? Would love to see the swagger api description page within gin |
+1 |
1 similar comment
+1 |
Can everyone please stop spamming |
@schrej You are right, but I guess we did that in order to utterly highlight the importance of the feature 😉 |
personally, I am using https://github.com/swaggo/gin-swagger thanks @easonlin404 |
@thinkerou You'll encounter a lot of limitations when you get to base models, custom types etc. If it's a small project gin-swagger is fine though. |
@frg thank you for reminding, I also use it for personal small project right now. |
Has anyone found a favorite approach to this? |
hello world! |
Hi everyone, |
+1 |
+1 |
I really love the concept of Fast-API which can auto generate swagger. |
I'm using https://github.com/caicloud/nirvana to generate swagger automatic. But nirvana is not continued. |
I'm wondering if it's possible to generate the e2e testing automatically. |
I've developed an implicit OpenAPI generator called astra that will take in your gin configuration and generate an OpenAPI 3.0 specification. Hopefully this helps solve the issue above. |
I'll point out that my framework https://github.com/danielgtaylor/huma is loosely based on FastAPI and able to provide OpenAPI & JSON Schema on top of Gin (and other popular routers). It provides a bunch of useful features and there's a simple 5 minute tutorial to get you started. Here's the tutorial code converted to use Gin and runnable in the Go playground: Hopefully this helps somebody. |
this was opened 10 years ago. I know that there have been 3rd party solutions, but, personally I would prefer a gin solution to prevent any breaking in the future |
Swagger is indeed a solid choice, but I'm currently using Apidog for automatic document generation. One of the aspects that really attracts me to Apidog is the aesthetic appeal of its UI. |
bulit-in support for generate openapi.json/yaml is definity great feature. |
One thing I really like about go-restful is their built-in Swagger integration. Check out the example project: https://github.com/emicklei/mora#swagger
Some things I really love about Gin is the focus on performance, and overall, it just seems like a much better designed framework. However, I think that auto-generated documentation with Swagger is pretty awesome. It makes it easy for new engineers to get up-to-speed quickly by reading through the docs, and I especially love the way you can auto-generate API clients for lots of different languages.
I'm not sure how involved this change would be, I'm not sure if this could be done as an extension. But we could probably take lots of inspiration from (or just copy) the swagger code from go-restful (MIT licensed).
Anyway, this is not a huge priority for me, just a nice to have.
The text was updated successfully, but these errors were encountered: