Skip to content

Commit f779d06

Browse files
fixes
1 parent 83584f8 commit f779d06

File tree

2 files changed

+17
-116
lines changed

2 files changed

+17
-116
lines changed

tools/cli/internal/openapi/filter/schemas.go

Lines changed: 17 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@
1515
package filter
1616

1717
import (
18+
"log"
19+
"maps"
1820
"strings"
1921

2022
"github.com/getkin/kin-openapi/openapi3"
2123
)
2224

23-
// SchemasFilter removes schemas that are not used in operations.
25+
// SchemasFilter removes unused #/components/schemas/.
2426
type SchemasFilter struct {
2527
oas *openapi3.T
2628
}
@@ -34,126 +36,26 @@ func (f *SchemasFilter) Apply() error {
3436
return nil
3537
}
3638

37-
usedRefs := map[string]bool{}
38-
allRefs := findRefs(f.oas)
39-
40-
// Extract unique references used in the OpenAPI document
41-
for ref := range allRefs {
42-
refParts := strings.Split(ref, "/")
43-
if len(refParts) >= 4 && refParts[1] == "components" && refParts[2] == "schemas" {
44-
usedRefs[refParts[3]] = true
45-
}
46-
}
47-
48-
//res2B, _ := json.Marshal(usedRefs)
49-
//fmt.Println("ANDREA:")
50-
//fmt.Println(string(res2B))
51-
52-
filterComponentSchemasInRefs(f.oas, usedRefs)
53-
return nil
54-
}
55-
56-
// findRefs returns all the ref included in an openapi spec.
57-
func findRefs(oas *openapi3.T) map[string]bool {
58-
if oas == nil {
59-
return nil
60-
}
61-
62-
refs := map[string]bool{}
63-
for _, v := range oas.Paths.Map() {
64-
refs[v.Ref] = true
65-
for _, op := range v.Operations() {
66-
for _, param := range op.Parameters {
67-
refs[param.Ref] = true
68-
if param.Value == nil {
69-
continue
70-
}
71-
findRefsSchemaRef(refs, param.Value.Schema)
72-
}
73-
74-
if op.RequestBody != nil {
75-
refs[op.RequestBody.Ref] = true
76-
if op.RequestBody.Value != nil {
77-
for _, content := range op.RequestBody.Value.Content {
78-
if content.Schema != nil {
79-
findRefsSchemaRef(refs, content.Schema)
80-
}
81-
}
82-
}
83-
}
84-
85-
for _, resp := range op.Responses.Map() {
86-
refs[resp.Ref] = true
87-
if resp.Value == nil {
88-
continue
89-
}
90-
for _, content := range resp.Value.Content {
91-
if content.Schema != nil {
92-
findRefsSchemaRef(refs, content.Schema)
93-
}
94-
}
95-
}
96-
}
97-
}
98-
99-
return refs
100-
}
101-
102-
func findRefsSchemasRefs(refs map[string]bool, schemas openapi3.SchemaRefs) {
103-
if schemas == nil {
104-
return
105-
}
106-
107-
for _, schema := range schemas {
108-
if ok := refs[schema.Ref]; ok {
109-
continue
110-
}
111-
112-
refs[schema.Ref] = true
113-
if schema.Value != nil {
114-
findRefsSchemasRefs(refs, schema.Value.AllOf)
115-
findRefsSchemasRefs(refs, schema.Value.OneOf)
116-
findRefsSchemasRefs(refs, schema.Value.AnyOf)
117-
findRefsSchemas(refs, schema.Value.Properties)
118-
}
119-
}
120-
}
121-
122-
func findRefsSchemas(refs map[string]bool, schemas openapi3.Schemas) {
123-
if schemas == nil {
124-
return
125-
}
126-
127-
for _, schema := range schemas {
128-
findRefsSchemaRef(refs, schema)
129-
}
130-
}
131-
132-
func findRefsSchemaRef(refs map[string]bool, schema *openapi3.SchemaRef) {
133-
if schema == nil {
134-
return
135-
}
136-
137-
refs[schema.Ref] = true
138-
if schema.Value == nil {
139-
return
39+
schemasToDelete := make([]string, 0)
40+
oasSpecAsBytes, err := f.oas.MarshalJSON()
41+
if err != nil {
42+
return err
14043
}
14144

142-
findRefsSchemasRefs(refs, schema.Value.AllOf)
143-
findRefsSchemasRefs(refs, schema.Value.OneOf)
144-
findRefsSchemasRefs(refs, schema.Value.AnyOf)
145-
}
146-
147-
func filterComponentSchemasInRefs(oas *openapi3.T, usedRefs map[string]bool) {
148-
schemasToDelete := make([]string, 0)
149-
for k, _ := range oas.Components.Schemas {
150-
//fmt.Printf("k: %s, v: %v", k, v)
151-
if ok := usedRefs[k]; !ok {
45+
spec := string(oasSpecAsBytes)
46+
for k := range f.oas.Components.Schemas {
47+
ref := "#/components/schemas/" + k
48+
if !strings.Contains(spec, ref) {
15249
schemasToDelete = append(schemasToDelete, k)
15350
}
15451
}
15552

15653
for _, schemaToDelete := range schemasToDelete {
157-
delete(oas.Components.Schemas, schemaToDelete)
54+
log.Printf("Deleting unused schema: '%s'", schemaToDelete)
55+
maps.DeleteFunc(f.oas.Components.Schemas, func(k string, _ *openapi3.SchemaRef) bool {
56+
return k == schemaToDelete
57+
})
15858
}
59+
60+
return nil
15961
}

tools/cli/internal/openapi/openapi.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ func (o *OasDiff) MergeOpenAPISpecs(paths []string) (*Spec, error) {
4949
if err != nil {
5050
return nil, err
5151
}
52-
5352
o.result, err = o.GetSimpleDiff(o.base, spec)
5453
if err != nil {
5554
log.Fatalf("error in calculating the diff of the specs: %s", err)

0 commit comments

Comments
 (0)