|
| 1 | +// Copyright 2025 MongoDB Inc |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package filter |
| 16 | + |
| 17 | +import ( |
| 18 | + "encoding/json" |
| 19 | + "fmt" |
| 20 | + "github.com/getkin/kin-openapi/openapi3" |
| 21 | + "strings" |
| 22 | +) |
| 23 | + |
| 24 | +// SchemasFilter removes tags that are not used in the operations. |
| 25 | +type SchemasFilter struct { |
| 26 | + oas *openapi3.T |
| 27 | +} |
| 28 | + |
| 29 | +func (f *SchemasFilter) Apply() error { |
| 30 | + if f.oas.Paths == nil { |
| 31 | + return nil |
| 32 | + } |
| 33 | + |
| 34 | + usedRefs := map[string]bool{} |
| 35 | + allRefs := findRefs(f.oas) |
| 36 | + |
| 37 | + // Extract unique references used in the OpenAPI document |
| 38 | + for ref := range allRefs { |
| 39 | + refParts := strings.Split(ref, "/") |
| 40 | + if len(refParts) >= 4 && refParts[1] == "components" && refParts[2] == "schemas" { |
| 41 | + usedRefs[refParts[3]] = true |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + res2B, _ := json.Marshal(usedRefs) |
| 46 | + fmt.Println("ANDREA:") |
| 47 | + fmt.Println(string(res2B)) |
| 48 | + |
| 49 | + filterComponentSchemasInRefs(f.oas, usedRefs) |
| 50 | + return nil |
| 51 | +} |
| 52 | + |
| 53 | +// findRefs returns all the ref included in an openapi spec |
| 54 | +func findRefs(oas *openapi3.T) map[string]bool { |
| 55 | + if oas == nil { |
| 56 | + return nil |
| 57 | + } |
| 58 | + |
| 59 | + refs := map[string]bool{} |
| 60 | + for _, v := range oas.Paths.Map() { |
| 61 | + refs[v.Ref] = true |
| 62 | + for _, op := range v.Operations() { |
| 63 | + for _, param := range op.Parameters { |
| 64 | + refs[param.Ref] = true |
| 65 | + } |
| 66 | + |
| 67 | + if op.RequestBody != nil { |
| 68 | + refs[op.RequestBody.Ref] = true |
| 69 | + } |
| 70 | + |
| 71 | + for _, resp := range op.Responses.Map() { |
| 72 | + refs[resp.Ref] = true |
| 73 | + |
| 74 | + for _, content := range resp.Value.Content { |
| 75 | + if content.Schema != nil { |
| 76 | + refs[content.Schema.Ref] = true |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + } |
| 83 | + |
| 84 | + return refs |
| 85 | +} |
| 86 | + |
| 87 | +func filterComponentSchemasInRefs(oas *openapi3.T, usedRefs map[string]bool) { |
| 88 | + schemasToDelete := make([]string, 0) |
| 89 | + for k, v := range oas.Components.Schemas { |
| 90 | + fmt.Printf("k: %s, v: %v", k, v) |
| 91 | + if ok := usedRefs[k]; !ok { |
| 92 | + schemasToDelete = append(schemasToDelete, k) |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + for _, schemaToDelete := range schemasToDelete { |
| 97 | + delete(oas.Components.Schemas, schemaToDelete) |
| 98 | + } |
| 99 | +} |
0 commit comments