Commit 976b2fc
refactor: split the Deck response contract from its detail view (#55)
One Deck schema served two endpoints with different card-loading
strategies, leaking the difference into the type:
class Deck(DeckBase):
id: PositiveInt
cards: list[Card] | None
- get_deck -> fetch_with_cards (selectinload) -> cards populated.
- list_decks / create_deck / update_deck -> relationship is
lazy="noload", so .cards returns [] without loading.
So list_decks reported cards: [] for every deck even when the deck has
cards. The | None was also dead: noload yields [], never None.
Split the one schema into two:
class Deck(DeckBase):
"""Light deck view for lists and writes; cards are not loaded."""
id: PositiveInt
class DeckWithCards(Deck):
"""Deck detail view; cards are eager-loaded and always present."""
cards: list[Card]
- get_deck -> DeckWithCards (cards always present, non-optional)
- list_decks -> Decks (Collection[Deck]), create_deck / update_deck -> Deck
Each endpoint's return type now states exactly what it loads.
Contract change (intentional): the cards key disappears from list /
create / update responses (they never loaded cards anyway). get_deck is
unchanged.
Ports modern-python/litestar-sqlalchemy-template#30.
Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>1 parent a8c9572 commit 976b2fc
3 files changed
Lines changed: 11 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
71 | 71 | | |
72 | 72 | | |
73 | 73 | | |
| 74 | + | |
74 | 75 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
22 | 22 | | |
23 | 23 | | |
24 | 24 | | |
25 | | - | |
| 25 | + | |
26 | 26 | | |
27 | | - | |
| 27 | + | |
28 | 28 | | |
29 | 29 | | |
30 | 30 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
49 | 49 | | |
50 | 50 | | |
51 | 51 | | |
| 52 | + | |
| 53 | + | |
52 | 54 | | |
53 | | - | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
54 | 61 | | |
55 | 62 | | |
56 | 63 | | |
| |||
0 commit comments