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

fix(detectors): handle arrays when checking for openapi and swagger #87

Merged
merged 1 commit into from
Nov 1, 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
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
}
]