Skip to content

Fix OAuth GetByToken panic with GORM many-to-many relation - #9

Merged
cursor[bot] merged 1 commit into
mainfrom
cursor/fix-oauth-member-scan-panic-99f3
Sep 18, 2026
Merged

cursor[bot] merged 1 commit into
mainfrom
cursor/fix-oauth-member-scan-panic-99f3

Conversation

@demdxx

@demdxx demdxx commented Sep 18, 2026

Copy link
Copy Markdown
Contributor

Problem

Affyard MCP OAuth token exchange succeeded, but the first authenticated POST /mcp panicked when resolving the access token to a user/account:

http: panic serving ...: reflect.StructOf: field 2 has invalid name

gorm.io/gorm/schema.(*Schema).buildMany2ManyRelation (.../relationship.go:363)
→ GetByToken → Authorize

Root Cause

The GetByToken method was scanning into a generic Member[TUser, TAccount] struct that embeds MemberBase (which has a many-to-many Roles relation). When GORM tried to dynamically build a struct via reflect.StructOf to process the many-to-many relation, it encountered the generic type parameters (TUser, TAccount) and panicked because these don't produce valid field names for reflection.

Solution

Changed GetByToken to scan into models.MemberBase directly instead of the full generic Member[TUser, TAccount] type. The Account and User relations on the generic Member type weren't being used anyway — they're fetched separately with explicit db.First() calls immediately after.

This avoids GORM's schema-building issue with generic types while maintaining identical behavior.

Testing

  • Added regression test TestGetByToken covering the full OAuth access token → user/account flow
  • All existing tests pass
  • Fix verified to prevent the production panic

Affyard Impact

Affyard can bump the blaze-api submodule after this merge to resolve the MCP OAuth panic.

Open in Web Open in Cursor 

The GetByToken method was scanning into a generic Member[TUser, TAccount] struct, causing GORM's reflect.StructOf to panic when processing the many-to-many Roles relation with invalid field names from generic types.

Changed to scan into models.MemberBase directly, since the Account and User relations are fetched separately anyway. This avoids the GORM schema-building issue with generic types.

Added test coverage for GetByToken to prevent regression.

Co-authored-by: Dmitry <demdxx@gmail.com>
@demdxx
demdxx marked this pull request as ready for review September 18, 2026 16:06
@coveralls

Copy link
Copy Markdown

Coverage Report for CI Build 35365839180

Coverage increased (+0.1%) to 4.203%

Details

  • Coverage increased (+0.1%) from the base build.
  • Patch coverage: 8 of 8 lines across 1 file are fully covered (100%).
  • No coverage regressions found.

Uncovered Changes

No uncovered changes found.

Coverage Regressions

No coverage regressions found.


Coverage Stats

Coverage Status
Relevant Lines: 27482
Covered Lines: 1155
Line Coverage: 4.2%
Coverage Strength: 0.04 hits per line

💛 - Coveralls

@cursor
cursor Bot merged commit dd6befc into main Sep 18, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants