@@ -23,9 +23,9 @@ func UseSwaggerDoc(router router.IRouterBuilder, info swagger.Info, configFunc f
23
23
_ = ctx .RequiredServices .GetService (& env )
24
24
baseUrl := fmt .Sprintf ("http://localhost:%s" , env .Port )
25
25
openapi := swagger .NewOpenApi (baseUrl , info )
26
+
26
27
configFunc (openapi )
27
- //openapi.AddSecurityBearerAuth()
28
- GetSwaggerRouteInfomation (openapi , router , env )
28
+ GetSwaggerRouteInformation (openapi , router , env )
29
29
ctx .JSON (200 , openapi )
30
30
})
31
31
@@ -76,15 +76,51 @@ func UseSwaggerDoc(router router.IRouterBuilder, info swagger.Info, configFunc f
76
76
})
77
77
}
78
78
79
- func GetSwaggerRouteInfomation (openapi * swagger.OpenApi , router router.IRouterBuilder , env * abstractions.HostEnvironment ) {
79
+ func GetSwaggerRouteInformation (openapi * swagger.OpenApi , router router.IRouterBuilder , env * abstractions.HostEnvironment ) {
80
+ // mvc routes
80
81
builder := router .GetMvcBuilder ()
81
82
controllerList := builder .GetControllerDescriptorList ()
82
83
for _ , controller := range controllerList {
83
- FilterValidParams (controller , openapi , env )
84
+ getMvcRouters (controller , openapi , env )
85
+ }
86
+
87
+ // default routes
88
+ getEndpointRouters (openapi , router , env )
89
+ }
90
+
91
+ func getEndpointRouters (openapi * swagger.OpenApi , router router.IRouterBuilder , env * abstractions.HostEnvironment ) {
92
+ // default normal route ,such as rb.POST("/info/:id", PostInfo)
93
+ routerInfoList := router .GetRouteInfo ()
94
+ openapi .Tags = append (openapi .Tags , swagger.Tag {Name : "default" , Description : fmt .Sprintf ("Endpoints of the default route. (%v)" , len (routerInfoList ))})
95
+ for idx , _ := range routerInfoList {
96
+ // uri parameters
97
+ pathInfo := swagger.Path {
98
+ Tags : []string {"default" },
99
+ Responses : map [string ]swagger.ResponsesItem {},
100
+ Parameters : []swagger.Parameters {}}
101
+
102
+ actPath := fmt .Sprintf ("/%s%s" , env .MetaData ["server.path" ], routerInfoList [idx ].Path )
103
+ // used regexp ,replace :id to {id}
104
+ if strings .Contains (actPath , ":" ) {
105
+ reg := regexp .MustCompile (`:([a-zA-Z0-9]+)` )
106
+ matches := reg .FindAllString (actPath , - 1 )
107
+ var params []swagger.Parameters
108
+ if len (matches ) > 0 {
109
+ for _ , match := range matches {
110
+ paramName := strings .Replace (match , ":" , "" , - 1 )
111
+ params = append (params , swagger.Parameters {In : "path" , Name : paramName })
112
+ }
113
+ pathInfo .Parameters = params
114
+ }
115
+ actPath = reg .ReplaceAllString (actPath , "{$1}" )
116
+ }
117
+
118
+ pathInfo .Responses ["200" ] = swagger.ResponsesItem {Description : "OK" }
119
+ openapi .Paths [actPath ] = map [string ]swagger.Path {strings .ToLower (routerInfoList [idx ].Method ): pathInfo }
84
120
}
85
121
}
86
122
87
- func FilterValidParams (controller mvc.ControllerDescriptor , openapi * swagger.OpenApi , env * abstractions.HostEnvironment ) {
123
+ func getMvcRouters (controller mvc.ControllerDescriptor , openapi * swagger.OpenApi , env * abstractions.HostEnvironment ) {
88
124
serverPath := env .MetaData ["server.path" ]
89
125
mvcTemplate := env .MetaData ["mvc.template" ]
90
126
// mvc
@@ -154,9 +190,8 @@ func FilterValidParams(controller mvc.ControllerDescriptor, openapi *swagger.Ope
154
190
actPath = act .Route .Template
155
191
actPath = fmt .Sprintf ("/%s%s" , serverPath , actPath )
156
192
// used regexp ,replace :id to {id}
157
- reg := regexp .MustCompile (`:[a-zA-Z0-9]+` )
158
- actPath = reg .ReplaceAllString (actPath , "{$0}" )
159
- actPath = strings .ReplaceAll (actPath , ":" , "" )
193
+ reg := regexp .MustCompile (`:([a-zA-Z0-9]+)` )
194
+ actPath = reg .ReplaceAllString (actPath , "{$1}" )
160
195
pathInfo .Summary = pathInfo .Summary + " ( Route Attribute ) "
161
196
} else {
162
197
pathInfo .Summary = pathInfo .Summary + " ( MVC ) "
0 commit comments