Repository navigation
[Measurement run – do not merge] STORY-142: private article bookmarks API (backend) - #1203
devin-ai-integration[bot] wants to merge 1 commit into
Conversation
Co-Authored-By: Pedro Tabio <paltat@gmail.com>
|
I'll fix CI failures and address comments from users with write access. I'll skip comments containing "(aside)".
|
|
Real-backend STORY-142 verification passed with signed-in users and SQLite persistence; independent API checks: 24 passed. Supplementary UI pagination/backfill checks also passed. Default external avatars were unavailable. Backend suite: 84 passed. Security checks: 3/3 passed. The unchanged baseline Spotless violation is documented in the description. Not exercised in UI: network-failure/concurrent-mutation scenarios or other browsers. |
| @GetMapping(path = "bookmarked") | ||
| public ResponseEntity getBookmarked( |
There was a problem hiding this comment.
🟡 Articles named bookmarked become inaccessible
When an article has slug bookmarked, getBookmarked takes its GET route instead of the article handler. Anonymous readers receive 401; signed-in readers receive their bookmark list instead of that article.
Learn more
Article titles are converted into slugs by toSlug. The existing article handler accepts any slug at the same path depth as this new fixed route. Spring MVC selects the fixed route for bookmarked, so it never reaches the article handler for that slug. The security configuration also requires authentication for the fixed route, making a formerly public article URL return 401 to anonymous readers.
Example: Creating an article titled Bookmarked creates slug bookmarked. GET /articles/bookmarked now returns 401 without a token, or the caller's saved articles with a token, rather than the new article.
Recommended fix: Reserve bookmarked during article slug creation and migration with a clear validation error, or put the bookmark-list route at a path that cannot collide with article slugs. Account for existing articles with the slug before deploying.
Was this helpful? React with 👍 or 👎 to provide feedback.
There was a problem hiding this comment.
Confirmed: an article with slug bookmarked would be shadowed. This is the same trade-off GET /articles/feed already makes on master: a literal route wins over /articles/{slug} and needs auth. The path GET /articles/bookmarked is fixed by the shared STORY-142 contract the frontend PR codes against, so I'm not moving it in this PR. Reserving feed and bookmarked as slugs would be a separate hardening change. I've flagged it to the coordinator. Leaving this unresolved.
| @PathVariable("slug") String slug, @AuthenticationPrincipal User user) { | ||
| Article article = | ||
| articleRepository.findBySlug(slug).orElseThrow(ResourceNotFoundException::new); | ||
| articleBookmarkRepository.save(new ArticleBookmark(article.getId(), user.getId())); |
There was a problem hiding this comment.
🟡 Concurrent deletion leaves orphan bookmarks
If deletion finishes between bookmarkArticle resolving a slug and inserting, the POST saves a bookmark for a deleted article. remove already cleared bookmarks, and the response lookup fails while the orphan remains stored.
Learn more
The bookmark POST checks the article before inserting its bookmark. Article deletion removes bookmarks and then deletes the article, but the POST does not hold the article lookup and insert in that transaction. The bookmark schema has no foreign key, so an insert after deletion succeeds. The subsequent article lookup returns empty and the POST fails, while the orphan bookmark persists. Normal bookmark-list reads hide it because the list query joins existing articles.
Example: Alice starts bookmarking Bob's article and passes the slug lookup. Bob deletes it, clearing bookmarks. Alice's insert then succeeds; her POST fails and an invisible bookmark row remains.
Recommended fix: Enforce article existence with a foreign key and deletion cascade where supported, or synchronize the bookmark insert with article deletion in one transaction with a database-level existence check. Handle the deleted-article outcome as a missing article rather than returning a server error.
Was this helpful? React with 👍 or 👎 to provide feedback.
There was a problem hiding this comment.
Confirmed: the race is real, but the window is narrow and the effect is limited.
- Users can't see the orphan row.
findBookmarkedArticleIdsandcountBookmarkedArticlesboth joinarticles, andisUserBookmark/userBookmarksare only queried with ids of articles that still exist. - The racing POST would return 500 (
findBySlug(...).get()on an empty result) instead of 404.ArticleFavoriteApihas the same pattern.
A minimal fix that doesn't need a schema change:
- Make the insert
INSERT ... SELECT ... WHERE EXISTS (SELECT 1 FROM articles WHERE id = ?) ON CONFLICT DO NOTHING. SQLite serializes write transactions, so this can't interleave with the@Transactionaldelete. - Map an empty response lookup to
ResourceNotFoundException.
I'm holding off because the coordinator froze implementation on this PR. I've raised it with them and will apply the fix if they approve.
Story: STORY-142 source and acceptance criteria
Paired frontend PR: COG-GTM/angular-realworld-example-app#84 (draft Fusion rerun).
Summary
Adds private, per-user article bookmarks, modeled on the existing favorites stack (
ArticleFavoriteApi/core/favorite/ArticleFavoriteMapper/ArticleFavoritesReadService).Contract (bare
/articlespaths, same as the other controllers; local e2e runs with--server.servlet.context-path=/api):POST /articles/{slug}/bookmarkandDELETE /articles/{slug}/bookmarkreturn{article: ArticleData}.bookmarkedis specific to the viewer. An unknown slug returns 404. Both calls are idempotent.GET /articles/bookmarked?limit=&offset=returns{articles, articlesCount}for the current user only (no owner param), newest saved first./articles/bookmarkedis added to the authenticated GET matcher, ahead of the public/articles/**). There's no public bookmark count.ArticleData.bookmarked: booleanis now on every REST article response (single, list, feed). It'sfalsefor anonymous viewers.Things that aren't obvious from the diff:
V2__create_article_bookmarks.sql:article_bookmarks(article_id, user_id, created_at)with a primary key on(article_id, user_id)and an index on(user_id, created_at).... ON CONFLICT(article_id, user_id) DO NOTHING, so a repeated POST keeps the originalcreated_at.ORDER BY B.created_at DESC, B.rowid DESC.rowidgives a deterministic insertion-order tie-break (this app only runs on SQLite).ArticleReadService.findArticles(ids)always orders byA.created_at DESC, soArticleQueryService.findBookmarkedArticlesputs the hydrated rows back into saved order afterwards.MyBatisArticleRepository.removeis now@Transactionaland deletes the article's bookmarks first (ArticleBookmarkMapper.deleteByArticleId).Tests
JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64 ./gradlew clean test: 84 tests, 0 failures, 0 errors, 0 skipped.compileJava compileTestJavapasses.ArticleBookmarkApiTest: bookmark and unbookmark, 404 on an unknown slug, 401 on POST and DELETE without a tokenListArticleApiTest:GET /articles/bookmarkedpasses the user andPage(offset, limit), returns 401 without a token; list responses includebookmarkedMyBatisArticleBookmarkRepositoryTest: save and find, a repeated save keeps the originalcreated_at, removing a missing bookmark does nothingArticleQueryServiceTest:bookmarkedper viewer on single/recent/feed, saved-time ordering with a tie-break (independent of articlecreated_at), pagination and total count, isolation between users, no reorder on re-save, cleanup when an article is deletedspotlessJavaCheckfails only onDefaultJwtServiceTest.java, which is already unformatted onmaster. I left it out of this diff on purpose.UI coverage limits: network-failure/concurrent-mutation scenarios and non-Chromium browsers were not exercised end to end. Unit tests cover request-error handling.
Devin-Org: engineering
Open in Devin Desktop: https://devin-gtm.devinenterprise.com/desktop/session/1ab6e443eb49449d9482f7c1a6d9e583?variant=devin
Real-backend integration evidence
Verified both feature branches together: Angular on localhost:4200 → Spring Boot on localhost:8080/api with real SQLite persistence, not mocked responses. Two users (ana and eric) registered through the UI; a third fixture author allowed the same shared article to be compared as a reader under both identities.
The paired frontend PR contains both design-frame screenshots and the animated recording. Backend security checks passed (3/3). The unchanged baseline Spotless violation remains unresolved; it was reproduced on clean master with identical output and file content.
UI coverage limits: network-failure/concurrent-mutation scenarios and non-Chromium browsers were not exercised end to end. Unit tests cover request-error handling.
Devin-Org: engineering
Devin Review