wI2L / fizz

:lemon: Gin wrapper with OpenAPI 3 spec generation
https://pkg.go.dev/github.com/wI2L/fizz
MIT License
214 stars 52 forks source link

Add API Group #49

Closed hmajid2301 closed 3 years ago

hmajid2301 commented 3 years ago

Is there an easy way to define an API Group like /api/v1/ or /v1/ to all paths ? Or do we have to simply add to every group in the "routes" file

thanks

wI2L commented 3 years ago

Routes are added to groups, so I'm not sure I understand what you mean by:

define an API Group like /api/v1/ or /v1/ to all paths

Aren't you refering to OpenAPI tags instead ?

hmajid2301 commented 3 years ago

Sorry I meant in an openapi spec (v2) I can specify something like

basePath: "/api/v1"

And I cannot remember exactly how you can do that in V3.

But I was wondering is there any easy way with add say prepend /api/v1 to all endpoints ?

wI2L commented 3 years ago

The Swagger v2 documentation regarding that subject is here: https://swagger.io/docs/specification/2-0/api-host-and-base-path/, and the equivalent for OpenAPI 3.x is https://swagger.io/docs/specification/api-host-and-base-path/.

OpenAPI v3 introduced the Server object. With fizz, you can see that the root object defines a Servers field.

To set a list of servers, see the following snippet:

    f := fizz.NewFromEngine(r)
    if url != "" {
        f.Generator().SetServers([]*openapi.Server{
            &openapi.Server{
                URL:             url,
                Description: description,
            },
        })
    }

The Server type is defined here.

hmajid2301 commented 3 years ago

Thanks but if I wanted the route in the application itself to change, will this work ? or do I need to do it a the group level like so

routes(fizz.Group("/api/v1/market", "market", "Your daily dose of freshness"))

wI2L commented 3 years ago

Your questions are confusing. You started talking about router groups, and then switched to mention thebasePath property of Swagger. To clarify, router groups are managed by the underlying router, Gin. The name of the groups is used to construct a list of tags in the OpenAPI spec. And finally, the basePath/URL server is just a static prefix.

So I think what your're looking for is using router groups. See the following example adapted from the README:

f := fizz.New()
api := f.Group("/api", "API", "API group")
v1 := api.Group("/v1", "v1 routes", "Version 1")

// /api/v1/{id}
bar.GET("/:id", nil, tonic.Handler(MyHandler, 200))

Does it answer your question ?

hmajid2301 commented 3 years ago

Yeh sorry for the confusion, I was basically asking how can we replicate the basepath via gin using Fizz. I didn't phrase it properly.