Skip to content

Commit b71a4b8

Browse files
Allow unknown properties in the config file for template initialization (#1315)
## Changes Before we would error if a property was defined in the config file, that was not defined in the schema. ## Tests Unit tests. Also manually that the e2e flow works file. Before: ``` shreyas.goenka@THW32HFW6T playground % cli bundle init default-python --config-file config.json Welcome to the default Python template for Databricks Asset Bundles! Error: failed to load config from file config.json: property include_pytho is not defined in the schema ``` After: ``` shreyas.goenka@THW32HFW6T playground % cli bundle init default-python --config-file config.json Welcome to the default Python template for Databricks Asset Bundles! Workspace to use (auto-detected, edit in 'test/databricks.yml'): https://dbc-a39a1eb1-ef95.cloud.databricks.com ✨ Your new project has been created in the 'test' directory! Please refer to the README.md file for "getting started" instructions. See also the documentation at https://docs.databricks.com/dev-tools/bundles/index.html. ```
1 parent 41239d1 commit b71a4b8

3 files changed

Lines changed: 25 additions & 2 deletions

File tree

libs/template/config.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,26 @@ func validateSchema(schema *jsonschema.Schema) error {
7070

7171
// Reads json file at path and assigns values from the file
7272
func (c *config) assignValuesFromFile(path string) error {
73-
// Load the config file.
73+
// It's valid to set additional properties in the config file that are not
74+
// defined in the schema. They will be filtered below. Thus for the duration of
75+
// the LoadInstance call, we disable the additional properties check,
76+
// to allow those properties to be loaded.
77+
c.schema.AdditionalProperties = true
7478
configFromFile, err := c.schema.LoadInstance(path)
79+
c.schema.AdditionalProperties = false
80+
7581
if err != nil {
7682
return fmt.Errorf("failed to load config from file %s: %w", path, err)
7783
}
7884

7985
// Write configs from the file to the input map, not overwriting any existing
8086
// configurations.
8187
for name, val := range configFromFile {
88+
// If a property is not defined in the schema, skip it.
89+
if _, ok := c.schema.Properties[name]; !ok {
90+
continue
91+
}
92+
// If a value is already assigned, keep the original value.
8293
if _, ok := c.values[name]; ok {
8394
continue
8495
}

libs/template/config_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,17 @@ func TestTemplateConfigAssignValuesFromFileDoesNotOverwriteExistingConfigs(t *te
5252
assert.Equal(t, "this-is-not-overwritten", c.values["string_val"])
5353
}
5454

55+
func TestTemplateConfigAssignValuesFromFileFiltersPropertiesNotInTheSchema(t *testing.T) {
56+
c := testConfig(t)
57+
58+
err := c.assignValuesFromFile("./testdata/config-assign-from-file-unknown-property/config.json")
59+
assert.NoError(t, err)
60+
61+
// assert only the known property is loaded
62+
assert.Len(t, c.values, 1)
63+
assert.Equal(t, "i am a known property", c.values["string_val"])
64+
}
65+
5566
func TestTemplateConfigAssignDefaultValues(t *testing.T) {
5667
c := testConfig(t)
5768

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
{
2-
"unknown_prop": 123
2+
"unknown_prop": 123,
3+
"string_val": "i am a known property"
34
}

0 commit comments

Comments
 (0)