Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions coderd/idpsync/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,17 @@ func (s *GroupSyncSettings) String() string {
return runtimeconfig.JSONString(s)
}

func (s *GroupSyncSettings) MarshalJSON() ([]byte, error) {
if s.Mapping == nil {
s.Mapping = make(map[string][]uuid.UUID)
}

// Aliasing the struct to avoid infinite recursion when calling json.Marshal
// on the struct itself.
type Alias GroupSyncSettings
return json.Marshal(&struct{ *Alias }{Alias: (*Alias)(s)})
}

type ExpectedGroup struct {
OrganizationID uuid.UUID
GroupID *uuid.UUID
Expand Down
44 changes: 44 additions & 0 deletions coderd/idpsync/idpsync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,57 @@ package idpsync_test

import (
"encoding/json"
"regexp"
"testing"

"github.com/stretchr/testify/require"

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

// TestMarshalJSONEmpty ensures no empty maps are marshaled as `null` in JSON.
func TestMarshalJSONEmpty(t *testing.T) {
t.Parallel()

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

output, err := json.Marshal(&idpsync.GroupSyncSettings{
RegexFilter: regexp.MustCompile(".*"),
})
require.NoError(t, err, "marshal empty group settings")
require.NotContains(t, string(output), "null")

require.JSONEq(t,
`{"field":"","mapping":{},"regex_filter":".*","auto_create_missing_groups":false}`,
string(output))
})

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

output, err := json.Marshal(&idpsync.RoleSyncSettings{})
require.NoError(t, err, "marshal empty group settings")
require.NotContains(t, string(output), "null")

require.JSONEq(t,
`{"field":"","mapping":{}}`,
string(output))
})

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

output, err := json.Marshal(&idpsync.OrganizationSyncSettings{})
require.NoError(t, err, "marshal empty group settings")
require.NotContains(t, string(output), "null")

require.JSONEq(t,
`{"field":"","mapping":{},"assign_default":false}`,
string(output))
})
}

func TestParseStringSliceClaim(t *testing.T) {
t.Parallel()

Expand Down
11 changes: 11 additions & 0 deletions coderd/idpsync/organization.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,17 @@ func (s *OrganizationSyncSettings) String() string {
return runtimeconfig.JSONString(s)
}

func (s *OrganizationSyncSettings) MarshalJSON() ([]byte, error) {
if s.Mapping == nil {
s.Mapping = make(map[string][]uuid.UUID)
}

// Aliasing the struct to avoid infinite recursion when calling json.Marshal
// on the struct itself.
type Alias OrganizationSyncSettings
return json.Marshal(&struct{ *Alias }{Alias: (*Alias)(s)})
}

// ParseClaims will parse the claims and return the list of organizations the user
// should sync to.
func (s *OrganizationSyncSettings) ParseClaims(ctx context.Context, db database.Store, mergedClaims jwt.MapClaims) ([]uuid.UUID, error) {
Expand Down
11 changes: 11 additions & 0 deletions coderd/idpsync/role.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,3 +291,14 @@ func (s *RoleSyncSettings) String() string {
}
return runtimeconfig.JSONString(s)
}

func (s *RoleSyncSettings) MarshalJSON() ([]byte, error) {
if s.Mapping == nil {
s.Mapping = make(map[string][]string)
}

// Aliasing the struct to avoid infinite recursion when calling json.Marshal
// on the struct itself.
type Alias RoleSyncSettings
return json.Marshal(&struct{ *Alias }{Alias: (*Alias)(s)})
}
3 changes: 3 additions & 0 deletions enterprise/coderd/idpsync.go
Original file line number Diff line number Diff line change
Expand Up @@ -836,6 +836,9 @@ func (api *API) idpSyncClaimFieldValues(orgID uuid.UUID, rw http.ResponseWriter,
httpapi.InternalServerError(rw, err)
return
}
if fieldValues == nil {
fieldValues = []string{}
}

httpapi.Write(ctx, rw, http.StatusOK, fieldValues)
}
Expand Down
Loading