Skip to content

Commit 8261a1c

Browse files
committed
Fixed bug in response handling when a definition file initiates a redirect
1 parent 17743c8 commit 8261a1c

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

cmd/requests.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,13 @@ func MakeRequest(client http.Client, method, target string, timeout int64, reqDa
152152

153153
bodyBytes, _ := io.ReadAll(resp.Body)
154154
bodyString := string(bodyBytes)
155+
156+
if (resp.StatusCode == 301 || resp.StatusCode == 302) && strings.Contains(bodyString, "<html>") {
157+
redirect, _ := resp.Location()
158+
bodyBytes, bodyString, requestStatus = MakeRequest(client, method, redirect.Scheme+"://"+redirect.Host+redirect.Path, timeout, reqData)
159+
return bodyBytes, bodyString, requestStatus
160+
}
161+
155162
requestStatus = resp.StatusCode
156163

157164
return bodyBytes, bodyString, requestStatus

cmd/root.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ $ sj brute -u https://petstore.swagger.io`,
4646
log.Error("Command not specified. See the --help flag for usage.")
4747
}
4848
},
49-
Version: "1.5.0",
49+
Version: "1.5.1",
5050
}
5151

5252
func Execute() {

cmd/utils.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -469,8 +469,14 @@ func UnmarshalSpec(bodyBytes []byte) (newDoc *openapi3.T) {
469469
_ = yaml.Unmarshal(bodyBytes, &doc)
470470
_ = yaml.Unmarshal(bodyBytes, &doc3)
471471
}
472-
_ = json.Unmarshal(bodyBytes, &doc)
473-
_ = json.Unmarshal(bodyBytes, &doc3)
472+
err := json.Unmarshal(bodyBytes, &doc)
473+
if err != nil {
474+
err = json.Unmarshal(bodyBytes, &doc3)
475+
if err != nil {
476+
log.Fatalf("Error unmarshalling API definitions: %s\n", err)
477+
}
478+
}
479+
err = json.Unmarshal(bodyBytes, &doc3)
474480

475481
if strings.HasPrefix(doc3.OpenAPI, "3") {
476482
newDoc := &doc3
@@ -485,7 +491,7 @@ func UnmarshalSpec(bodyBytes []byte) (newDoc *openapi3.T) {
485491
var noDoc openapi3.T
486492
return &noDoc
487493
} else {
488-
log.Fatal("Error parsing definition file.\n")
494+
log.Fatalf("Error parsing definition file: %s\n", doc3.OpenAPI)
489495
return nil
490496
}
491497
}

0 commit comments

Comments
 (0)