Skip to content

Commit

Permalink
importer-rest-api-specs: fix format string, ensure model names are UR…
Browse files Browse the repository at this point in the history
…L-decoded when parsing from the Ref URL fragment
  • Loading branch information
manicminer committed Oct 1, 2024
1 parent f1a9aad commit 23eccdf
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package comparison
import (
"fmt"
"strings"

"github.com/hashicorp/go-azure-helpers/lang/pointer"
sdkModels "github.com/hashicorp/pandora/tools/data-api-sdk/v1/models"
"github.com/hashicorp/pandora/tools/importer-rest-api-specs/internal/logging"
Expand Down Expand Up @@ -32,8 +32,16 @@ func ObjectDefinitionsMatch(first, second *sdkModels.SDKObjectDefinition) (bool,
return false, fmt.Errorf("ReferenceName differed - %q vs %q", pointer.From(first.ReferenceName), pointer.From(second.ReferenceName))
}
if first.ReferenceNameIsCommonType != second.ReferenceNameIsCommonType {
logging.Tracef("ReferenceNameIsCommonType differed - %t vs %t", first.ReferenceNameIsCommonType, second.ReferenceNameIsCommonType)
return false, fmt.Errorf("ReferenceNameIsCommonType differed - %t vs %t", first.ReferenceNameIsCommonType, second.ReferenceNameIsCommonType)
firstValue := "nil"
secondValue := "nil"
if first.ReferenceNameIsCommonType != nil {
firstValue = fmt.Sprintf("%t", *first.ReferenceNameIsCommonType)
}
if second.ReferenceNameIsCommonType != nil {
secondValue = fmt.Sprintf("%t", *second.ReferenceNameIsCommonType)
}
logging.Tracef("ReferenceNameIsCommonType differed - %s vs %s", firstValue, secondValue)
return false, fmt.Errorf("ReferenceNameIsCommonType differed - %s vs %s", firstValue, secondValue)
}
if first.NestedItem != nil && second.NestedItem != nil {
if ok, err := ObjectDefinitionsMatch(first.NestedItem, second.NestedItem); !ok {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package parsingcontext

import (
"fmt"
"net/url"
"path/filepath"
"strings"

Expand Down Expand Up @@ -31,7 +32,7 @@ import (
// are mangled and thus fail to be ingested. To prevent accidental clobbering of models/constants in external swagger
// files that have the same name, we specifically only compile the models/constants that are being remotely referenced,
// as well as any references they may have to ensure no reference remains unresolved. To trick the `analysis.Flatten`
// function, we strip out the paths in remove references after resolving them, to make it appear like everything was
// function, we strip out the paths in remote references after resolving them, to make it appear like everything was
// loaded from the original swagger file.
func BuildFromFile(filePath string) (*Context, error) {

Expand Down Expand Up @@ -59,7 +60,6 @@ func BuildFromFile(filePath string) (*Context, error) {
Verbose: true,
Expand: false,
RemoveUnused: false,
//ContinueOnError: true,

BasePath: basePath,
Spec: analysis.New(swaggerDocWithReferences.Spec()),
Expand Down Expand Up @@ -92,7 +92,6 @@ func BuildFromFile(filePath string) (*Context, error) {
Verbose: true,
Expand: false,
RemoveUnused: false,
//ContinueOnError: true,

BasePath: basePath,
Spec: analysis.New(expandedSwaggerDoc.Spec()),
Expand Down Expand Up @@ -322,9 +321,13 @@ func modelNamePathFromRef(ref spec.Ref, sourcePath, topLevelDir string) (modelNa
modelFilePath = filepath.Join(sourceDir, modelFilePath)
}

// get the modelName from the last URL fragment
// get the modelName from the last slug in the URL fragment
fragments := strings.Split(refUrl.Fragment, "/")
modelName = fragments[len(fragments)-1]

// url-decode the model name, as it comes from the fragment which might contain urlencoded characters like `[` or `]`
// ignoring any errors here, since this function will err if invalid escape sequences are present
modelName, _ = url.QueryUnescape(modelName)

return
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package parsingcontext

import (
"fmt"
"net/url"
"strings"

"github.com/go-openapi/spec"
Expand All @@ -22,6 +23,10 @@ func fragmentNameFromString(fragmentName string) *string {
if fragmentName != "" {
fragments := strings.Split(fragmentName, "/") // format #/definitions/ConfigurationStoreListResult
referenceName := fragments[len(fragments)-1]

// url-decode the referenceName, as it comes from the fragment which might contain urlencoded characters like
// `[` or `]` - ignoring any errors here, since this function will err if invalid escape sequences are present
referenceName, _ = url.QueryUnescape(referenceName)
return &referenceName
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func TestCanParseFilesForAllServicesFromConfig(t *testing.T) {

parsedService, err := apidefinitions.ParseService(*availableDataSet)
if err != nil {
t.Errorf("parsing Data for Service %q: %+v", service.Name, err)
t.Fatalf("parsing Data for Service %q: %+v", service.Name, err)
}

t.Logf("Service %q contains %d API Versions", service.Name, len(availableDataSet.DataSetsForAPIVersions))
Expand Down

0 comments on commit 23eccdf

Please sign in to comment.