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
13 changes: 12 additions & 1 deletion libs/template/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,26 @@ func validateSchema(schema *jsonschema.Schema) error {

// Reads json file at path and assigns values from the file
func (c *config) assignValuesFromFile(path string) error {
// Load the config file.
// It's valid to set additional properties in the config file that are not
// defined in the schema. They will be filtered below. Thus for the duration of
// the LoadInstance call, we disable the additional properties check,
// to allow those properties to be loaded.
c.schema.AdditionalProperties = true
configFromFile, err := c.schema.LoadInstance(path)
c.schema.AdditionalProperties = false

if err != nil {
return fmt.Errorf("failed to load config from file %s: %w", path, err)
}

// Write configs from the file to the input map, not overwriting any existing
// configurations.
for name, val := range configFromFile {
// If a property is not defined in the schema, skip it.
if _, ok := c.schema.Properties[name]; !ok {
continue
}
// If a value is already assigned, keep the original value.
if _, ok := c.values[name]; ok {
continue
}
Expand Down
11 changes: 11 additions & 0 deletions libs/template/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,17 @@ func TestTemplateConfigAssignValuesFromFileDoesNotOverwriteExistingConfigs(t *te
assert.Equal(t, "this-is-not-overwritten", c.values["string_val"])
}

func TestTemplateConfigAssignValuesFromFileFiltersPropertiesNotInTheSchema(t *testing.T) {
c := testConfig(t)

err := c.assignValuesFromFile("./testdata/config-assign-from-file-unknown-property/config.json")
assert.NoError(t, err)

// assert only the known property is loaded
assert.Len(t, c.values, 1)
assert.Equal(t, "i am a known property", c.values["string_val"])
}

func TestTemplateConfigAssignDefaultValues(t *testing.T) {
c := testConfig(t)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"unknown_prop": 123
"unknown_prop": 123,
"string_val": "i am a known property"
}