Skip to content

Fix thread-safety bug in OpenAPIDereferencer31#2294

Open
dontgitit wants to merge 1 commit intoswagger-api:masterfrom
dontgitit:fix/dereferencer31-thread-safety
Open

Fix thread-safety bug in OpenAPIDereferencer31#2294
dontgitit wants to merge 1 commit intoswagger-api:masterfrom
dontgitit:fix/dereferencer31-thread-safety

Conversation

@dontgitit
Copy link

Summary

Fixes #2293

OpenAPIDereferencer31 is instantiated as a singleton via DereferencersFactory, but its dereference() method stored per-call state in mutable instance fields (openAPI, result, messages). When multiple threads parse different OpenAPI 3.1 specs concurrently with resolve=true, these fields get overwritten by whichever thread runs last, causing one parse to return another parse's result.

Fix: Make openAPI and result local variables in dereference() instead of instance fields, since they are only used within that method. Also remove the unused messages field which had the same issue.

This only affects OpenAPI 3.1 specs — the 3.0 code path uses a per-call OpenAPIResolver instance and is not affected.

Changes

  • OpenAPIDereferencer31.java: Converted openAPI and result from instance fields to local variables in dereference(). Removed unused messages field.
  • OpenAPIDereferencer31ThreadSafetyTest.java: New test that parses two different OpenAPI 3.1 specs concurrently (200 iterations across 8 threads) and asserts each result matches its input.

Test plan

  • New test OpenAPIDereferencer31ThreadSafetyTest fails without the fix, passes with it
  • Verified by temporarily reverting the fix and running the test — fails with ExecutionException due to assertion mismatch

OpenAPIDereferencer31 is a singleton (via DereferencersFactory) but stored
per-call state in mutable instance fields `openAPI` and `result`. When
multiple threads parse different OpenAPI 3.1 specs concurrently, these
fields get overwritten, causing one parse to return another's result.

Fix: make `openAPI` and `result` local variables in `dereference()` instead
of instance fields, since they are only used within that method.

Also remove the unused `messages` instance field which had the same problem.

Fixes swagger-api#2293

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Thread-safety bug: OpenAPIDereferencer31 singleton has mutable instance fields overwritten on each call

1 participant