Skip to content

Commit

Permalink
fix(detectors): handle arrays when checking for openapi and swagger (#87
Browse files Browse the repository at this point in the history
)

fix: handle arrays when checking for openapi and swagger
  • Loading branch information
elsapet authored Nov 1, 2022
1 parent 3d6b474 commit cba816b
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 0 deletions.
1 change: 1 addition & 0 deletions pkg/detectors/openapi/.snapshots/TestOtherjson
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
([]*detections.Detection) <nil>
9 changes: 9 additions & 0 deletions pkg/detectors/openapi/openapi.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package openapi

import (
"bytes"
"encoding/json"
"io/ioutil"

Expand Down Expand Up @@ -73,6 +74,10 @@ func getFileType(file *file.FileInfo) (detectors.OpenAPIFileType, error) {

var version version
if ext == ".json" {
if isArray(input) { // fallback to json|yaml detector
return "", nil
}

err := json.Unmarshal(input, &version)
if err != nil {
return detectors.OpenAPIFileType(""), err
Expand Down Expand Up @@ -109,3 +114,7 @@ func getFileType(file *file.FileInfo) (detectors.OpenAPIFileType, error) {
}

}

func isArray(input []byte) bool {
return bytes.HasPrefix(bytes.TrimSpace(input), []byte("["))
}
6 changes: 6 additions & 0 deletions pkg/detectors/openapi/openapi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,9 @@ func TestDetectorV2yaml(t *testing.T) {

cupaloy.SnapshotT(t, report.Detections)
}

func TestOtherjson(t *testing.T) {
report := testhelper.Extract(t, filepath.Join("testdata", "arrayjson"), registrations, detectorType)

cupaloy.SnapshotT(t, report.Detections)
}
14 changes: 14 additions & 0 deletions pkg/detectors/openapi/testdata/arrayjson/packages.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[
{
"name": "actioncable",
"package_type": "rubygems",
"fallback_state": "reviewed",
"fallback_detected_data_store_technology": null
},
{
"name": "actionmailbox",
"package_type": "rubygems",
"fallback_state": "reviewed",
"fallback_detected_data_store_technology": null
}
]

0 comments on commit cba816b

Please sign in to comment.