Skip to content

Commit 28e609e

Browse files
fix: remove top level extensions (#406)
1 parent 51f1c77 commit 28e609e

File tree

2 files changed

+25
-9
lines changed

2 files changed

+25
-9
lines changed

remove_elements.go

+2
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ func RemoveElements(doc *openapi3.T, excludes ExcludePatterns) error {
6363
}
6464

6565
func (ex *excluder) apply() error {
66+
// Remove top-level extensions
67+
ex.applyExtensions(ex.doc.Extensions)
6668
for _, pathItem := range ex.doc.Paths.Map() {
6769
ex.applyExtensions(pathItem.Extensions)
6870
for _, operation := range pathItem.Operations() {

remove_elements_test.go

+23-9
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,16 @@ func TestRemoveElementsExact(t *testing.T) {
3333
"x-private-matter",
3434
)
3535

36+
// PATH-LEVEL EXTENSION
3637
c.Assert(doc.Paths.Value("/orgs/{orgId}/projects").Extensions["x-snyk-api-resource"], qt.Not(qt.IsNil))
38+
39+
// DOC-LEVEL EXTENSION (Root of the OpenAPI)
3740
c.Assert(doc.Extensions["x-snyk-api-lifecycle"], qt.Not(qt.IsNil))
3841

3942
// Remove some of them
4043

4144
err = vervet.RemoveElements(doc.T, vervet.ExcludePatterns{
42-
ExtensionPatterns: []string{"x-snyk-api-releases", "x-snyk-api-resource"},
45+
ExtensionPatterns: []string{"x-snyk-api-releases", "x-snyk-api-resource", "x-snyk-api-lifecycle"},
4346
HeaderPatterns: []string{"snyk-request-id", "x-private-matter"},
4447
Paths: []string{"/examples/hello-world", "/examples/hello-world/{id}"},
4548
})
@@ -60,8 +63,11 @@ func TestRemoveElementsExact(t *testing.T) {
6063
c.Assert(doc.Paths.Value("/orgs/{org_id}/projects/{project_id}").
6164
Delete.Parameters, qt.HasLen, 3) // x-private-matter removed
6265

63-
c.Assert(doc.Paths.Value("/orgs/{orgId}/projects").Extensions["x-snyk-api-resource"], qt.IsNil) // now removed
64-
c.Assert(doc.Extensions["x-snyk-api-lifecycle"], qt.Not(qt.IsNil)) // still there
66+
// PATH-LEVEL EXTENSION removed
67+
c.Assert(doc.Paths.Value("/orgs/{orgId}/projects").Extensions["x-snyk-api-resource"], qt.IsNil)
68+
69+
// DOC-LEVEL EXTENSION removed
70+
c.Assert(doc.Extensions["x-snyk-api-lifecycle"], qt.IsNil)
6571
}
6672

6773
func TestRemoveElementsRegex(t *testing.T) {
@@ -91,12 +97,17 @@ func TestRemoveElementsRegex(t *testing.T) {
9197
)
9298

9399
c.Assert(doc.Paths.Value("/orgs/{orgId}/projects").Extensions["x-snyk-api-resource"], qt.Not(qt.IsNil))
94-
c.Assert(doc.Extensions["x-snyk-api-lifecycle"], qt.Not(qt.IsNil))
95100

96-
// Remove some of them
101+
// DOC-LEVEL EXTENSION (root)
102+
c.Assert(doc.Extensions["x-snyk-api-lifecycle"], qt.Not(qt.IsNil))
97103

104+
// Remove using regex patterns
105+
// - "x-snyk-api-r.*" matches "x-snyk-api-resource"
106+
// - "snyk-version-.*" matches "snyk-version-served" header
107+
// - "x-private-.*" matches "x-private-matter"
108+
// - "x-snyk-api-lifecycle" for doc-level extension
98109
err = vervet.RemoveElements(doc.T, vervet.ExcludePatterns{
99-
ExtensionPatterns: []string{"x-snyk-api-r.*"},
110+
ExtensionPatterns: []string{"x-snyk-api-r.*", "x-snyk-api-lifecycle"},
100111
HeaderPatterns: []string{"snyk-version-.*", "x-private-.*"},
101112
})
102113
c.Assert(err, qt.IsNil)
@@ -115,8 +126,11 @@ func TestRemoveElementsRegex(t *testing.T) {
115126
doc.Paths.Value("/orgs/{org_id}/projects/{project_id}").Delete.Parameters,
116127
qt.HasLen,
117128
3,
118-
) // x-private-matter removed
129+
)
130+
131+
// PATH-LEVEL EXTENSION "x-snyk-api-resource" removed
132+
c.Assert(doc.Paths.Value("/orgs/{orgId}/projects").Extensions["x-snyk-api-resource"], qt.IsNil)
119133

120-
c.Assert(doc.Paths.Value("/orgs/{orgId}/projects").Extensions["x-snyk-api-resource"], qt.IsNil) // now removed
121-
c.Assert(doc.Extensions["x-snyk-api-lifecycle"], qt.Not(qt.IsNil)) // still there
134+
// DOC-LEVEL EXTENSION "x-snyk-api-lifecycle" removed
135+
c.Assert(doc.Extensions["x-snyk-api-lifecycle"], qt.IsNil)
122136
}

0 commit comments

Comments
 (0)