Skip to content

fix(search): resolve null entrances in cave search results#1711

Merged
ClemRz merged 2 commits into
developfrom
fix/search-cave-entrances-null
Jun 28, 2026
Merged

fix(search): resolve null entrances in cave search results#1711
ClemRz merged 2 commits into
developfrom
fix/search-cave-entrances-null

Conversation

@ClemRz

@ClemRz ClemRz commented Jun 28, 2026

Copy link
Copy Markdown
Contributor

🤔 What

  • Fix entrances field in cave search results returning [null, null, ...] instead of actual entrance IDs
  • Add regression tests for toSimpleCave entrance ID extraction

🤷‍♂️ Why

The quick-search endpoint (POST /api/v1/search) returns null values in the entrances array for cave results. This happens because the production Typesense index stores entrance IDs as plain integers (e.g., [20, 21, 25877]), but the toSimpleCave converter assumed they would always be Waterline objects with an .id property. Calling .id on a number yields undefined, which JSON serializes as null.

🔍 How

Changed source.entrances?.map((e) => e.id) to source.entrances?.map((e) => e?.id ?? e) in toSimpleCave. This handles both formats:

  • Waterline objects (from DB queries with .populate()) → extracts .id
  • Plain IDs (from Typesense indexed documents) → passes through as-is

🧪 Testing

npm run test -- --grep "toSimpleCave"

Four new test cases cover:

  1. Waterline objects → extracts IDs
  2. Plain numeric IDs (Typesense format) → passes through
  3. String IDs → passes through
  4. Absent entrances field → returns undefined

- Handle both Waterline objects and plain IDs in toSimpleCave converter
- Add regression tests for entrance ID extraction
@ClemRz ClemRz self-assigned this Jun 28, 2026
@ClemRz ClemRz requested a review from Paul-AUB June 28, 2026 18:50
Paul-AUB
Paul-AUB previously approved these changes Jun 28, 2026

@Paul-AUB Paul-AUB left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestions (Should Consider)

  1. [test/integration/1_services/Converters.test.js] The null-in-array edge case is not covered. With e?.id ?? e, a null entry evaluates as null?.id → undefined, then undefined ?? null → null, so nulls pass through silently. This matches the old broken behavior and could mask corrupt Typesense documents. Consider adding a test that documents the current behavior (or asserts that nulls are filtered out, if that's the desired outcome).

Nitpicks (Optional)

  1. [test/integration/1_services/Converters.test.js] Consider adding a test for entrances: [] (empty array). It's trivially handled by the current code, but it documents the contract and prevents future regressions if anyone refactors the optional-chaining logic.

- Add .filter() to remove null/undefined from toSimpleCave entrances
- Add test for null-in-array edge case
- Add test for empty entrances array
@ClemRz

ClemRz commented Jun 28, 2026

Copy link
Copy Markdown
Contributor Author

Both review items addressed in 6c950a2:

  1. Null-in-array edge case — Added .filter((e) => e != null) after the .map() in toSimpleCave so null/undefined entries from corrupt Typesense documents are stripped rather than passing through silently. Added a regression test.

  2. Empty array contract test — Added a test for entrances: [] documenting it returns [].

All 3107 tests pass, lint clean.

@ClemRz ClemRz merged commit d47d115 into develop Jun 28, 2026
3 checks passed
@ClemRz ClemRz deleted the fix/search-cave-entrances-null branch June 28, 2026 23:01
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.

2 participants