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

Add mixins to catch models in local files #1138

Merged
merged 1 commit into from
Jul 15, 2022
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
46 changes: 46 additions & 0 deletions tools/importer-rest-api-specs/parser/flattener.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/go-openapi/analysis"
"github.com/go-openapi/loads"
"github.com/go-openapi/spec"
)

// load loads the swagger document and flattens it to ensure this contains
Expand All @@ -23,6 +24,10 @@ func load(directory string, fileName string, debugLogging bool) (*SwaggerDefinit
if err != nil {
return nil, fmt.Errorf("loading swagger file %q: %+v", filePath, err)
}
swaggerDocWithReferences, err = findAndMergeLocalMixins(swaggerDocWithReferences, directory, fileName)
if err != nil {
return nil, fmt.Errorf("could not mixin remote swagger files referenced by %q: %+v", filePath, err)
}
flattenedWithReferencesOpts := &analysis.FlattenOpts{
Minimal: true,
Verbose: true,
Expand All @@ -44,6 +49,11 @@ func load(directory string, fileName string, debugLogging bool) (*SwaggerDefinit
if err != nil {
return nil, fmt.Errorf("loading swagger file %q: %+v", filePath, err)
}
expandedSwaggerDoc, err = findAndMergeLocalMixins(expandedSwaggerDoc, directory, fileName)
if err != nil {
return nil, fmt.Errorf("could not mixin remote swagger files referenced by %q: %+v", filePath, err)
}

flattenedExpandedOpts := &analysis.FlattenOpts{
Minimal: false,
Verbose: true,
Expand Down Expand Up @@ -72,3 +82,39 @@ func load(directory string, fileName string, debugLogging bool) (*SwaggerDefinit
swaggerSpecExtendedRaw: swaggerSpecExpandedRaw,
}, nil
}

func findAndMergeLocalMixins(input *loads.Document, basePath string, baseFile string) (*loads.Document, error) {
if len(strings.Split(baseFile, "/")) != 2 { // We only care about local files, not sub-folders
return input, nil
}
pathsToMixin := make([]string, 0)
if input.Analyzer != nil {
allRefs := input.Analyzer.AllRefs()
for _, v := range allRefs {
if path := v.Ref.GetURL(); path != nil && path.Path != "" && len(strings.Split(path.Path, "/")) == 2 { // Check if we have a reference in the CWD.
pathsToMixin = append(pathsToMixin, path.Path)
}
}
}

if len(pathsToMixin) > 0 {
uniqueFilter := make(map[string]bool)
uniquePaths := make([]string, 0)
for _, path := range pathsToMixin {
if _, ok := uniqueFilter[path]; !ok {
uniqueFilter[path] = true
uniquePaths = append(uniquePaths, path)
}
}
mixins := make([]*spec.Swagger, 0)
for _, v := range uniquePaths {
doc, err := loads.Spec(fmt.Sprintf("%s/%s", basePath, v))
if err != nil {
return nil, fmt.Errorf("could not load remote ref %q for mixin in %q: %+v", v, baseFile, err)
}
mixins = append(mixins, doc.Spec())
}
_ = analysis.Mixin(input.Spec(), mixins...)
}
return input, nil
}
4 changes: 2 additions & 2 deletions tools/importer-rest-api-specs/parser/swagger_resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ func (d *SwaggerDefinition) findModelNamesWhichImplement(parentName string) []st
modelNames := make([]string, 0)

for childName, value := range d.swaggerSpecExtendedRaw.Definitions {
if childName == parentName {
if strings.EqualFold(childName, parentName) {
continue
}

Expand All @@ -448,7 +448,7 @@ func (d *SwaggerDefinition) findModelNamesWhichImplement(parentName string) []st
continue
}

if *fragmentName == parentName {
if strings.EqualFold(*fragmentName, parentName) {
implementsParent = true
break
}
Expand Down