From 843b85ae8150214bf37e31c48c8e608aaac1405f Mon Sep 17 00:00:00 2001 From: Flo <53355483+Flo4604@users.noreply.github.com> Date: Thu, 31 Jul 2025 15:55:05 +0200 Subject: [PATCH 1/4] fix omitting array vs null --- go/apps/api/routes/v2_keys_get_key/handler.go | 20 +++++++++++------- go/apps/api/routes/v2_keys_whoami/handler.go | 21 ++++++++++++------- 2 files changed, 27 insertions(+), 14 deletions(-) diff --git a/go/apps/api/routes/v2_keys_get_key/handler.go b/go/apps/api/routes/v2_keys_get_key/handler.go index 0710b3f3f0..9975163ddb 100644 --- a/go/apps/api/routes/v2_keys_get_key/handler.go +++ b/go/apps/api/routes/v2_keys_get_key/handler.go @@ -311,7 +311,9 @@ func (h *Handler) Handle(ctx context.Context, s *zen.Session) error { } } - k.Ratelimits = ptr.P(ratelimitsResponse) + if len(ratelimitsResponse) > 0 { + k.Ratelimits = ptr.P(ratelimitsResponse) + } if key.Meta.Valid { err = json.Unmarshal([]byte(key.Meta.String), &k.Meta) @@ -330,7 +332,9 @@ func (h *Handler) Handle(ctx context.Context, s *zen.Session) error { return fault.Wrap(err, fault.Code(codes.App.Internal.UnexpectedError.URN()), fault.Internal("unable to find permissions for key"), fault.Public("Could not load permissions for key.")) } - k.Permissions = ptr.P(permissionSlugs) + if len(permissionSlugs) > 0 { + k.Permissions = ptr.P(permissionSlugs) + } // Get roles for the key roles, err := db.Query.ListRolesByKeyID(ctx, h.DB.RO(), k.KeyId) @@ -339,12 +343,14 @@ func (h *Handler) Handle(ctx context.Context, s *zen.Session) error { fault.Internal("unable to find roles for key"), fault.Public("Could not load roles for key.")) } - roleNames := make([]string, len(roles)) - for i, role := range roles { - roleNames[i] = role.Name - } + if len(roles) > 0 { + roleNames := make([]string, len(roles)) + for i, role := range roles { + roleNames[i] = role.Name + } - k.Roles = ptr.P(roleNames) + k.Roles = ptr.P(roleNames) + } return s.JSON(http.StatusOK, Response{ Meta: openapi.Meta{ diff --git a/go/apps/api/routes/v2_keys_whoami/handler.go b/go/apps/api/routes/v2_keys_whoami/handler.go index 6f7f94c610..198d06724f 100644 --- a/go/apps/api/routes/v2_keys_whoami/handler.go +++ b/go/apps/api/routes/v2_keys_whoami/handler.go @@ -239,7 +239,9 @@ func (h *Handler) Handle(ctx context.Context, s *zen.Session) error { } } - k.Ratelimits = ptr.P(ratelimitsResponse) + if len(ratelimitsResponse) > 0 { + k.Ratelimits = ptr.P(ratelimitsResponse) + } if key.Meta.Valid { err = json.Unmarshal([]byte(key.Meta.String), &k.Meta) @@ -258,7 +260,10 @@ func (h *Handler) Handle(ctx context.Context, s *zen.Session) error { return fault.Wrap(err, fault.Code(codes.App.Internal.UnexpectedError.URN()), fault.Internal("unable to find permissions for key"), fault.Public("Could not load permissions for key.")) } - k.Permissions = ptr.P(permissionSlugs) + + if len(permissionSlugs) > 0 { + k.Permissions = ptr.P(permissionSlugs) + } // Get roles for the key roles, err := db.Query.ListRolesByKeyID(ctx, h.DB.RO(), k.KeyId) @@ -267,12 +272,14 @@ func (h *Handler) Handle(ctx context.Context, s *zen.Session) error { fault.Internal("unable to find roles for key"), fault.Public("Could not load roles for key.")) } - roleNames := make([]string, len(roles)) - for i, role := range roles { - roleNames[i] = role.Name - } + if len(roles) > 0 { + roleNames := make([]string, len(roles)) + for i, role := range roles { + roleNames[i] = role.Name + } - k.Roles = ptr.P(roleNames) + k.Roles = ptr.P(roleNames) + } return s.JSON(http.StatusOK, Response{ Meta: openapi.Meta{ From 6f3bdf528c31e261904280bfc04e7a464c6258ee Mon Sep 17 00:00:00 2001 From: "autofix-ci[bot]" <114827586+autofix-ci[bot]@users.noreply.github.com> Date: Thu, 31 Jul 2025 14:06:44 +0000 Subject: [PATCH 2/4] [autofix.ci] apply automated fixes --- .../src/routes/v1_migrations_createKey.happy.test.ts | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/apps/api/src/routes/v1_migrations_createKey.happy.test.ts b/apps/api/src/routes/v1_migrations_createKey.happy.test.ts index bcaf420fff..0bd991ddb5 100644 --- a/apps/api/src/routes/v1_migrations_createKey.happy.test.ts +++ b/apps/api/src/routes/v1_migrations_createKey.happy.test.ts @@ -480,10 +480,12 @@ test("an error rolls back and does not create any keys", async (t) => { // add a duplicate req.push(req[0]); - - const res = await h.post({ + const res = await h.post< + V1MigrationsCreateKeysRequest, + { + error: { code: string }; + } + >({ url: "/v1/migrations.createKeys", headers: { "Content-Type": "application/json", From 6dcd2683b1135fb1977d3a94e626e6f153a0e2d8 Mon Sep 17 00:00:00 2001 From: Flo <53355483+Flo4604@users.noreply.github.com> Date: Thu, 31 Jul 2025 16:47:09 +0200 Subject: [PATCH 3/4] fix flakey test --- .../v2_identities_list_identities/200_test.go | 33 +++++++++++++------ 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/go/apps/api/routes/v2_identities_list_identities/200_test.go b/go/apps/api/routes/v2_identities_list_identities/200_test.go index 33b22cd37b..c61b38e396 100644 --- a/go/apps/api/routes/v2_identities_list_identities/200_test.go +++ b/go/apps/api/routes/v2_identities_list_identities/200_test.go @@ -13,6 +13,7 @@ import ( "github.com/stretchr/testify/require" handler "github.com/unkeyed/unkey/go/apps/api/routes/v2_identities_list_identities" "github.com/unkeyed/unkey/go/pkg/db" + "github.com/unkeyed/unkey/go/pkg/ptr" "github.com/unkeyed/unkey/go/pkg/testutil" "github.com/unkeyed/unkey/go/pkg/uid" ) @@ -348,19 +349,31 @@ func TestSuccess(t *testing.T) { // If we have results, verify the structure of an identity if len(res.Body.Data) > 0 { - // Check the first identity - identity := res.Body.Data[0] + // Check all identities + for _, identity := range res.Body.Data { + // ID fields should never be empty + require.NotEmpty(t, identity.ExternalId, "External ID should not be empty") + + // Meta might be nil if none set + if identity.Meta != nil { + // If present, should be a valid map + require.NotNil(t, *identity.Meta, "Meta should be a valid map") + } - // ID fields should never be empty - require.NotEmpty(t, identity.ExternalId, "External ID should not be empty") + dbIdentity, err := db.Query.FindIdentity(ctx, h.DB.RW(), db.FindIdentityParams{WorkspaceID: workspaceID, Identity: identity.ExternalId, Deleted: false}) + require.NoError(t, err) + require.NotNil(t, dbIdentity, "Identity should be found in the database") - // Meta might be nil if none set - if identity.Meta != nil { - // If present, should be a valid map - require.NotNil(t, *identity.Meta, "Meta should be a valid map") - } + identityRatelimits, err := db.Query.ListIdentityRatelimits(ctx, h.DB.RW(), sql.NullString{Valid: true, String: dbIdentity.ID}) + require.NoError(t, err) - require.NotNil(t, identity.Ratelimits, "Ratelimits should be set") + if len(identityRatelimits) == 0 { + require.Nil(t, identity.Ratelimits, "Ratelimits should be nil") + } else { + require.NotNil(t, identity.Ratelimits, "Ratelimits should be set") + require.Len(t, ptr.SafeDeref(identity.Ratelimits), len(identityRatelimits), "Ratelimits should match the database") + } + } } }) } From 07c7d892abd4f42ea7a284549642616d42758b83 Mon Sep 17 00:00:00 2001 From: Flo <53355483+Flo4604@users.noreply.github.com> Date: Thu, 31 Jul 2025 16:51:58 +0200 Subject: [PATCH 4/4] fix flakey test --- .../v2_identities_list_identities/200_test.go | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/go/apps/api/routes/v2_identities_list_identities/200_test.go b/go/apps/api/routes/v2_identities_list_identities/200_test.go index c61b38e396..61991cae7c 100644 --- a/go/apps/api/routes/v2_identities_list_identities/200_test.go +++ b/go/apps/api/routes/v2_identities_list_identities/200_test.go @@ -354,12 +354,6 @@ func TestSuccess(t *testing.T) { // ID fields should never be empty require.NotEmpty(t, identity.ExternalId, "External ID should not be empty") - // Meta might be nil if none set - if identity.Meta != nil { - // If present, should be a valid map - require.NotNil(t, *identity.Meta, "Meta should be a valid map") - } - dbIdentity, err := db.Query.FindIdentity(ctx, h.DB.RW(), db.FindIdentityParams{WorkspaceID: workspaceID, Identity: identity.ExternalId, Deleted: false}) require.NoError(t, err) require.NotNil(t, dbIdentity, "Identity should be found in the database") @@ -373,6 +367,13 @@ func TestSuccess(t *testing.T) { require.NotNil(t, identity.Ratelimits, "Ratelimits should be set") require.Len(t, ptr.SafeDeref(identity.Ratelimits), len(identityRatelimits), "Ratelimits should match the database") } + + if len(dbIdentity.Meta) > 0 { + raw, err := json.Marshal(identity.Meta) + require.NoError(t, err) + require.NotNil(t, identity.Meta, "Meta should be set") + require.JSONEq(t, string(raw), string(dbIdentity.Meta), "Meta should match the database") + } } } })