From de94f282c332330fab6c83aab0f1ae79d630368f Mon Sep 17 00:00:00 2001 From: Grant Linville Date: Mon, 24 Jun 2024 16:16:58 -0400 Subject: [PATCH 1/3] fix: get remote file before appending default file names Signed-off-by: Grant Linville --- pkg/loader/url.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/pkg/loader/url.go b/pkg/loader/url.go index 95249478..155a3d80 100644 --- a/pkg/loader/url.go +++ b/pkg/loader/url.go @@ -133,6 +133,19 @@ func loadURL(ctx context.Context, cache *cache.Client, base *source, name string func getWithDefaults(req *http.Request) ([]byte, error) { originalPath := req.URL.Path + + // First, try to get the original path as is. + // It might be a raw tool file or an OpenAPI definition. + resp, err := http.DefaultClient.Do(req) + if err != nil { + return nil, err + } + defer resp.Body.Close() + + if resp.StatusCode == http.StatusOK { + return io.ReadAll(resp.Body) + } + for i, def := range types.DefaultFiles { base := path.Base(originalPath) if !strings.Contains(base, ".") { From 94ab8c28ee18d0448c46838a88255445cbd66e4c Mon Sep 17 00:00:00 2001 From: Grant Linville Date: Mon, 24 Jun 2024 16:24:15 -0400 Subject: [PATCH 2/3] improve Signed-off-by: Grant Linville --- pkg/loader/url.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/pkg/loader/url.go b/pkg/loader/url.go index 155a3d80..bbeb57ce 100644 --- a/pkg/loader/url.go +++ b/pkg/loader/url.go @@ -135,7 +135,7 @@ func getWithDefaults(req *http.Request) ([]byte, error) { originalPath := req.URL.Path // First, try to get the original path as is. - // It might be a raw tool file or an OpenAPI definition. + // It might be an OpenAPI definition. resp, err := http.DefaultClient.Do(req) if err != nil { return nil, err @@ -143,7 +143,9 @@ func getWithDefaults(req *http.Request) ([]byte, error) { defer resp.Body.Close() if resp.StatusCode == http.StatusOK { - return io.ReadAll(resp.Body) + if toolBytes, err := io.ReadAll(resp.Body); err == nil && isOpenAPI(toolBytes) != 0 { + return toolBytes, nil + } } for i, def := range types.DefaultFiles { From 95a44a5d291f012b2c8bcb4b49b8b9f14c780f33 Mon Sep 17 00:00:00 2001 From: Grant Linville Date: Mon, 24 Jun 2024 16:24:39 -0400 Subject: [PATCH 3/3] fix comment Signed-off-by: Grant Linville --- pkg/loader/url.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pkg/loader/url.go b/pkg/loader/url.go index bbeb57ce..c050edba 100644 --- a/pkg/loader/url.go +++ b/pkg/loader/url.go @@ -134,8 +134,7 @@ func loadURL(ctx context.Context, cache *cache.Client, base *source, name string func getWithDefaults(req *http.Request) ([]byte, error) { originalPath := req.URL.Path - // First, try to get the original path as is. - // It might be an OpenAPI definition. + // First, try to get the original path as is. It might be an OpenAPI definition. resp, err := http.DefaultClient.Do(req) if err != nil { return nil, err