Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

fix: defensively handle nil maps and slices in marshaling#18418

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
angrycub merged 3 commits intomainfromcv-fix-bad-idp-flags-nil
Jun 17, 2025
Merged
Show file tree
Hide file tree
Changes fromall 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 deletionscoderd/idpsync/group.go
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionscoderd/idpsync/idpsync_test.go
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionscoderd/idpsync/organization.go
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionscoderd/idpsync/role.go
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionsenterprise/coderd/idpsync.go
View file
Open in desktop
Original file line numberDiff line numberDiff 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

[8]ページ先頭

©2009-2025 Movatter.jp