Skip to content

Commit efd73ff

Browse files
committed
Handle gemini review and lint issue
Signed-off-by: GUAN MING <guanmingchiu@gmail.com>
1 parent 87f2534 commit efd73ff

1 file changed

Lines changed: 45 additions & 30 deletions

File tree

utils/component/openapi_generator.go

Lines changed: 45 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -204,132 +204,147 @@ func getResolvedManifest(manifest string) (string, error) {
204204
return string(resolved), nil
205205
}
206206

207-
// clearDocRefs clears $ref strings across the entire OpenAPI document,
208-
// including paths, components/parameters, components/headers,
209-
// components/requestBodies, components/responses, and components/callbacks.
207+
// clearDocRefs clears $ref strings across the entire OpenAPI document.
210208
func clearDocRefs(doc *openapi3.T) {
211209
stack := make(map[*openapi3.Schema]bool)
210+
visited := make(map[*openapi3.PathItem]bool)
212211

213212
if doc.Components != nil {
214213
for _, sr := range doc.Components.Schemas {
215214
clearSchemaRefs(sr, stack)
216215
}
217216
for _, pr := range doc.Components.Parameters {
218-
clearParameterRefs(pr, stack)
217+
clearParameterRefs(pr, stack, visited)
219218
}
220219
for _, hr := range doc.Components.Headers {
221-
clearHeaderRefs(hr, stack)
220+
clearHeaderRefs(hr, stack, visited)
222221
}
223222
for _, rb := range doc.Components.RequestBodies {
224-
clearRequestBodyRefs(rb, stack)
223+
clearRequestBodyRefs(rb, stack, visited)
225224
}
226225
for _, rr := range doc.Components.Responses {
227-
clearResponseRefs(rr, stack)
226+
clearResponseRefs(rr, stack, visited)
228227
}
229228
for _, cr := range doc.Components.Callbacks {
230-
clearCallbackRefs(cr, stack)
229+
clearCallbackRefs(cr, stack, visited)
230+
}
231+
for _, er := range doc.Components.Examples {
232+
if er != nil {
233+
er.Ref = ""
234+
}
235+
}
236+
for _, lr := range doc.Components.Links {
237+
if lr != nil {
238+
lr.Ref = ""
239+
}
240+
}
241+
for _, ssr := range doc.Components.SecuritySchemes {
242+
if ssr != nil {
243+
ssr.Ref = ""
244+
}
231245
}
232246
}
233247

234248
if doc.Paths != nil {
235249
for _, pathItem := range doc.Paths.Map() {
236-
clearPathItemRefs(pathItem, stack)
250+
clearPathItemRefs(pathItem, stack, visited)
237251
}
238252
}
239253
}
240254

241-
func clearContentRefs(content openapi3.Content, stack map[*openapi3.Schema]bool) {
255+
func clearContentRefs(content openapi3.Content, stack map[*openapi3.Schema]bool, visited map[*openapi3.PathItem]bool) {
242256
for _, mt := range content {
243257
if mt != nil {
244258
clearSchemaRefs(mt.Schema, stack)
245259
}
246260
}
247261
}
248262

249-
func clearParameterRefs(pr *openapi3.ParameterRef, stack map[*openapi3.Schema]bool) {
263+
func clearParameterRefs(pr *openapi3.ParameterRef, stack map[*openapi3.Schema]bool, visited map[*openapi3.PathItem]bool) {
250264
if pr == nil {
251265
return
252266
}
253267
pr.Ref = ""
254268
if pr.Value != nil {
255269
clearSchemaRefs(pr.Value.Schema, stack)
256-
clearContentRefs(pr.Value.Content, stack)
270+
clearContentRefs(pr.Value.Content, stack, visited)
257271
}
258272
}
259273

260-
func clearHeaderRefs(hr *openapi3.HeaderRef, stack map[*openapi3.Schema]bool) {
274+
func clearHeaderRefs(hr *openapi3.HeaderRef, stack map[*openapi3.Schema]bool, visited map[*openapi3.PathItem]bool) {
261275
if hr == nil {
262276
return
263277
}
264278
hr.Ref = ""
265279
if hr.Value != nil {
266280
clearSchemaRefs(hr.Value.Schema, stack)
267-
clearContentRefs(hr.Value.Content, stack)
281+
clearContentRefs(hr.Value.Content, stack, visited)
268282
}
269283
}
270284

271-
func clearRequestBodyRefs(rb *openapi3.RequestBodyRef, stack map[*openapi3.Schema]bool) {
285+
func clearRequestBodyRefs(rb *openapi3.RequestBodyRef, stack map[*openapi3.Schema]bool, visited map[*openapi3.PathItem]bool) {
272286
if rb == nil {
273287
return
274288
}
275289
rb.Ref = ""
276290
if rb.Value != nil {
277-
clearContentRefs(rb.Value.Content, stack)
291+
clearContentRefs(rb.Value.Content, stack, visited)
278292
}
279293
}
280294

281-
func clearResponseRefs(rr *openapi3.ResponseRef, stack map[*openapi3.Schema]bool) {
295+
func clearResponseRefs(rr *openapi3.ResponseRef, stack map[*openapi3.Schema]bool, visited map[*openapi3.PathItem]bool) {
282296
if rr == nil {
283297
return
284298
}
285299
rr.Ref = ""
286300
if rr.Value != nil {
287-
clearContentRefs(rr.Value.Content, stack)
301+
clearContentRefs(rr.Value.Content, stack, visited)
288302
for _, hr := range rr.Value.Headers {
289-
clearHeaderRefs(hr, stack)
303+
clearHeaderRefs(hr, stack, visited)
290304
}
291305
}
292306
}
293307

294-
func clearCallbackRefs(cr *openapi3.CallbackRef, stack map[*openapi3.Schema]bool) {
308+
func clearCallbackRefs(cr *openapi3.CallbackRef, stack map[*openapi3.Schema]bool, visited map[*openapi3.PathItem]bool) {
295309
if cr == nil {
296310
return
297311
}
298312
cr.Ref = ""
299313
if cr.Value != nil {
300314
for _, pathItem := range cr.Value.Map() {
301-
clearPathItemRefs(pathItem, stack)
315+
clearPathItemRefs(pathItem, stack, visited)
302316
}
303317
}
304318
}
305319

306-
func clearPathItemRefs(pathItem *openapi3.PathItem, stack map[*openapi3.Schema]bool) {
307-
if pathItem == nil {
320+
func clearPathItemRefs(pathItem *openapi3.PathItem, stack map[*openapi3.Schema]bool, visited map[*openapi3.PathItem]bool) {
321+
if pathItem == nil || visited[pathItem] {
308322
return
309323
}
324+
visited[pathItem] = true
310325
for _, pr := range pathItem.Parameters {
311-
clearParameterRefs(pr, stack)
326+
clearParameterRefs(pr, stack, visited)
312327
}
313328
for _, op := range pathItem.Operations() {
314-
clearOperationRefs(op, stack)
329+
clearOperationRefs(op, stack, visited)
315330
}
316331
}
317332

318-
func clearOperationRefs(op *openapi3.Operation, stack map[*openapi3.Schema]bool) {
333+
func clearOperationRefs(op *openapi3.Operation, stack map[*openapi3.Schema]bool, visited map[*openapi3.PathItem]bool) {
319334
if op == nil {
320335
return
321336
}
322337
for _, pr := range op.Parameters {
323-
clearParameterRefs(pr, stack)
338+
clearParameterRefs(pr, stack, visited)
324339
}
325-
clearRequestBodyRefs(op.RequestBody, stack)
340+
clearRequestBodyRefs(op.RequestBody, stack, visited)
326341
if op.Responses != nil {
327342
for _, rr := range op.Responses.Map() {
328-
clearResponseRefs(rr, stack)
343+
clearResponseRefs(rr, stack, visited)
329344
}
330345
}
331346
for _, cr := range op.Callbacks {
332-
clearCallbackRefs(cr, stack)
347+
clearCallbackRefs(cr, stack, visited)
333348
}
334349
}
335350

0 commit comments

Comments
 (0)