Skip to content

Commit

Permalink
Fix logic when skipping schema input
Browse files Browse the repository at this point in the history
The Required||Optional logic in schemaMap.Input was incorrect, causing
it to always request input. Fix the logic, and the associated tests
which were passing "just because".
  • Loading branch information
jbardin committed Mar 17, 2017
1 parent d55e1bc commit 592601f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
4 changes: 3 additions & 1 deletion helper/schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,9 @@ func (m schemaMap) Input(

// Skip things that don't require config, if that is even valid
// for a provider schema.
if !v.Required && !v.Optional {
// Reuiqred XOR Optional must always be true to validate, so we only
// need to check one.
if v.Optional {
continue
}

Expand Down
32 changes: 21 additions & 11 deletions helper/schema/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3173,23 +3173,17 @@ func TestSchemaMap_Input(t *testing.T) {
* String decode
*/

"uses input on optional field with no config": {
"no input on optional field with no config": {
Schema: map[string]*Schema{
"availability_zone": &Schema{
Type: TypeString,
Optional: true,
},
},

Input: map[string]string{
"availability_zone": "foo",
},

Result: map[string]interface{}{
"availability_zone": "foo",
},

Err: false,
Input: map[string]string{},
Result: map[string]interface{}{},
Err: false,
},

"input ignored when config has a value": {
Expand Down Expand Up @@ -3276,7 +3270,7 @@ func TestSchemaMap_Input(t *testing.T) {
DefaultFunc: func() (interface{}, error) {
return nil, nil
},
Optional: true,
Required: true,
},
},

Expand All @@ -3290,6 +3284,22 @@ func TestSchemaMap_Input(t *testing.T) {

Err: false,
},

"input not used when optional default function returns nil": {
Schema: map[string]*Schema{
"availability_zone": &Schema{
Type: TypeString,
DefaultFunc: func() (interface{}, error) {
return nil, nil
},
Optional: true,
},
},

Input: map[string]string{},
Result: map[string]interface{}{},
Err: false,
},
}

for i, tc := range cases {
Expand Down

0 comments on commit 592601f

Please sign in to comment.