TestLoadParseErrorIsWarning confirms a malformed spec produces a warning rather than aborting load — other specs should still be processed.
(t *testing.T)
| 258 | // warning rather than aborting load — other specs should still be |
| 259 | // processed. |
| 260 | func TestLoadParseErrorIsWarning(t *testing.T) { |
| 261 | dir := t.TempDir() |
| 262 | if err := os.WriteFile(filepath.Join(dir, "bad.yaml"), []byte("not: { valid: openapi"), 0o644); err != nil { |
| 263 | t.Fatal(err) |
| 264 | } |
| 265 | if err := os.WriteFile(filepath.Join(dir, "good.yaml"), []byte(` |
| 266 | openapi: 3.0.0 |
| 267 | info: { title: ok, version: '1' } |
| 268 | paths: |
| 269 | /things: |
| 270 | get: |
| 271 | operationId: listThings |
| 272 | responses: |
| 273 | '200': |
| 274 | description: ok |
| 275 | content: { application/json: { schema: { type: object } } } |
| 276 | `), 0o644); err != nil { |
| 277 | t.Fatal(err) |
| 278 | } |
| 279 | |
| 280 | res, err := Load(LoaderOptions{SpecsDir: dir}, nil, nil) |
| 281 | if err != nil { |
| 282 | t.Fatal(err) |
| 283 | } |
| 284 | // Bad spec produces a warning; good spec loads successfully. |
| 285 | if len(res.Registry.Specs) != 1 { |
| 286 | t.Errorf("expected 1 spec loaded, got %d", len(res.Registry.Specs)) |
| 287 | } |
| 288 | var sawBadWarning bool |
| 289 | for _, w := range res.Warnings { |
| 290 | if contains(w, "bad.yaml") { |
| 291 | sawBadWarning = true |
| 292 | } |
| 293 | } |
| 294 | if !sawBadWarning { |
| 295 | t.Errorf("expected warning mentioning bad.yaml, got %v", res.Warnings) |
| 296 | } |
| 297 | } |