Skip to content

#141 Part 1: Implement parseItem callback in parseCollectionResponse + unit tests #154

@Sam-Bolling

Description

@Sam-Bolling

Parent Issue

Split from #141parseCollectionResponse 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 links array 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)

  • parseCollectionResponse requires a parseItem callback parameter
  • No as T[] casts remain in the function
  • response.spec.ts call sites updated to pass parseItem
  • New test: server returning [null, 42, {"no-id": true}] — elements go through parseItem (no untyped cast)
  • tsc --noEmit — zero errors
  • response.spec.ts tests 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:


Operational Constraints

⚠️ MANDATORY: Before starting work on this issue, review docs/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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions