Skip to content

Commit 09b2c8b

Browse files
Ernest MickleiErnest Micklei
Ernest Micklei
authored and
Ernest Micklei
committed
use callback func to customise, enrich the created Swagger Object.
removes the Info field as this can be set in the callback fund.
1 parent c03d1e0 commit 09b2c8b

File tree

3 files changed

+35
-20
lines changed

3 files changed

+35
-20
lines changed

config.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,18 @@ import (
88
)
99

1010
// MapSchemaFormatFunc can be used to modify typeName at definition time.
11+
// To use it set the SchemaFormatHandler in the config.
1112
type MapSchemaFormatFunc func(typeName string) string
1213

1314
// MapModelTypeNameFunc can be used to return the desired typeName for a given
1415
// type. It will return false if the default name should be used.
16+
// To use it set the ModelTypeNameHandler in the config.
1517
type MapModelTypeNameFunc func(t reflect.Type) (string, bool)
1618

19+
// PostBuildSwaggerObjectFunc can be used to change the creates Swagger Object
20+
// before serving it. To use it set the PostBuildSwaggerObjectHandler in the config.
21+
type PostBuildSwaggerObjectFunc func(s *spec.Swagger)
22+
1723
// Config holds service api metadata.
1824
type Config struct {
1925
// WebServicesURL is the url where the services are available, e.g. http://localhost:8080
@@ -27,10 +33,10 @@ type Config struct {
2733
DisableCORS bool
2834
// Top-level API version. Is reflected in the resource listing.
2935
APIVersion string
30-
// OpenAPI global info struct
31-
Info spec.Info
3236
// [optional] If set, model builder should call this handler to get addition typename-to-swagger-format-field conversion.
3337
SchemaFormatHandler MapSchemaFormatFunc
3438
// [optional] If set, model builder should call this handler to retrieve the name for a given type.
3539
ModelTypeNameHandler MapModelTypeNameFunc
40+
// [optional] If set then call this function with the generated Swagger Object
41+
PostBuildSwaggerObjectHandler PostBuildSwaggerObjectFunc
3642
}

examples/user-resource.go

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -121,28 +121,35 @@ func main() {
121121
WebServices: restful.RegisteredWebServices(), // you control what services are visible
122122
WebServicesURL: "http://localhost:8080",
123123
APIPath: "/apidocs.json",
124-
Info: spec.Info{
125-
InfoProps: spec.InfoProps{
126-
Title: "UserService",
127-
Description: "Resource for managing Users",
128-
Contact: &spec.ContactInfo{
129-
Name: "john",
130-
Email: "john@doe.rp",
131-
URL: "http://johndoe.org",
132-
},
133-
License: &spec.License{
134-
Name: "MIT",
135-
URL: "http://mit.org",
136-
},
137-
Version: "1.0.0",
138-
},
139-
}}
124+
PostBuildSwaggerObjectHandler: enrichSwaggerObject}
140125
restfulspec.RegisterOpenAPIService(config, restful.DefaultContainer)
141126

142127
log.Printf("start listening on localhost:8080")
143128
log.Fatal(http.ListenAndServe(":8080", nil))
144129
}
145130

131+
func enrichSwaggerObject(swo *spec.Swagger) {
132+
swo.Info = &spec.Info{
133+
InfoProps: spec.InfoProps{
134+
Title: "UserService",
135+
Description: "Resource for managing Users",
136+
Contact: &spec.ContactInfo{
137+
Name: "john",
138+
Email: "john@doe.rp",
139+
URL: "http://johndoe.org",
140+
},
141+
License: &spec.License{
142+
Name: "MIT",
143+
URL: "http://mit.org",
144+
},
145+
Version: "1.0.0",
146+
},
147+
}
148+
swo.Tags = []spec.Tag{spec.Tag{TagProps: spec.TagProps{
149+
Name: "users",
150+
Description: "Managing users"}}}
151+
}
152+
146153
type User struct {
147154
Id, Name string
148155
}

spec_resource.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,14 @@ type specResource struct {
4444
}
4545

4646
func (s specResource) getSwagger(req *restful.Request, resp *restful.Response) {
47-
sw := spec.Swagger{
47+
sw := &spec.Swagger{
4848
SwaggerProps: spec.SwaggerProps{
49-
Info: &s.config.Info,
5049
Swagger: "2.0",
5150
Paths: &(s.paths),
5251
},
5352
}
53+
if s.config.PostBuildSwaggerObjectHandler != nil {
54+
s.config.PostBuildSwaggerObjectHandler(sw)
55+
}
5456
resp.WriteAsJson(sw)
5557
}

0 commit comments

Comments
 (0)