Skip to content

Commit fc53d02

Browse files
committed
fix(api): correctly serialize user roles for database operations
User creation was failing with a JSON syntax error because the `roles` field (`List<String>`) was not being correctly serialized into a JSON array string for the database's `JSONB` column. This change updates the `userRepository`'s configuration to use a custom `toJson` function. This function explicitly JSON-encodes the `roles` list before it's sent to the database client, resolving the error at the source without altering the generic repository or the User model.
1 parent b007772 commit fc53d02

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

lib/src/config/app_dependencies.dart

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import 'dart:async';
2+
import 'dart:convert';
23

34
import 'package:ht_api/src/config/database_connection.dart';
45
import 'package:ht_api/src/rbac/permission_service.dart';
@@ -124,7 +125,13 @@ class AppDependencies {
124125
connection,
125126
'users',
126127
User.fromJson,
127-
(u) => u.toJson(),
128+
(user) {
129+
// The `roles` field is a List<String>, but the database expects a
130+
// JSONB array. We must explicitly encode it.
131+
final json = user.toJson();
132+
json['roles'] = jsonEncode(json['roles']);
133+
return json;
134+
},
128135
);
129136
userAppSettingsRepository = _createRepository(
130137
connection,

0 commit comments

Comments
 (0)