Skip to content

fix: allow creating empty sessions - #303

Open
lprnmns wants to merge 1 commit into
fastify:mainfrom
lprnmns:fix/create-secure-session-without-data-human-dco
Open

lprnmns wants to merge 1 commit into
fastify:mainfrom
lprnmns:fix/create-secure-session-without-data-human-dco

Conversation

@lprnmns

@lprnmns lprnmns commented Aug 31, 2026

Copy link
Copy Markdown

Problem

FastifyInstance.createSecureSession(data?) is declared with optional data in types/index.d.ts and is exercised without an argument in the TypeScript tests. At runtime, calling createSecureSession() throws while Session initializes its timestamp, so integrations that need an empty session cannot use the public helper.

Fix

Default omitted data to an empty object at the createSecureSession decorator boundary. Existing object arguments are unchanged, and the regression assertion confirms the returned empty session is usable.

Tests

  • node --test test/decorators.test.js - passed; 3 tests
  • npm test - passed; 49 unit tests with 100 percent coverage and 16 TypeScript assertions
  • npm run lint - passed
  • git diff HEAD^ HEAD --check - passed

Compatibility

This changes only the previously failing omitted or undefined-data case to match the existing optional TypeScript contract. Sessions created with an object retain their current behavior.

Related issue

Independent reproduction; no issue linked.

Signed-off-by: lprnmns <manasalperen@gmail.com>
@lprnmns
lprnmns marked this pull request as ready for review August 31, 2026 16:19
@Fdawgs
Fdawgs requested a balanced review from Copilot September 22, 2026 12:49

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟢 Approval recommended

The focused fix matches the public type contract and has adequate regression coverage.

Review effort: Balanced
Findings: None

What changed in this PR

Aligns runtime behavior with the existing optional-data TypeScript contract.

Changes:

  • Defaults omitted session data to an empty object.
  • Adds regression coverage for empty-session creation.
File Description
index.js Adds the empty-object default.
test/​decorators.test.js Verifies empty sessions are usable.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@Fdawgs

Fdawgs commented Sep 22, 2026

Copy link
Copy Markdown
Member

I guess the question is are the types wrong or is the implementation wrong?

@lprnmns

lprnmns commented Sep 24, 2026

Copy link
Copy Markdown
Author

I think the implementation is the mismatch here. types/index.d.ts explicitly makes data optional, and types/index.tst.ts already checks app.createSecureSession(); separately, the plugin creates a new session with {} when a request has no cookie (index.js). The public decorator alone passes omitted data as undefined, which makes Session throw while setting __ts. Defaulting only that decorator argument to {} aligns it with both existing contracts without changing calls that provide data. If the intended API is instead to require data, I can update the type declaration and type test, but that would be a breaking change to the declared API.

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