Skip to content

Commit 0641013

Browse files
refactor: improve extension pattern filtering architecture (#63)
Co-authored-by: Brian Flad <[email protected]>
1 parent 690d05e commit 0641013

File tree

7 files changed

+548
-86
lines changed

7 files changed

+548
-86
lines changed

openapi/cmd/README.md

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -251,10 +251,17 @@ Create a YAML configuration file to control sanitization behavior:
251251
```yaml
252252
# sanitize-config.yaml
253253

254-
# Only remove extensions that match these patterns, null will remove ALL extensions, [] will remove no extensions (default: null, removes ALL extensions)
254+
# Extension filtering (not provided or empty = remove all extensions by default)
255255
extensionPatterns:
256-
- "x-go-*"
257-
- "x-internal-*"
256+
# Whitelist: keep only matching extensions (when provided, only these are kept)
257+
# Keep takes precedence over Remove when both are specified
258+
keep:
259+
- "x-speakeasy-schema-*" # Example: keep only schema-related extensions
260+
# Blacklist: remove only matching extensions, keep all others
261+
remove:
262+
- "x-go-*"
263+
- "x-internal-*"
264+
- "x-speakeasy-*" # Combined with Keep above, removes all x-speakeasy-* EXCEPT x-speakeasy-schema-*
258265

259266
# Keep unused components (default: false, removes them)
260267
keepUnusedComponents: true
@@ -333,7 +340,7 @@ components:
333340
334341
**After sanitization (with pattern config):**
335342
336-
Using config with `extensionPatterns: ["x-go-*"]`:
343+
Using config with `extensionPatterns: { remove: ["x-go-*"] }`:
337344

338345
```yaml
339346
openapi: 3.1.0

openapi/cmd/sanitize.go

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,17 @@ Benefits of sanitization:
5353
5454
Configuration file format (YAML):
5555
56-
# Only remove extensions that match these patterns, null will remove ALL extensions, [] will remove no extensions (default: null, removes ALL extensions)
56+
# Extension filtering (not provided or empty = remove all extensions by default)
5757
extensionPatterns:
58-
- "x-go-*"
59-
- "x-internal-*"
58+
# Keep only matching extensions (when provided, only these are kept)
59+
# Keep takes precedence over Remove when both are specified
60+
keep:
61+
- "x-speakeasy-schema-*" # Keeps x-speakeasy-schema-*, allows narrowing Remove patterns
62+
# Remove only matching extensions, keep all others
63+
remove:
64+
- "x-go-*"
65+
- "x-internal-*"
66+
- "x-speakeasy-*" # When combined with Keep above, removes all x-speakeasy-* EXCEPT x-speakeasy-schema-*
6067
6168
# Keep unused components (default: false, removes them)
6269
keepUnusedComponents: true
@@ -164,16 +171,28 @@ func reportSanitizationResults(processor *OpenAPIProcessor, opts *openapi.Saniti
164171
var messages []string
165172

166173
// Determine what was done with extensions
167-
switch {
168-
case opts == nil || opts.ExtensionPatterns == nil:
174+
if opts == nil || opts.ExtensionPatterns == nil {
169175
// nil patterns = remove all extensions (default)
170176
messages = append(messages, "removed all extensions")
171-
case len(opts.ExtensionPatterns) == 0:
172-
// empty slice = keep all extensions (explicit)
173-
messages = append(messages, "kept all extensions")
174-
default:
175-
// specific patterns = remove matching extensions
176-
messages = append(messages, fmt.Sprintf("removed extensions matching %v", opts.ExtensionPatterns))
177+
} else {
178+
filter := opts.ExtensionPatterns
179+
hasKeep := len(filter.Keep) > 0
180+
hasRemove := len(filter.Remove) > 0
181+
182+
switch {
183+
case !hasKeep && !hasRemove:
184+
// empty filter = remove all extensions
185+
messages = append(messages, "removed all extensions")
186+
case hasKeep && !hasRemove:
187+
// keep only
188+
messages = append(messages, fmt.Sprintf("kept only extensions matching %v", filter.Keep))
189+
case !hasKeep && hasRemove:
190+
// remove only
191+
messages = append(messages, fmt.Sprintf("removed extensions matching %v", filter.Remove))
192+
case hasKeep && hasRemove:
193+
// both (keep overrides remove)
194+
messages = append(messages, fmt.Sprintf("kept extensions matching %v (remove patterns %v overridden by keep)", filter.Keep, filter.Remove))
195+
}
177196
}
178197

179198
// Determine what was done with components

0 commit comments

Comments
 (0)