[PR #3703] [CLOSED] Fix/organization additional fields 3686 #4961

Closed
opened 2026-03-13 12:05:38 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/3703
Author: @adityashirsatrao007
Created: 7/30/2025
Status: Closed

Base: mainHead: fix/organization-additional-fields-3686


📝 Commits (4)

  • 24bbd79 Fix organization additional fields not being returned in API responses
  • 415582f Fix organization additional fields not being returned
  • 93ff22c Update tests and add changeset for organization additional fields fix
  • f2f3c45 Clean up temporary test files and fix formatting

📊 Changes

4 files changed (+200 additions, -5 deletions)

View changed files

.changeset/fix-organization-additional-fields.md (+12 -0)
📝 packages/better-auth/src/adapters/create-adapter/index.ts (+7 -0)
📝 packages/better-auth/src/plugins/organization/adapter.ts (+11 -5)
packages/better-auth/src/plugins/organization/test/additional-fields.test.ts (+170 -0)

📄 Description

🔧 Fix: Organization additional fields not being returned in API responses

Fixes #3686

🐛 Problem

Organization additional fields configured with returned: true were not being included in API responses from methods like listOrganizations(), getFullOrganization(), and useActiveOrganization(), despite being properly configured and stored in the database.

Issue Details:

  • User configured organization additional fields (e.g., publicId) with returned: true
  • Fields exist in the database and are properly stored
  • API responses were missing these additional fields
  • Type inference was also not working for the additional fields

🔍 Root Cause Analysis

Through extensive debugging and investigation, I identified two key issues:

  1. Primary Issue: The transformOutput function in the adapter layer wasn't properly handling field filtering based on the returned attribute
  2. Secondary Issue: Organization plugin additional fields aren't being included in the schema that transformOutput uses (deeper schema generation issue)

Changes Made

1. Enhanced transformOutput Function (/packages/better-auth/src/adapters/create-adapter/index.ts)

  • Before: Only included fields that existed in the database result
  • After: Includes all fields with returned !== false, even when they're undefined/null from the database
  • Logic: Fields with returned: true should always be present in the response, defaulting to undefined if missing from DB

2. Fixed Organization Adapter (/packages/better-auth/src/plugins/organization/adapter.ts)

  • Before: Only returning basic organization fields, losing additional fields
  • After: Preserves all fields from the transformed output while only handling metadata parsing
  • Benefit: Ensures additional fields from transformOutput are not lost

3. Comprehensive Test Coverage (/packages/better-auth/src/plugins/organization/test/additional-fields.test.ts)

  • Added test cases to verify fields with returned: true are present
  • Added test cases to verify fields with returned: false are excluded
  • Tests currently demonstrate the issue and will pass once the schema generation is fixed

📋 Technical Details

Files Modified:

  • /packages/better-auth/src/adapters/create-adapter/index.ts - Core transformation logic
  • /packages/better-auth/src/plugins/organization/adapter.ts - Organization-specific fixes
  • /packages/better-auth/src/plugins/organization/test/additional-fields.test.ts - Test coverage

Key Code Changes:

// Before: Only included existing fields
if (result[key] !== undefined) {
  output[fieldName || key] = result[key];
}

// After: Include fields with returned !== false
if (result[key] !== undefined || field.returned !== false) {
  output[fieldName || key] = result[key];
}

---

<sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
## 📋 Pull Request Information **Original PR:** https://github.com/better-auth/better-auth/pull/3703 **Author:** [@adityashirsatrao007](https://github.com/adityashirsatrao007) **Created:** 7/30/2025 **Status:** ❌ Closed **Base:** `main` ← **Head:** `fix/organization-additional-fields-3686` --- ### 📝 Commits (4) - [`24bbd79`](https://github.com/better-auth/better-auth/commit/24bbd79278844ca4fbe6eb6cb4f4cddea970aea4) Fix organization additional fields not being returned in API responses - [`415582f`](https://github.com/better-auth/better-auth/commit/415582ff34c217107ef46ddacbb7241c44b3f6a8) Fix organization additional fields not being returned - [`93ff22c`](https://github.com/better-auth/better-auth/commit/93ff22cccb426610456175410c44072ddae814f7) Update tests and add changeset for organization additional fields fix - [`f2f3c45`](https://github.com/better-auth/better-auth/commit/f2f3c454fb4c3e0d96a632b0f671a08c7203c019) Clean up temporary test files and fix formatting ### 📊 Changes **4 files changed** (+200 additions, -5 deletions) <details> <summary>View changed files</summary> ➕ `.changeset/fix-organization-additional-fields.md` (+12 -0) 📝 `packages/better-auth/src/adapters/create-adapter/index.ts` (+7 -0) 📝 `packages/better-auth/src/plugins/organization/adapter.ts` (+11 -5) ➕ `packages/better-auth/src/plugins/organization/test/additional-fields.test.ts` (+170 -0) </details> ### 📄 Description ## 🔧 Fix: Organization additional fields not being returned in API responses **Fixes #3686** ### 🐛 Problem Organization additional fields configured with `returned: true` were not being included in API responses from methods like `listOrganizations()`, `getFullOrganization()`, and `useActiveOrganization()`, despite being properly configured and stored in the database. **Issue Details:** - User configured organization additional fields (e.g., `publicId`) with `returned: true` - Fields exist in the database and are properly stored - API responses were missing these additional fields - Type inference was also not working for the additional fields ### 🔍 Root Cause Analysis Through extensive debugging and investigation, I identified two key issues: 1. **Primary Issue**: The `transformOutput` function in the adapter layer wasn't properly handling field filtering based on the `returned` attribute 2. **Secondary Issue**: Organization plugin additional fields aren't being included in the schema that `transformOutput` uses (deeper schema generation issue) ### ✅ Changes Made #### 1. Enhanced `transformOutput` Function (`/packages/better-auth/src/adapters/create-adapter/index.ts`) - **Before**: Only included fields that existed in the database result - **After**: Includes all fields with `returned !== false`, even when they're `undefined`/`null` from the database - **Logic**: Fields with `returned: true` should always be present in the response, defaulting to `undefined` if missing from DB #### 2. Fixed Organization Adapter (`/packages/better-auth/src/plugins/organization/adapter.ts`) - **Before**: Only returning basic organization fields, losing additional fields - **After**: Preserves all fields from the transformed output while only handling metadata parsing - **Benefit**: Ensures additional fields from `transformOutput` are not lost #### 3. Comprehensive Test Coverage (`/packages/better-auth/src/plugins/organization/test/additional-fields.test.ts`) - Added test cases to verify fields with `returned: true` are present - Added test cases to verify fields with `returned: false` are excluded - Tests currently demonstrate the issue and will pass once the schema generation is fixed ### 📋 Technical Details **Files Modified:** - ✅ `/packages/better-auth/src/adapters/create-adapter/index.ts` - Core transformation logic - ✅ `/packages/better-auth/src/plugins/organization/adapter.ts` - Organization-specific fixes - ✅ `/packages/better-auth/src/plugins/organization/test/additional-fields.test.ts` - Test coverage **Key Code Changes:** ```typescript // Before: Only included existing fields if (result[key] !== undefined) { output[fieldName || key] = result[key]; } // After: Include fields with returned !== false if (result[key] !== undefined || field.returned !== false) { output[fieldName || key] = result[key]; } --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
GiteaMirror added the pull-request label 2026-03-13 12:05:38 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#4961