fix(open-api): correct get-session nullable schema for OAS 3.1 (#8389)

Co-authored-by: Maxwell <145994855+ping-maxwell@users.noreply.github.com>
Co-authored-by: ping-maxwell <maxwell.multinite@gmail.com>
This commit is contained in:
Oluwatobi Mustapha
2026-04-09 21:44:35 +01:00
committed by GitHub
parent 6ce30cf138
commit f6428d02fc
4 changed files with 29 additions and 6 deletions

View File

@@ -0,0 +1,5 @@
---
"better-auth": patch
---
fix(open-api): correct get-session nullable schema for OAS 3.1

View File

@@ -46,8 +46,8 @@ export const getSession = <Option extends BetterAuthOptions>() =>
content: {
"application/json": {
schema: {
type: "object",
nullable: true,
// better-call's OpenAPI schema type doesn't yet model OAS 3.1 union `type`.
type: ["object", "null"] as unknown as "object",
properties: {
session: {
$ref: "#/components/schemas/Session",

View File

@@ -1591,7 +1591,6 @@ exports[`open-api > should generate OpenAPI schema > openAPISchema 1`] = `
"content": {
"application/json": {
"schema": {
"nullable": true,
"properties": {
"session": {
"$ref": "#/components/schemas/Session",
@@ -1604,7 +1603,10 @@ exports[`open-api > should generate OpenAPI schema > openAPISchema 1`] = `
"session",
"user",
],
"type": "object",
"type": [
"object",
"null",
],
},
},
},
@@ -1735,7 +1737,6 @@ exports[`open-api > should generate OpenAPI schema > openAPISchema 1`] = `
"content": {
"application/json": {
"schema": {
"nullable": true,
"properties": {
"session": {
"$ref": "#/components/schemas/Session",
@@ -1748,7 +1749,10 @@ exports[`open-api > should generate OpenAPI schema > openAPISchema 1`] = `
"session",
"user",
],
"type": "object",
"type": [
"object",
"null",
],
},
},
},

View File

@@ -125,6 +125,20 @@ describe("open-api", async () => {
);
});
it("should use OpenAPI 3.1 nullable format for get-session response", async () => {
const schema = await auth.api.generateOpenAPISchema();
const paths = schema.paths as Record<string, any>;
const getSessionSchema =
paths["/get-session"].post.responses["200"].content["application/json"]
.schema;
expect(Array.isArray(getSessionSchema.type)).toBe(true);
expect(getSessionSchema.type).toContain("object");
expect(getSessionSchema.type).toContain("null");
expect(getSessionSchema.nullable).toBe(undefined);
});
it("should use anyOf format for optional object types in OpenAPI 3.1", async () => {
const schema = await auth.api.generateOpenAPISchema();
const paths = schema.paths as Record<string, any>;