Skip to content

Commit 2676b35

Browse files
bug: fix relative references when defaulting files from dirs
1 parent 75bd379 commit 2676b35

File tree

1 file changed

+25
-13
lines changed

1 file changed

+25
-13
lines changed

pkg/loader/url.go

+25-13
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,20 @@ func loadURL(ctx context.Context, cache *cache.Client, base *source, name string
111111
req.Header.Set("Authorization", "Bearer "+bearerToken)
112112
}
113113

114-
data, err := getWithDefaults(req)
114+
data, defaulted, err := getWithDefaults(req)
115115
if err != nil {
116116
return nil, false, fmt.Errorf("error loading %s: %v", url, err)
117117
}
118118

119+
if defaulted != "" {
120+
pathString = url
121+
name = defaulted
122+
if repo != nil {
123+
repo.Path = path.Join(repo.Path, repo.Name)
124+
repo.Name = defaulted
125+
}
126+
}
127+
119128
log.Debugf("opened %s", url)
120129

121130
result := &source{
@@ -137,31 +146,32 @@ func loadURL(ctx context.Context, cache *cache.Client, base *source, name string
137146
return result, true, nil
138147
}
139148

140-
func getWithDefaults(req *http.Request) ([]byte, error) {
149+
func getWithDefaults(req *http.Request) ([]byte, string, error) {
141150
originalPath := req.URL.Path
142151

143152
// First, try to get the original path as is. It might be an OpenAPI definition.
144153
resp, err := http.DefaultClient.Do(req)
145154
if err != nil {
146-
return nil, err
155+
return nil, "", err
147156
}
148157
defer resp.Body.Close()
149158

150159
if resp.StatusCode == http.StatusOK {
151-
if toolBytes, err := io.ReadAll(resp.Body); err == nil && isOpenAPI(toolBytes) != 0 {
152-
return toolBytes, nil
153-
}
160+
toolBytes, err := io.ReadAll(resp.Body)
161+
return toolBytes, "", err
162+
}
163+
164+
base := path.Base(originalPath)
165+
if strings.Contains(base, ".") {
166+
return nil, "", fmt.Errorf("error loading %s: %s", req.URL.String(), resp.Status)
154167
}
155168

156169
for i, def := range types.DefaultFiles {
157-
base := path.Base(originalPath)
158-
if !strings.Contains(base, ".") {
159-
req.URL.Path = path.Join(originalPath, def)
160-
}
170+
req.URL.Path = path.Join(originalPath, def)
161171

162172
resp, err := http.DefaultClient.Do(req)
163173
if err != nil {
164-
return nil, err
174+
return nil, "", err
165175
}
166176
defer resp.Body.Close()
167177

@@ -170,11 +180,13 @@ func getWithDefaults(req *http.Request) ([]byte, error) {
170180
}
171181

172182
if resp.StatusCode != http.StatusOK {
173-
return nil, fmt.Errorf("error loading %s: %s", req.URL.String(), resp.Status)
183+
return nil, "", fmt.Errorf("error loading %s: %s", req.URL.String(), resp.Status)
174184
}
175185

176-
return io.ReadAll(resp.Body)
186+
data, err := io.ReadAll(resp.Body)
187+
return data, def, err
177188
}
189+
178190
panic("unreachable")
179191
}
180192

0 commit comments

Comments
 (0)