-
Notifications
You must be signed in to change notification settings - Fork 1
#141 Part 1: Implement parseItem callback in parseCollectionResponse + unit tests #154
Description
Parent Issue
Split from #141 — parseCollectionResponse casts raw JSON to T[] without element validation.
This is Part 1 of 2. See also: Part 2 (integration test call-site updates — issue to follow).
Scope — Part 1 Only
This part implements the core change (Option A from #141) and updates the unit test file. It does NOT touch integration test files — those are Part 2.
Files to Modify
| File | Action | Est. Edits |
|---|---|---|
src/ogc-api/csapi/formats/response.ts |
Add parseItem callback parameter, replace as T[] casts |
~10 lines |
src/ogc-api/csapi/formats/response.spec.ts |
Update existing call sites to pass parseItem; add malformed-element test |
~15 lines |
Implementation
Add a parseItem callback parameter to parseCollectionResponse (Option A from the parent issue):
export function parseCollectionResponse<T>(
body: unknown,
parseItem: (item: unknown, index: number) => T
): CollectionResponse<T> {
// ...existing envelope normalization unchanged...
const rawItems = Array.isArray(obj.features)
? obj.features
: (obj.items as unknown[]);
const items = rawItems.map((item, i) => parseItem(item, i));
return { items, links, numberMatched, numberReturned, timeStamp };
}What NOT to Touch
- ❌ Integration test files (
discovery.spec.ts,command.spec.ts,navigation.spec.ts,observation.spec.ts,pipeline.spec.ts) — those are Part 2 - ❌ The
CollectionResponse<T>interface definition - ❌ Envelope normalization logic (links, numberMatched, timeStamp extraction)
- ❌ The
linksarray cast at line 112 - ❌ Any file outside
src/ogc-api/csapi/
Design Constraint
The parseItem callback is typed extraction, not validation reintroduction. The existing Part 2 parsers (parseDatastream, parseObservation, etc.) already take (json: unknown) => T and extract fields with type guards and defaults. The callback delegates to these existing tolerant parsers — it does NOT add a new validation layer. See Validation-Extraction Decoupling Design Notes.
Acceptance Criteria (Part 1)
-
parseCollectionResponserequires aparseItemcallback parameter - No
as T[]casts remain in the function -
response.spec.tscall sites updated to passparseItem - New test: server returning
[null, 42, {"no-id": true}]— elements go throughparseItem(no untyped cast) -
tsc --noEmit— zero errors -
response.spec.tstests pass
Note: After Part 1, integration tests WILL fail because their call sites haven't been updated yet. That's expected — Part 2 fixes them.
Dependencies
Blocked by: Nothing
Blocks: Part 2 (integration test updates)
References
All references from #141 apply. Key ones for this part:
docs/code-review/003-pending-p1-unchecked-generic-cast-response.md— original findingsrc/ogc-api/csapi/formats/response.tslines 87–130 — affected functionsrc/ogc-api/csapi/formats/part2.ts— existing per-item parsers that serve asparseItemcallbacks- Validation-Extraction Decoupling — design constraint
Operational Constraints
⚠️ MANDATORY: Before starting work on this issue, reviewdocs/governance/AI_OPERATIONAL_CONSTRAINTS.md.
Upstream Isolation Constraint
Per the upstream maintainer's comment on PR #136: all files in this part are within src/ogc-api/csapi/ — purely internal to the isolated CSAPI module.