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(client): Serialize all Floats in exponential notation #15879

Merged
merged 9 commits into from
Oct 19, 2022
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ query {
age_gt: 10123123123
id_endsWith: "veryLongNameGoIntoaNewLineNow@gmail.com"
name_contains: "hans"
name_gt: 2020123100000023
name_gt: 2131203912039123
name_in: ["hans"]
AND: [
{
Expand Down Expand Up @@ -541,7 +541,7 @@ Invalid \`prisma.findManyUser()\` invocation:
~~~~~~~~~~~
name_contains: 'hans',
~~~~~~~~~~~~~
name_gt: 2020123100000023,
name_gt: 2131203912039123,
~~~~~~~
name_in: [
~~~~~~~
Expand Down Expand Up @@ -827,7 +827,7 @@ Invalid \`prisma.findManyUser()\` invocation:
~~~~~~~~~~~
name_contains: 'hans',
~~~~~~~~~~~~~
name_gt: 2020123100000023,
name_gt: 2131203912039123,
~~~~~~~
name_in: [
~~~~~~~
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ describe('minimal atomic update transformation', () => {
mutation {
updateOneUser(
data: {
countFloat: 3.1415926
countFloat: 3.1415926e+0
countInt1: 3
}
) {
Expand Down
82 changes: 82 additions & 0 deletions packages/client/src/__tests__/query/floats.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { getDMMF } from '../../generation/getDMMF'
import { DMMFClass, makeDocument, transformDocument } from '../../runtime'

const datamodel = /* Prisma */ `
datasource my_db {
provider = "postgres"
url = env("POSTGRES_URL")
}

model Floats {
id Int @id
value Float
}
`

let dmmf

function getTransformedDocument(select) {
const document = makeDocument({
dmmf,
select,
rootTypeName: 'mutation',
rootField: 'createOneFloats',
})
return String(transformDocument(document))
}

beforeAll(async () => {
dmmf = new DMMFClass(await getDMMF({ datamodel }))
})

test('serializes floats in exponential notation', () => {
const largeInt = getTransformedDocument({
data: {
value: 100_000_000_000_000_000_000,
},
})

expect(largeInt).toMatchInlineSnapshot(`
mutation {
createOneFloats(data: {
value: 1e+20
}) {
id
value
}
}
`)

const negativeInt = getTransformedDocument({
data: {
value: Number.MIN_SAFE_INTEGER,
},
})

expect(negativeInt).toMatchInlineSnapshot(`
mutation {
createOneFloats(data: {
value: -9.007199254740991e+15
}) {
id
value
}
}
`)

const otherFloat = getTransformedDocument({
data: {
value: 13.37,
},
})
expect(otherFloat).toMatchInlineSnapshot(`
mutation {
createOneFloats(data: {
value: 1.337e+1
}) {
id
value
}
}
`)
})
4 changes: 2 additions & 2 deletions packages/client/src/__tests__/scalarListCreate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ describe('scalar where transformation', () => {
}
}
someFloats: {
set: [1, 1.2]
set: [1e+0, 1.2e+0]
}
}) {
id
Expand Down Expand Up @@ -112,7 +112,7 @@ describe('scalar where transformation', () => {
id: 5
}
}
someFloats: [1, 1.2]
someFloats: [1e+0, 1.2e+0]
}) {
id
name
Expand Down
4 changes: 4 additions & 0 deletions packages/client/src/runtime/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,10 @@ function stringify(value: any, inputType?: DMMF.SchemaArgInputType) {
return value
}

if (typeof value === 'number' && inputType?.type === 'Float') {
return value.toExponential()
}

return JSON.stringify(value, null, 2)
}

Expand Down
24 changes: 24 additions & 0 deletions packages/client/tests/functional/large-floats/_matrix.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { defineMatrix } from '../_utils/defineMatrix'

export default defineMatrix(() => [
[
{
provider: 'sqlite',
},
{
provider: 'postgresql',
},
{
provider: 'mysql',
},
{
provider: 'mongodb',
},
{
provider: 'cockroachdb',
},
{
provider: 'sqlserver',
},
],
])
20 changes: 20 additions & 0 deletions packages/client/tests/functional/large-floats/prisma/_schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { idForProvider } from '../../_utils/idForProvider'
import testMatrix from '../_matrix'

export default testMatrix.setupSchema(({ provider }) => {
return /* Prisma */ `
generator client {
provider = "prisma-client-js"
}

datasource db {
provider = "${provider}"
url = env("DATABASE_URI_${provider}")
}

model Floats {
id ${idForProvider(provider)}
value Float
}
`
})
32 changes: 32 additions & 0 deletions packages/client/tests/functional/large-floats/tests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// @ts-ignore
import type { PrismaClient } from '@prisma/client'

import testMatrix from './_matrix'

declare let prisma: PrismaClient

testMatrix.setupTestSuite(() => {
test('floats', async () => {
const largeFloat = await prisma.floats.create({
data: { value: 1e20 },
})
const negativeFloat = await prisma.floats.create({
data: { value: -1e20 },
})
const largeInteger = await prisma.floats.create({
data: { value: Number.MAX_SAFE_INTEGER },
})
const negativeInteger = await prisma.floats.create({
data: { value: Number.MIN_SAFE_INTEGER },
})
const otherFloat = await prisma.floats.create({
data: { value: 13.37 },
})

expect(largeFloat.value).toEqual(1e20)
expect(negativeFloat.value).toEqual(-1e20)
expect(largeInteger.value).toEqual(Number.MAX_SAFE_INTEGER)
expect(negativeInteger.value).toEqual(Number.MIN_SAFE_INTEGER)
expect(otherFloat.value).toEqual(13.37)
})
})
3 changes: 2 additions & 1 deletion packages/internals/src/utils/jestSnapshotSerializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ function normalizeBinaryFilePath(str) {
}

function normalizeMigrateTimestamps(str) {
return str.replace(/\d{14}/g, '20201231000000')
return str.replace(/(?<!\d)\d{14}(?!\d)/g, '20201231000000')
// return str.replace(/\d{14}/g, '20201231000000')
SevInf marked this conversation as resolved.
Show resolved Hide resolved
}

function normalizeDbUrl(str) {
Expand Down