Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix flaky export test. #14932

Merged
merged 3 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 17 additions & 13 deletions packages/server/src/api/controllers/view/exporters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,33 @@ function getHeaders(
return headers.map(header => `"${customHeaders[header] || header}"`)
}

function escapeCsvString(str: string) {
return str.replace(/"/g, '""')
}

export function csv(
headers: string[],
rows: Row[],
delimiter: string = ",",
customHeaders: { [key: string]: string } = {}
) {
let csv = getHeaders(headers, customHeaders).join(delimiter)
let csvRows = [getHeaders(headers, customHeaders)]

for (let row of rows) {
csv = `${csv}\n${headers
.map(header => {
let val = row[header]
val =
typeof val === "object" && !(val instanceof Date)
? `"${JSON.stringify(val).replace(/"/g, "'")}"`
: val !== undefined
? `"${val}"`
: ""
return val.trim()
csvRows.push(
headers.map(header => {
const val = row[header]
if (typeof val === "object" && !(val instanceof Date)) {
return `"${JSON.stringify(val).replace(/"/g, "'")}"`
}
if (val !== undefined) {
return `"${escapeCsvString(val.toString())}"`
}
return ""
})
.join(delimiter)}`
)
}
return csv
return csvRows.map(row => row.join(delimiter)).join("\n")
}

export function json(rows: Row[]) {
Expand Down
34 changes: 34 additions & 0 deletions packages/server/src/api/routes/tests/row.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2630,6 +2630,40 @@ describe.each([
})
})

it("can handle csv-special characters in strings", async () => {
const badString = 'test":, wow", "test": "wow"'
const table = await config.api.table.save(
saveTableRequest({
schema: {
string: {
type: FieldType.STRING,
name: "string",
},
},
})
)

await config.api.row.save(table._id!, { string: badString })

const exportedValue = await config.api.row.exportRows(
table._id!,
{ query: {} },
RowExportFormat.CSV
)

const json = await config.api.table.csvToJson(
{
csvString: exportedValue,
},
{
status: 200,
}
)

expect(json).toHaveLength(1)
expect(json[0].string).toEqual(badString)
})

it("exported data can be re-imported", async () => {
// export all
const exportedValue = await config.api.row.exportRows(
Expand Down
6 changes: 4 additions & 2 deletions packages/server/src/utilities/csv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ export async function jsonFromCsvString(csvString: string) {
csvString
)

// By default the csvtojson library casts empty values as empty strings. This is causing issues on conversion.
// ignoreEmpty will remove the key completly if empty, so creating this empty object will ensure we return the values with the keys but empty values
// By default the csvtojson library casts empty values as empty strings. This
// is causing issues on conversion. ignoreEmpty will remove the key completly
// if empty, so creating this empty object will ensure we return the values
// with the keys but empty values
const result = await csv({ ignoreEmpty: false }).fromString(csvString)
result.forEach((r, i) => {
for (const [key] of Object.entries(r).filter(([, value]) => value === "")) {
Expand Down
Loading