Skip to content
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

kie-issues#2718: [kn-plugin-workflow] Minify the openAPI spec files to trim components/schemas #2749

Merged
merged 2 commits into from
Jan 8, 2025
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
78 changes: 63 additions & 15 deletions packages/kn-plugin-workflow/pkg/specs/openapi_minifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"os"
"path"
"path/filepath"
"reflect"
"strings"

"github.com/apache/incubator-kie-tools/packages/kn-plugin-workflow/pkg/common"
Expand Down Expand Up @@ -158,22 +159,9 @@ func (m *OpenApiMinifier) minifySpecsFile(specFileName string, operations sets.S
return "", fmt.Errorf("❌ ERROR: failed to read OpenAPI document: %w", err)
}

doc, err := openapi3.NewLoader().LoadFromData(data)
doc, err := m.removeUnusedNodes(data, specFile, operations)
if err != nil {
return "", fmt.Errorf("❌ ERROR: failed to load OpenAPI document: %w", err)
}
if doc.Paths == nil {
return "", fmt.Errorf("OpenAPI document %s has no paths", specFileName)
}
for key, value := range doc.Paths.Map() {
for method, operation := range value.Operations() {
if !operations.Has(operation.OperationID) {
value.SetOperation(method, nil)
}
}
if isPathItemEmpty(value) {
doc.Paths.Delete(key)
}
return "", err
}

minifiedFile, err := m.writeMinifiedFileToDisk(specFile, doc)
Expand All @@ -194,6 +182,66 @@ func (m *OpenApiMinifier) minifySpecsFile(specFileName string, operations sets.S
return minifiedFile, nil
}

func (m *OpenApiMinifier) removeUnusedNodes(data []byte, specFileName string, operations sets.Set[string]) (*openapi3.T, error) {
doc, err := openapi3.NewLoader().LoadFromData(data)

collector, err := newCollector(specFileName)
if err != nil {
return nil, err
}

keep, err := collector.collect(operations)
if err != nil {
return nil, err
}

if err != nil {
return nil, fmt.Errorf("❌ ERROR: failed to load OpenAPI document: %w", err)
}
if doc.Paths == nil {
return nil, fmt.Errorf("OpenAPI document %s has no paths", specFileName)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to return an error here? Maybe just log silently to not spur the user's console with an error. If this is handled by the caller, just ignore my comment.

}
for key, value := range doc.Paths.Map() {
for method, operation := range value.Operations() {
if !operations.Has(operation.OperationID) {
value.SetOperation(method, nil)
}
}
if isPathItemEmpty(value) {
doc.Paths.Delete(key)
}
}

if doc.Components != nil {
// note we have to skip securitySchemes, because it aren't referenced by operation directly via $ref
components := map[string]interface{}{
"schemas": doc.Components.Schemas,
"headers": doc.Components.Headers,
"parameters": doc.Components.Parameters,
"responses": doc.Components.Responses,
"requestBodies": doc.Components.RequestBodies,
"examples": doc.Components.Examples,
"links": doc.Components.Links,
"callbacks": doc.Components.Callbacks,
}

for key, componentMap := range components {
if componentMap == nil {
continue
}

componentValue := reflect.ValueOf(componentMap)
for _, name := range componentValue.MapKeys() {
nameStr := name.String()
if !keep["components"][key].Has(nameStr) {
componentValue.SetMapIndex(name, reflect.Value{})
}
}
}
}
return doc, nil
}

func (m *OpenApiMinifier) findWorkflowFile() error {
file, err := common.FindSonataFlowFile(workflowExtensionsType)
if err != nil {
Expand Down
Loading
Loading