Repository navigation
Add /collections endpoint - #32
Conversation
…etadata Lets the frontend list collections to hide and exclude them from random picks (weights re-normalized over the remaining set, not just filtered client-side), and surfaces creator/date/subject for a fuller "more info" view.
The Swift handler already switched on it, but SAM's HttpApi only wires up explicitly declared routes -- without this the API Gateway itself 404s before the request ever reaches the Lambda.
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: ASSERTIVE Plan: Pro Plus Run ID: 📒 Files selected for processing (1)
🔗 Linked repositories identifiedCodeRabbit considers these linked repositories for cross-repo context during reviews:
📝 WalkthroughWalkthroughAdds collection exclusion support to random image selection, a new ChangesCollection Exclusion Feature
Estimated code review effort: 3 (Moderate) | ~25 minutes Sequence Diagram(s)sequenceDiagram
participant Client
participant NZImageApiLambda
participant NZImageApi
participant DigitalNZAPIDataSource
Client->>NZImageApiLambda: GET /image?exclude=a,b
NZImageApiLambda->>NZImageApi: image(collection: nil, exclude: [a,b])
NZImageApi->>NZImageApi: filteredCollectionWeights(excluding: [a,b])
NZImageApi->>DigitalNZAPIDataSource: newResult(weightsOverride: filteredWeights)
DigitalNZAPIDataSource-->>NZImageApi: NZRecordsResult
NZImageApi-->>NZImageApiLambda: NZRecordsResult
NZImageApiLambda-->>Client: JSON response
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
✨ Simplify code
Comment |
There was a problem hiding this comment.
Actionable comments posted: 3
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
Sources/NZImageApiLambda/NZImageApi.swift (1)
119-133: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value
NoEligibleCollectionsErroris logged as an "unexpected error."Since
NoEligibleCollectionsErrordoesn't conform toRichError, it falls into the genericelsebranch and gets logged as"An unexpected error occurred: ...", even though the docstring on Line 99-100 frames it as an expected, user-triggerable condition (all collections excluded). This makes it harder to distinguish genuine bugs from user misconfiguration in logs.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@Sources/NZImageApiLambda/NZImageApi.swift` around lines 119 - 133, `NoEligibleCollectionsError` is currently treated as a generic unexpected failure in `NZImageApi.newResult` because it does not match the `RichError` branch. Update the error handling around the `digitalNZAPIDataSource.newResult(...)` call so this expected, user-triggerable case is logged distinctly from real bugs; use the existing `NoEligibleCollectionsError` symbol (and the surrounding `do/catch` in `NZImageApi.swift`) to add a specific catch or equivalent check, and keep the generic `"unexpected error"` path only for truly unknown errors.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@Sources/NZImageApiLambda/Data` Sources/DigitalNZAPIDataSource.swift:
- Around line 51-53: `weightedRandomPick()` can trap on an empty dictionary
because it falls through to the last element access; update this helper to
handle empty input safely by either returning an optional or adding a clear
precondition/guard before selection. Keep the fix localized in the
`weightedRandomPick()` implementation and adjust the `chosenCollection =
(weightsOverride ?? collectionWeights).weightedRandomPick()` call site only if
the new return type requires it.
In `@Sources/NZImageApiLambda/Models/NZRecordsResult.swift`:
- Around line 25-27: Keep the `NZRecordsResult` metadata fields (`creator`,
`date`, `subject`) as optional arrays because synthesized `Codable` depends on
them being nullable when keys are missing. Do not change them to non-optional
`[String] = []` unless you also add a custom `init(from:)` that uses
`decodeIfPresent(... ) ?? []`; otherwise decoding will fail for absent DigitalNZ
metadata fields. If needed, suppress SwiftLint’s
`discouraged_optional_collection` directly on those properties in
`NZRecordsResult`.
In `@Tests/NZImageApiLambdaTests/FilteredCollectionWeightsTests.swift`:
- Line 15: The SwiftLint `required_deinit` warning is being triggered on the
stateless `FilteredCollectionWeightsTests` XCTestCase subclass, so don’t add a
boilerplate deinit here; instead adjust the lint configuration to exempt test
targets or `XCTestCase` subclasses globally. Update the relevant SwiftLint
rules/config so `FilteredCollectionWeightsTests` and similar test classes are
ignored, keeping the test file itself unchanged.
---
Outside diff comments:
In `@Sources/NZImageApiLambda/NZImageApi.swift`:
- Around line 119-133: `NoEligibleCollectionsError` is currently treated as a
generic unexpected failure in `NZImageApi.newResult` because it does not match
the `RichError` branch. Update the error handling around the
`digitalNZAPIDataSource.newResult(...)` call so this expected, user-triggerable
case is logged distinctly from real bugs; use the existing
`NoEligibleCollectionsError` symbol (and the surrounding `do/catch` in
`NZImageApi.swift`) to add a specific catch or equivalent check, and keep the
generic `"unexpected error"` path only for truly unknown errors.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: ASSERTIVE
Plan: Pro Plus
Run ID: ddecfd56-d5dd-4981-8f31-bc13793aec54
📒 Files selected for processing (6)
Sources/NZImageApiLambda/Data Sources/DigitalNZAPIDataSource.swiftSources/NZImageApiLambda/Models/NZRecordsResult.swiftSources/NZImageApiLambda/NZImageApi.swiftSources/NZImageApiLambda/NZImageApiLambda.swiftTests/NZImageApiLambdaTests/FilteredCollectionWeightsTests.swifttemplate.yaml
CodeRabbit review on #32: falling through to the generic catch made an expected, user-triggerable condition (an exclude list covering every collection) read as a genuine bug in the logs.
|
Addressing the outside-diff-range comment on |
Summary
GET /collectionsroute (Swift handler switch + SAMHttpApievent) that returns the list of collection names fromNZImageApi.collectionImageCounts.excludesupport toGET /image: a comma-separatedexcludequery param filters those collections out of the random pick and re-normalizes weights over the remaining set (NZImageApi.filteredCollectionWeights), rather than subtracting counts and leaving weights unnormalized.creator/date/subjecttoNZRecordsResult(decoded straight from the existing DigitalNZ response shape; no new fetch/mapping needed) for a fuller "more info" view.Why
nzimage-web's Settings modal already calls/api/collectionsto populate the "Hidden Collections" list, and itsNZImagemodel already parsescreator/date/subject/exclude— none of that has ever worked in production because this repo'smainnever had a/collectionsroute orexclude/richer-metadata support. The work existed on a stale, unmerged branch (settings-metadata-and-exclude, diverged 2026-07-06/07) that predates severalmainchanges since then (theswift-aws-lambda-eventsv1 upgrade makingqueryStringParametersnon-optional, the AL2023 runtime migration, and the code-review security fixes). This PR rebases just the endpoint/feature work (2 commits:Add /collections endpoint, exclude-filtered image picks, and richer metadataandRegister the /collections route on the HTTP API) onto currentmain, fixing one resulting compile break (a leftover?onqueryStringParametersthat's no longer optional).Test plan
swift buildsucceedsswift test— all 131 tests pass (including the newFilteredCollectionWeightsTests)nzimage-web's Settings → Hidden Collections list populates in production🤖 Generated with Claude Code
Summary by CodeRabbit
GET /collectionsendpoint to list available collections.