fix(db-adapter): string[] and number[] fieldTypes incorrectly parsed for plugins (#4237)

This commit is contained in:
Dylan Vanmali
2025-12-02 10:24:22 -08:00
committed by GitHub
parent 189dedd006
commit fd7256014b
2 changed files with 89 additions and 0 deletions
@@ -247,6 +247,13 @@ export const createAdapterFactory =
fieldAttributes!.type === "json"
) {
newValue = JSON.stringify(newValue);
} else if (
config.supportsJSON === false &&
Array.isArray(newValue) &&
(fieldAttributes!.type === "string[]" ||
fieldAttributes!.type === "number[]")
) {
newValue = JSON.stringify(newValue);
} else if (
config.supportsDates === false &&
newValue instanceof Date &&
@@ -335,6 +342,12 @@ export const createAdapterFactory =
field.type === "json"
) {
newValue = safeJSONParse(newValue);
} else if (
config.supportsJSON === false &&
typeof newValue === "string" &&
(field.type === "string[]" || field.type === "number[]")
) {
newValue = safeJSONParse(newValue);
} else if (
config.supportsDates === false &&
typeof newValue === "string" &&
@@ -522,6 +522,82 @@ describe("Create Adapter Helper", async () => {
expect(createFALSEParameters.data.emailVerified).toBe(0);
});
test("Should modify string[] type to TEXT if the DB doesn't support it. And expect the result to be transformed back to string[]", async () => {
const createJSONParameters: { data: { preferences: string } } =
await new Promise(async (r) => {
const adapter = await createTestAdapter({
config: {
supportsJSON: false,
},
options: {
user: {
additionalFields: {
preferences: {
type: "string[]",
},
},
},
},
adapter(args_0) {
return {
async create(data) {
r(data as any);
return data.data;
},
};
},
});
const obj = { preferences: ["medium", "large"] };
const res = await adapter.create({
model: "user",
data: obj,
});
expect(res).toHaveProperty("preferences");
expect(res.preferences).toEqual(obj.preferences);
});
expect(createJSONParameters.data).toHaveProperty("preferences");
expect(createJSONParameters.data.preferences).toEqual(
'[\"medium\",\"large\"]',
);
});
test("Should modify number[] type to TEXT if the DB doesn't support it. And expect the result to be transformed back to number[]", async () => {
const createJSONParameters: { data: { preferences: string } } =
await new Promise(async (r) => {
const adapter = await createTestAdapter({
config: {
supportsJSON: false,
},
options: {
user: {
additionalFields: {
preferences: {
type: "number[]",
},
},
},
},
adapter(args_0) {
return {
async create(data) {
r(data as any);
return data.data;
},
};
},
});
const obj = { preferences: [6, 7] };
const res = await adapter.create({
model: "user",
data: obj,
});
expect(res).toHaveProperty("preferences");
expect(res.preferences).toEqual(obj.preferences);
});
expect(createJSONParameters.data).toHaveProperty("preferences");
expect(createJSONParameters.data.preferences).toEqual("[6,7]");
});
test("Should modify JSON type to TEXT if the DB doesn't support it. And expect the result to be transformed back to JSON", async () => {
const createJSONParameters: { data: { preferences: string } } =
await new Promise(async (r) => {