MM-68276: Apply default values for plugin settings inside sections (#36119)

* MM-68276: Apply default values for plugin settings inside sections

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

* Revert work-around from #36056

This is no longer needed with the previous fix in place.

---------

Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-authored-by: Mattermost Build <build@mattermost.com>
This commit is contained in:
Alejandro García Montoro
2026-04-16 15:45:09 +00:00
committed by GitHub
co-authored by Claude Opus 4.6 Mattermost Build
parent 983ea5a49f
commit dc7241941e
3 changed files with 101 additions and 9 deletions
@@ -17,18 +17,10 @@ describe('Link tooltips', () => {
cy.shouldNotRunOnCloudEdition();
cy.shouldHavePluginUploadEnabled();
// # Set plugin settings, including demo plugin defaults so that
// OnConfigurationChange can create the demo user with a valid email
// # Set plugin settings
const newSettings = {
PluginSettings: {
Enable: true,
Plugins: {
[demoPlugin.id]: {
channelname: 'demo_plugin',
username: 'demo_plugin',
lastname: 'Plugin User',
},
},
},
ServiceSettings: {
EnableGifPicker: true,
+5
View File
@@ -55,6 +55,11 @@ func (api *PluginAPI) LoadPluginConfiguration(dest any) error {
for _, setting := range api.manifest.SettingsSchema.Settings {
finalConfig[strings.ToLower(setting.Key)] = setting.Default
}
for _, section := range api.manifest.SettingsSchema.Sections {
for _, setting := range section.Settings {
finalConfig[strings.ToLower(setting.Key)] = setting.Default
}
}
}
// If we have settings given we override the defaults with them
+95
View File
@@ -982,6 +982,101 @@ func TestPluginAPILoadPluginConfigurationDefaults(t *testing.T) {
require.NoError(t, err)
}
func TestPluginAPILoadPluginConfigurationDefaultsFromSections(t *testing.T) {
mainHelper.Parallel(t)
th := Setup(t)
var pluginJson map[string]any
err := json.Unmarshal([]byte(`{"mysectionstringsetting": "override"}`), &pluginJson)
require.NoError(t, err)
th.App.UpdateConfig(func(cfg *model.Config) {
cfg.PluginSettings.Plugins["testsectiondefaults"] = pluginJson
})
manifest := &model.Manifest{
Id: "testsectiondefaults",
SettingsSchema: &model.PluginSettingsSchema{
Sections: []*model.PluginSettingsSection{
{
Key: "section1",
Settings: []*model.PluginSetting{
{Key: "MySectionStringSetting", Type: "text", Default: "notthis"},
{Key: "MySectionIntSetting", Type: "text", Default: float64(42)},
{Key: "MySectionBoolSetting", Type: "bool", Default: true},
},
},
},
},
}
api := NewPluginAPI(th.App, th.Context, manifest)
var dest struct {
MySectionStringSetting string
MySectionIntSetting int
MySectionBoolSetting bool
}
err = api.LoadPluginConfiguration(&dest)
require.NoError(t, err)
assert.Equal(t, "override", dest.MySectionStringSetting) // saved config overrides default
assert.Equal(t, 42, dest.MySectionIntSetting) // default applied from section
assert.True(t, dest.MySectionBoolSetting) // default applied from section
}
func TestPluginAPILoadPluginConfigurationDefaultsMixed(t *testing.T) {
mainHelper.Parallel(t)
th := Setup(t)
var pluginJson map[string]any
err := json.Unmarshal([]byte(`{"toplevelsetting": "saved_value"}`), &pluginJson)
require.NoError(t, err)
th.App.UpdateConfig(func(cfg *model.Config) {
cfg.PluginSettings.Plugins["testmixeddefaults"] = pluginJson
})
manifest := &model.Manifest{
Id: "testmixeddefaults",
SettingsSchema: &model.PluginSettingsSchema{
Settings: []*model.PluginSetting{
{Key: "TopLevelSetting", Type: "text", Default: "top_default"},
{Key: "TopLevelBool", Type: "bool", Default: true},
},
Sections: []*model.PluginSettingsSection{
{
Key: "section1",
Settings: []*model.PluginSetting{
{Key: "SectionSetting", Type: "text", Default: "section_default"},
{Key: "SectionInt", Type: "text", Default: float64(99)},
},
},
},
},
}
api := NewPluginAPI(th.App, th.Context, manifest)
var dest struct {
TopLevelSetting string
TopLevelBool bool
SectionSetting string
SectionInt int
}
err = api.LoadPluginConfiguration(&dest)
require.NoError(t, err)
// Top-level: saved config overrides default
assert.Equal(t, "saved_value", dest.TopLevelSetting)
// Top-level: default applied
assert.True(t, dest.TopLevelBool)
// Section: default applied
assert.Equal(t, "section_default", dest.SectionSetting)
// Section: default applied
assert.Equal(t, 99, dest.SectionInt)
}
func TestPluginAPIGetPlugins(t *testing.T) {
mainHelper.Parallel(t)
th := Setup(t)