Skip to content

fix: add Conn and *Conn to mapperFor and isUnsafe type switches#992

Closed
xbrxr03 wants to merge 1 commit into
jmoiron:masterfrom
xbrxr03:fix/mapperfor-conn-type
Closed

fix: add Conn and *Conn to mapperFor and isUnsafe type switches#992
xbrxr03 wants to merge 1 commit into
jmoiron:masterfrom
xbrxr03:fix/mapperfor-conn-type

Conversation

@xbrxr03

@xbrxr03 xbrxr03 commented Jun 22, 2026

Copy link
Copy Markdown

Problem

mapperFor() and isUnsafe() only handle DB, *DB, Tx, and *Tx types in their type switches. When a Stmt is created from a *sqlx.Conn, both functions fall through to the default case:

  • mapperFor(conn) returns the global mapper instead of conn.Mapper
  • isUnsafe(conn) returns false instead of conn.unsafe

This silently drops any custom Mapper configured on a Conn (e.g. via tagMapFunc), causing field mapping to use the wrong mapper when creating prepared statements from a connection.

Reported in #975.

Fix

Add Conn and *Conn to both type switches:

// mapperFor
case Conn:
    return i.Mapper
case *Conn:
    return i.Mapper

// isUnsafe
case Conn:
    return v.unsafe
case *Conn:
    return v.unsafe

Tests

  • TestMapperForConn: verifies mapperFor returns the correct Mapper for Conn and *Conn, including custom mapper instances
  • TestIsUnsafeConn: verifies isUnsafe returns the correct unsafe flag for Conn and *Conn

Fixes #975

mapperFor() and isUnsafe() only handled DB, *DB, Tx, and *Tx types.
When a Stmt was created from a *sqlx.Conn, both functions fell through
to the default case — mapperFor returned the global mapper instead of
the Conn's custom Mapper, and isUnsafe returned false instead of the
Conn's unsafe flag.

This meant that any custom Mapper configured on a Conn (e.g. via
tagMapFunc) was silently dropped when creating prepared statements,
causing field mapping to use the wrong mapper.

Add Conn and *Conn to both type switches with tests.

Fixes jmoiron#975
@xbrxr03

xbrxr03 commented Jun 29, 2026

Copy link
Copy Markdown
Author

Closing this PR due to lack of maintainer engagement. I'm happy to reopen if there's interest. Thanks for the great project.

@xbrxr03 xbrxr03 closed this Jun 29, 2026
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.

The reflectx.Mapper from conn cannot be passed to stmt when creating stmt from conn

2 participants