-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: fixes the filtering logic #110
Changes from 5 commits
5d2c41e
4f1abbc
54a93c5
34fb778
02ea645
ee76923
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,6 +30,13 @@ type HiddenEnvsFilter struct { | |
metadata *Metadata | ||
} | ||
|
||
func InitHiddenEnvsFilter(oas *openapi3.T, metadata *Metadata) *HiddenEnvsFilter { | ||
andreaangiolillo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return &HiddenEnvsFilter{ | ||
oas: oas, | ||
metadata: metadata, | ||
} | ||
} | ||
|
||
func (f *HiddenEnvsFilter) Apply() error { | ||
// delete hidden paths first before processing | ||
for pathName, pathItem := range f.oas.Paths.Map() { | ||
|
@@ -91,8 +98,15 @@ func (f *HiddenEnvsFilter) removeRequestBodyIfHiddenForEnv(operation *openapi3.O | |
return | ||
} | ||
|
||
for _, contentType := range operation.RequestBody.Value.Content { | ||
f.removeContentIfHiddenForEnv(contentType) | ||
for k, contentType := range operation.RequestBody.Value.Content { | ||
if isContentTypeHiddenForEnv := isContentTypeHiddenForEnv(contentType, f.metadata.targetEnv); isContentTypeHiddenForEnv { | ||
log.Printf("Removing contentType: %q because is hidden for target env: %q", contentType.Schema.Ref, f.metadata.targetEnv) | ||
// Remove ContentType if it is hidden for the target environment | ||
delete(operation.RequestBody.Value.Content, k) | ||
} else if contentType.Extensions != nil { | ||
// Remove the Hidden extension from the final OAS | ||
delete(contentType.Extensions, hiddenEnvsExtension) | ||
} | ||
} | ||
} | ||
|
||
|
@@ -109,19 +123,17 @@ func (f *HiddenEnvsFilter) removeResponseIfHiddenForEnv(operation *openapi3.Oper | |
if response.Value == nil || response.Value.Content == nil { | ||
continue | ||
} | ||
for _, contentType := range response.Value.Content { | ||
f.removeContentIfHiddenForEnv(contentType) | ||
} | ||
} | ||
} | ||
|
||
func (f *HiddenEnvsFilter) removeContentIfHiddenForEnv(contentType *openapi3.MediaType) { | ||
if isContentTypeHiddenForEnv := isContentTypeHiddenForEnv(contentType, f.metadata.targetEnv); isContentTypeHiddenForEnv { | ||
log.Printf("Removing contentType: %q because is hidden for target env: %q", contentType.Schema.Ref, f.metadata.targetEnv) | ||
contentType.Schema = nil // Remove ContentType if it is hidden for the target environment | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. but why are we breaking the patterns you stablished in this filter with the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The main reason was that we need to pass the |
||
} else if contentType.Extensions != nil { | ||
// Remove the Hidden extension from the final OAS | ||
delete(contentType.Extensions, hiddenEnvsExtension) | ||
for k, contentType := range response.Value.Content { | ||
if isContentTypeHiddenForEnv := isContentTypeHiddenForEnv(contentType, f.metadata.targetEnv); isContentTypeHiddenForEnv { | ||
log.Printf("Removing contentType: %q because is hidden for target env: %q", contentType.Schema.Ref, f.metadata.targetEnv) | ||
// Remove ContentType if it is hidden for the target environment | ||
delete(response.Value.Content, k) | ||
} else if contentType.Extensions != nil { | ||
// Remove the Hidden extension from the final OAS | ||
delete(contentType.Extensions, hiddenEnvsExtension) | ||
} | ||
} | ||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nit] I think naming this
WithInit
leaks filter.go logic that is not necessary. Would make more sense to call this ApplyFilter and pass the filter type, maybe? and do the init logic here instead of outsideThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do you think the problem is the name and/or the fact that we are passing a function returning an array of filters?
I can call the function
newFilters
if we thinkinit
is the problemThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated. Let me know what you think