Skip to content

Commit

Permalink
[Bugfix] fixed error message when only file is referenced (#348)
Browse files Browse the repository at this point in the history
without internal reference
  • Loading branch information
steffakasid authored Apr 23, 2021
1 parent a4e36cd commit 1b47cce
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 4 deletions.
2 changes: 1 addition & 1 deletion openapi3/swagger_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func (swaggerLoader *SwaggerLoader) readURL(location *url.URL) ([]byte, error) {
}
defer resp.Body.Close()
if resp.StatusCode > 399 {
return nil, fmt.Errorf("request returned status code %d", resp.StatusCode)
return nil, fmt.Errorf("error loading %q: request returned status code %d", location.String(), resp.StatusCode)
}
return ioutil.ReadAll(resp.Body)
}
Expand Down
50 changes: 47 additions & 3 deletions openapi3/swagger_loader_http_error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/stretchr/testify/require"
)

func TestLoadFromRemoteURLFailsWithHttpError(t *testing.T) {
func TestLoadReferenceFromRemoteURLFailsWithHttpError(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprint(w, "")
Expand Down Expand Up @@ -47,9 +47,53 @@ func TestLoadFromRemoteURLFailsWithHttpError(t *testing.T) {
swagger, err := loader.LoadSwaggerFromDataWithPath(spec, &url.URL{Path: "testdata/testfilename.openapi.json"})

require.Nil(t, swagger)
require.EqualError(t, err, fmt.Sprintf("error resolving reference \"%s/components.openapi.json#/components/headers/CustomTestHeader\": request returned status code 400", ts.URL))
require.EqualError(t, err, fmt.Sprintf("error resolving reference \"%s/components.openapi.json#/components/headers/CustomTestHeader\": error loading \"%s/components.openapi.json\": request returned status code 400", ts.URL, ts.URL))

swagger, err = loader.LoadSwaggerFromData(spec)
require.Nil(t, swagger)
require.EqualError(t, err, fmt.Sprintf("error resolving reference \"%s/components.openapi.json#/components/headers/CustomTestHeader\": error loading \"%s/components.openapi.json\": request returned status code 400", ts.URL, ts.URL))
}

func TestLoadFromRemoteURLFailsWithHttpError(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprint(w, "")
}))
defer ts.Close()

spec := []byte(`
{
"openapi": "3.0.0",
"info": {
"title": "",
"version": "1"
},
"paths": {
"/test": {
"post": {
"responses": {
"default": {
"description": "test",
"headers": {
"X-TEST-HEADER": {
"$ref": "` + ts.URL + `/components.openapi.json"
}
}
}
}
}
}
}
}`)

loader := NewSwaggerLoader()
loader.IsExternalRefsAllowed = true
swagger, err := loader.LoadSwaggerFromDataWithPath(spec, &url.URL{Path: "testdata/testfilename.openapi.json"})

require.Nil(t, swagger)
require.EqualError(t, err, fmt.Sprintf("error loading \"%s/components.openapi.json\": request returned status code 400", ts.URL))

swagger, err = loader.LoadSwaggerFromData(spec)
require.Nil(t, swagger)
require.EqualError(t, err, fmt.Sprintf("error resolving reference \"%s/components.openapi.json#/components/headers/CustomTestHeader\": request returned status code 400", ts.URL))
require.EqualError(t, err, fmt.Sprintf("error loading \"%s/components.openapi.json\": request returned status code 400", ts.URL))
}

0 comments on commit 1b47cce

Please sign in to comment.