A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/coder/coder/commit/44d46469e10a78053d28c28d6addc979326d863d below:

defensively handle nil maps and slices in marshaling (#18418) · coder/coder@44d4646 · GitHub

File tree Expand file treeCollapse file tree 5 files changed

+80

-0

lines changed

Filter options

Expand file treeCollapse file tree 5 files changed

+80

-0

lines changed Original file line number Diff line number Diff line change

@@ -274,6 +274,17 @@ func (s *GroupSyncSettings) String() string {

274 274

return runtimeconfig.JSONString(s)

275 275

}

276 276 277 +

func (s *GroupSyncSettings) MarshalJSON() ([]byte, error) {

278 +

if s.Mapping == nil {

279 +

s.Mapping = make(map[string][]uuid.UUID)

280 +

}

281 + 282 +

// Aliasing the struct to avoid infinite recursion when calling json.Marshal

283 +

// on the struct itself.

284 +

type Alias GroupSyncSettings

285 +

return json.Marshal(&struct{ *Alias }{Alias: (*Alias)(s)})

286 +

}

287 + 277 288

type ExpectedGroup struct {

278 289

OrganizationID uuid.UUID

279 290

GroupID *uuid.UUID

Original file line number Diff line number Diff line change

@@ -2,13 +2,57 @@ package idpsync_test

2 2 3 3

import (

4 4

"encoding/json"

5 +

"regexp"

5 6

"testing"

6 7 7 8

"github.com/stretchr/testify/require"

8 9 9 10

"github.com/coder/coder/v2/coderd/idpsync"

10 11

)

11 12 13 +

// TestMarshalJSONEmpty ensures no empty maps are marshaled as `null` in JSON.

14 +

func TestMarshalJSONEmpty(t *testing.T) {

15 +

t.Parallel()

16 + 17 +

t.Run("Group", func(t *testing.T) {

18 +

t.Parallel()

19 + 20 +

output, err := json.Marshal(&idpsync.GroupSyncSettings{

21 +

RegexFilter: regexp.MustCompile(".*"),

22 +

})

23 +

require.NoError(t, err, "marshal empty group settings")

24 +

require.NotContains(t, string(output), "null")

25 + 26 +

require.JSONEq(t,

27 +

`{"field":"","mapping":{},"regex_filter":".*","auto_create_missing_groups":false}`,

28 +

string(output))

29 +

})

30 + 31 +

t.Run("Role", func(t *testing.T) {

32 +

t.Parallel()

33 + 34 +

output, err := json.Marshal(&idpsync.RoleSyncSettings{})

35 +

require.NoError(t, err, "marshal empty group settings")

36 +

require.NotContains(t, string(output), "null")

37 + 38 +

require.JSONEq(t,

39 +

`{"field":"","mapping":{}}`,

40 +

string(output))

41 +

})

42 + 43 +

t.Run("Organization", func(t *testing.T) {

44 +

t.Parallel()

45 + 46 +

output, err := json.Marshal(&idpsync.OrganizationSyncSettings{})

47 +

require.NoError(t, err, "marshal empty group settings")

48 +

require.NotContains(t, string(output), "null")

49 + 50 +

require.JSONEq(t,

51 +

`{"field":"","mapping":{},"assign_default":false}`,

52 +

string(output))

53 +

})

54 +

}

55 + 12 56

func TestParseStringSliceClaim(t *testing.T) {

13 57

t.Parallel()

14 58 Original file line number Diff line number Diff line change

@@ -234,6 +234,17 @@ func (s *OrganizationSyncSettings) String() string {

234 234

return runtimeconfig.JSONString(s)

235 235

}

236 236 237 +

func (s *OrganizationSyncSettings) MarshalJSON() ([]byte, error) {

238 +

if s.Mapping == nil {

239 +

s.Mapping = make(map[string][]uuid.UUID)

240 +

}

241 + 242 +

// Aliasing the struct to avoid infinite recursion when calling json.Marshal

243 +

// on the struct itself.

244 +

type Alias OrganizationSyncSettings

245 +

return json.Marshal(&struct{ *Alias }{Alias: (*Alias)(s)})

246 +

}

247 + 237 248

// ParseClaims will parse the claims and return the list of organizations the user

238 249

// should sync to.

239 250

func (s *OrganizationSyncSettings) ParseClaims(ctx context.Context, db database.Store, mergedClaims jwt.MapClaims) ([]uuid.UUID, error) {

Original file line number Diff line number Diff line change

@@ -291,3 +291,14 @@ func (s *RoleSyncSettings) String() string {

291 291

}

292 292

return runtimeconfig.JSONString(s)

293 293

}

294 + 295 +

func (s *RoleSyncSettings) MarshalJSON() ([]byte, error) {

296 +

if s.Mapping == nil {

297 +

s.Mapping = make(map[string][]string)

298 +

}

299 + 300 +

// Aliasing the struct to avoid infinite recursion when calling json.Marshal

301 +

// on the struct itself.

302 +

type Alias RoleSyncSettings

303 +

return json.Marshal(&struct{ *Alias }{Alias: (*Alias)(s)})

304 +

}

Original file line number Diff line number Diff line change

@@ -836,6 +836,9 @@ func (api *API) idpSyncClaimFieldValues(orgID uuid.UUID, rw http.ResponseWriter,

836 836

httpapi.InternalServerError(rw, err)

837 837

return

838 838

}

839 +

if fieldValues == nil {

840 +

fieldValues = []string{}

841 +

}

839 842 840 843

httpapi.Write(ctx, rw, http.StatusOK, fieldValues)

841 844

}

You can’t perform that action at this time.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4