feat: add external reference inspection, export ErrExternalRefNotAllowed, and recover linter rule panics

TH
2d ago
+548-6

Summary

  • Export references.ErrExternalRefNotAllowed (references/resolution.go): Replaces the inline errors.New("external reference not allowed") sentinel so callers using ResolveOptions.DisableExternalRefs can match the error via errors.Is.
  • Add zero-I/O external reference inspection on *openapi.OpenAPI (openapi/external_refs.go):
    • FindExternalRefs(ctx) ([]ExternalReference, error) walks modeled OpenAPI referenceable nodes (Walk) without performing filesystem or network I/O and returns every $ref with a non-empty URI alongside a cloned Locations slice. Respects context cancellation cleanly.
    • ValidateNoExternalRefs(ctx) error joins errors wrapping references.ErrExternalRefNotAllowed for each external reference found.
  • Recognize YAML merge keys on Reference Objects (openapi/core/reference.go): Expands << merge keys via yml.ResolveMergeKeys before checking for $ref so merged references on non-schema Reference Objects (e.g. responses, parameters, path items) are unmarshaled as references rather than inline objects.
  • Guard nil elements in walkers (openapi/walk_tags_servers.go, jsonschema/oas3/walk.go): Avoid nil-pointer dereferences when walking programmatically constructed documents containing nil ServerVariable map values or zero-value JSONSchema wrappers.
  • Recover panics in concurrent linter rule goroutines (linter/linter.go): Pre-captures the rule ID before launching each goroutine and converts any recovered panic into a validation.Error rather than terminating the caller process.

Verification

  • mise ci passed locally (format check, go.mod tidy across workspace modules, golangci-lint with 0 issues, nilaway, examples check, unit tests with -race, CLI integration tests, and build).

Summary by cubic

Adds zero-I/O external reference inspection for OpenAPI documents and makes the linter recover from rule panics instead of crashing.

New Features

  • OpenAPI.FindExternalRefs(ctx) walks all modeled references without filesystem or network I/O and returns external $ref URIs with their locations.
  • OpenAPI.ValidateNoExternalRefs(ctx) returns an error wrapping references.ErrExternalRefNotAllowed, with each reference's location in the error message.
  • Exports references.ErrExternalRefNotAllowed so callers can match it with errors.Is.

Bug Fixes

  • Recovered panics in concurrent linter rule goroutines: panics are now converted to validation.Error results with stack traces rather than terminating the process.
  • YAML merge keys (<<) are expanded before $ref detection on Reference Objects, so merged references unmarshal as references instead of inline objects.
  • Walkers no longer panic on nil ServerVariable values or zero-value JSONSchema wrappers in programmatically built documents.