-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add tests, that verify contents of the schema * add test for Swagger/OpenAPI based schemas * add fetching schema definition either from `value` or `definitions` based on content type * add Json supress diff function, that allows ignoring insignificant changes, e.g. in white spaces * use Json supress diff function when the content type is json based
- Loading branch information
Showing
5 changed files
with
129 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
azurerm/internal/services/apimanagement/testdata/api_management_api_schema_swagger.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"schema-bug-example":{"properties":{"Field2":{"description":"Field2","type":"string"},"field1":{"description":"Field1","type":"string"}},"required":["field1","Field2"],"type":"object"}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package suppress | ||
|
||
import ( | ||
"encoding/json" | ||
"reflect" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/helper/schema" | ||
) | ||
|
||
func JsonDiff(_, old, new string, _ *schema.ResourceData) bool { | ||
if old == new { | ||
return true | ||
} | ||
var oldJson interface{} | ||
var newJson interface{} | ||
|
||
json.Unmarshal([]byte(old), &oldJson) | ||
json.Unmarshal([]byte(new), &newJson) | ||
|
||
return reflect.DeepEqual(oldJson, newJson) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package suppress | ||
|
||
import "testing" | ||
|
||
func TestJsonDiff(t *testing.T) { | ||
cases := []struct { | ||
Name string | ||
StringA string | ||
StringB string | ||
Suppress bool | ||
}{ | ||
{ | ||
Name: "empty", | ||
StringA: "", | ||
StringB: "", | ||
Suppress: true, | ||
}, | ||
{ | ||
Name: "simple same object", | ||
StringA: "{\"field\": \"value\"}", | ||
StringB: "{\"field\": \"value\"}", | ||
Suppress: true, | ||
}, | ||
{ | ||
Name: "simple object whitespace diff", | ||
StringA: "{\n\"field\": \"value\"\n}", | ||
StringB: "{\"field\": \"value\"}", | ||
Suppress: false, | ||
}, | ||
} | ||
|
||
for _, tc := range cases { | ||
t.Run(tc.Name, func(t *testing.T) { | ||
if JsonDiff("test", tc.StringA, tc.StringB, nil) != tc.Suppress { | ||
t.Fatalf("Expected JsonDiff to return %t for '%q' == '%q'", tc.Suppress, tc.StringA, tc.StringB) | ||
} | ||
}) | ||
} | ||
} |