go-swagger dynamic API creation

428 views
Skip to first unread message

Johann Höchtl

unread,
Mar 27, 2017, 2:39:48 PM3/27/17
to golang-nuts
The last time I used it swagger was called swagger.

Lots has changed since it's OpenAPI. A huge framework evolved around it https://goswagger.io/

I really like the approach of defining the API entirely dynamically in code (and announcements like https://groups.google.com/forum/#!topic/golang-nuts/3ebgsgF6W2c, nice!) . Unless I misunderstand goswagger.io, nothing prevents the drifting apart of the generated code from the YML - api spec.

There is also an example to dynamically generate the swagger spec using goswagger.io
https://goswagger.io/tutorial/dynamic.html
and I wonder if there is experience using that. Especially is it in feature parity with the go generate approach of  goswagger.io?

Thank you!


Matt Ho

unread,
Mar 27, 2017, 3:00:58 PM3/27/17
to golang-nuts
Before writing github.com/savaki/swag, I gave goswagger a try.  I think goswagger is a fantastic library with lots of useful features.  However, for my own use, I found things like:

var findTodos = runtime.OperationHandlerFunc(func(params interface{}) (interface{}, error) {
    log.Println("received 'findTodos'")
    log.Printf("%#v\n", params)

    return items, nil
})

a little cumbersome.  I also wanted to be able to use automatic code reload tools like https://github.com/codegangsta/gin and code generation made that a little more problematic.

Hence was born:


M

Kevin Conway

unread,
Mar 28, 2017, 10:39:38 PM3/28/17
to Matt Ho, golang-nuts
Having used go-swagger (https://github.com/go-swagger/go-swagger) at one point, I'd say that these YAML generators are possibly useful for generating documentation from your code. One pain point of the documentation generators, though, is that most require that I create and maintain code objects exclusively for the benefit of doc generation. At that point, I'd rather maintain docs than unused code objects. That's a personal preference, though.

Now, if you're doing any amount of contract negotiation with consumers, attempting to implement an API contract, intending to perform any amount of contract testing, or are targeting any form of contract driven development then these generator tools are a complete inversion of the model you want. If any of the previous statements are true then you should consider finding code generators that consume swagger/OpenAPI documents and generate code skeletons to fill in rather than YAML generators that leverage your code.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages