Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion tools/cli/internal/openapi/filter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ The Atlas Admin API OpenAPI specifications are used not only to document REST en
- Filtering per version, so that only the endpoints that are available in that version are shown.
## What filters are available?
### List of filters
[ExtensionFilter is a filter that updates the x-sunset and x-xgen-version extensions to a date string](../internal/openapi/filter/extension.go?plain=1#L24)
[ExtensionFilter is a filter that deletes the x-xgen-ipa-exception extensions, updates the x-sunset and x-xgen-version](../internal/openapi/filter/extension.go?plain=1#L24)
[HiddenEnvsFilter is a filter that removes paths, operations,](../internal/openapi/filter/hidden_envs.go?plain=1#L28)
[InfoFilter is a filter that modifies the Info object in the OpenAPI spec.](../internal/openapi/filter/info.go?plain=1#L23)
[OperationsFilter is a filter that removes the x-xgen-owner-team extension from operations](../internal/openapi/filter/operations.go?plain=1#L20)
Expand Down
120 changes: 117 additions & 3 deletions tools/cli/internal/openapi/filter/extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@ type ExtensionFilter struct {
}

const (
sunsetExtension = "x-sunset"
xGenExtension = "x-xgen-version"
format = "2006-01-02T15:04:05Z07:00"
sunsetExtension = "x-sunset"
xGenExtension = "x-xgen-version"
ipaExceptionExtension = "x-xgen-IPA-exception"
format = "2006-01-02T15:04:05Z07:00"
)

func (f *ExtensionFilter) Apply() error {
Expand All @@ -42,27 +43,38 @@ func (f *ExtensionFilter) Apply() error {
continue
}
updateExtensionToDateString(pathItem.Extensions)
deleteIpaExceptionExtension(pathItem.Extensions)

for _, operation := range pathItem.Operations() {
if operation == nil {
continue
}

updateExtensionToDateString(operation.Extensions)
deleteIpaExceptionExtension(operation.Extensions)

if operation.Parameters != nil {
updateExtensionsForOperationParameters(operation.Parameters)
}

updateExtensionsForRequestBody(operation.RequestBody)

latestVersionMatch := apiversion.FindLatestContentVersionMatched(operation, f.metadata.targetVersion)

for _, response := range operation.Responses.Map() {
if response == nil {
continue
}

updateExtensionToDateString(response.Extensions)
deleteIpaExceptionExtension(response.Extensions)

if response.Value == nil {
continue
}

updateExtensionToDateString(response.Value.Extensions)
deleteIpaExceptionExtension(response.Value.Extensions)

if response.Value.Content == nil {
continue
Expand All @@ -80,9 +92,111 @@ func (f *ExtensionFilter) Apply() error {
f.deleteSunsetIfDeprecatedByHiddenVersions(latestVersionMatch, request.Value.Content)
}
}
if f.oas.Tags != nil {
updateExtensionsForTags(&f.oas.Tags)
}
if f.oas.Components != nil {
updateExtensionsForComponents(f.oas.Components)
}
return nil
}

func updateExtensionsForRequestBody(requestBody *openapi3.RequestBodyRef) {
if requestBody == nil {
return
}
deleteIpaExceptionExtension(requestBody.Extensions)
_, contentsInVersion := getVersionsInContentType(requestBody.Value.Content)
for _, content := range contentsInVersion {
deleteIpaExceptionExtension(content.Extensions)
updateExtensionsForSchema(content.Schema)
}
}

func updateExtensionsForOperationParameters(parameters openapi3.Parameters) {
for _, parameter := range parameters {
if parameter.Value == nil || parameter.Value.Schema == nil {
continue
}
deleteIpaExceptionExtension(parameter.Value.Schema.Extensions)
if parameter.Value.Schema.Value == nil {
continue
}
deleteIpaExceptionExtension(parameter.Value.Schema.Value.Extensions)
}
}

func updateExtensionsForComponents(components *openapi3.Components) {
for _, schema := range components.Schemas {
updateExtensionsForSchema(schema)
}
for _, parameter := range components.Parameters {
if parameter != nil {
deleteIpaExceptionExtension(parameter.Extensions)
}
}
}

func updateExtensionsForTags(tags *openapi3.Tags) {
for _, tag := range *tags {
if tag != nil {
deleteIpaExceptionExtension(tag.Extensions)
}
}
}

func updateExtensionsForSchema(schema *openapi3.SchemaRef) {
if schema != nil {
deleteIpaExceptionExtension(schema.Extensions)
}
if schema.Value != nil {
deleteIpaExceptionExtension(schema.Value.Extensions)
for _, allOf := range schema.Value.AllOf {
if allOf.Value == nil {
continue
}
for _, property := range allOf.Value.Properties {
if property.Value != nil {
deleteIpaExceptionExtension(property.Value.Extensions)
}
}
}
for _, anyOf := range schema.Value.AnyOf {
if anyOf.Value == nil {
continue
}
for _, property := range anyOf.Value.Properties {
if property.Value != nil {
deleteIpaExceptionExtension(property.Value.Extensions)
}
}
}
for _, oneOf := range schema.Value.OneOf {
if oneOf.Value == nil {
continue
}
for _, property := range oneOf.Value.Properties {
if property.Value != nil {
deleteIpaExceptionExtension(property.Value.Extensions)
}
}
}
for _, property := range schema.Value.Properties {
if property.Value != nil {
deleteIpaExceptionExtension(property.Value.Extensions)
}
}
}
}

func deleteIpaExceptionExtension(extensions map[string]any) {
if extensions == nil || extensions[ipaExceptionExtension] == nil {
return
}

delete(extensions, ipaExceptionExtension)
}

func updateExtensionToDateString(extensions map[string]any) {
if extensions == nil {
return
Expand Down
Loading
Loading