Prisma Client Dart is an auto-generated type-safe ORM. It uses Prisma Engine as the data access layer and is as consistent as possible with the Prisma Client JS/TS APIs.
The toJson() method generated for classes containing DateTime and Enum fields does not encode these fields correctly for HTTP JSON responses, resulting in encoding errors.
Error Details
Error with Enum
When attempting to encode an Enum field, the following error is thrown:
ERROR - 2024-05-28 13:04:13.656400
GET /users/BIHTcphhQPwPzto
Error thrown by handler.
Converting object to an encodable object failed: Instance of 'Role'
dart:convert jsonEncode
package:dart_frog/src/response.dart 70:32 new Response.json
routes/users/[id].dart 18:23 onRequest
Error with DateTime
When attempting to encode a DateTime field, the following error is thrown:
ERROR - 2024-05-28 13:26:45.489214
GET /users/BIHTcphhQPwPzto
Error thrown by handler.
Converting object to an encodable object failed: Instance of 'DateTime'
dart:convert jsonEncode
package:dart_frog/src/response.dart 70:32 new Response.json
routes/users/[id].dart 18:23 onRequest
Output of Generated Model Class
The current output of the generated toJson() method is as follows:
1. Create Prisma schema with datasource Postgres along with User model:
model User{
id Int @id @default(autoincrement())
mobile String @unique
email String?
role Role @default(USER)
name String?
created_at DateTime @default(now()) @db.Timestamptz(0)
updated_at DateTime @default(now()) @db.Timestamptz(0)
@@map("users")
}
enum Role {
ADMIN
USER
}
2. Using dart_frog to Create a Server:
Example: A Route to Fetch User Data
Create route in the routes folder: users/[id].dart
Issue
The
toJson()
method generated for classes containingDateTime
andEnum
fields does not encode these fields correctly for HTTP JSON responses, resulting in encoding errors.Error Details
Error with Enum
When attempting to encode an
Enum
field, the following error is thrown:Error with DateTime
When attempting to encode a
DateTime
field, the following error is thrown:Output of Generated Model Class
The current output of the generated
toJson()
method is as follows:Expected Behavior
The
toJson()
method should correctly encodeDateTime
fields usingtoIso8601String()
andEnum
fields using their string representations.The expected output of the generated toJson() method should be:
Steps to Reproduce Issue (Example)
1. Create Prisma schema with datasource Postgres along with User model:
2. Using dart_frog to Create a Server:
Example: A Route to Fetch User Data
Create route in the routes folder: users/[id].dart
controller
print of data (i.e user.toJson()) :
Environment