-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Fix type name clashing & nullability issues (#39)
Blocked by: * invopop/jsonschema#109 * invopop/jsonschema#110
- Loading branch information
1 parent
b537cc6
commit 6940313
Showing
6 changed files
with
214 additions
and
5 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package jsonschema | ||
|
||
import ( | ||
"encoding/json" | ||
"net/url" | ||
"strings" | ||
|
||
"github.com/invopop/jsonschema" | ||
) | ||
|
||
func Sanitize(sc *jsonschema.Schema) error { | ||
data, err := json.MarshalIndent(sc, "", " ") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
refs := make(map[string]bool) | ||
for _, line := range strings.Split(string(data), "\n") { | ||
line = strings.TrimSpace(line) | ||
const refPfx = `"$ref": ` | ||
if strings.HasPrefix(line, refPfx) { | ||
refs[unescapeRef(strings.Trim(strings.TrimSuffix(strings.TrimPrefix(line, refPfx), ","), `"`))] = true | ||
} | ||
} | ||
|
||
for key := range sc.Definitions { | ||
if _, ok := refs[key]; !ok { | ||
delete(sc.Definitions, key) | ||
} | ||
} | ||
|
||
for p := sc.Properties.Oldest(); p != nil; p = p.Next() { | ||
if err := Sanitize(p.Value); err != nil { | ||
return err | ||
} | ||
} | ||
for _, def := range sc.Definitions { | ||
if err := Sanitize(def); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
func unescapeRef(ref string) string { | ||
ref = strings.TrimPrefix(ref, "#/$defs/") | ||
|
||
var err error | ||
ref, err = url.PathUnescape(ref) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
ref = strings.ReplaceAll(ref, "~1", "/") | ||
ref = strings.ReplaceAll(ref, "~0", "~") | ||
return ref | ||
} |
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 jsonschema | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/invopop/jsonschema" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestSanitize(t *testing.T) { | ||
sc := &jsonschema.Schema{ | ||
Properties: jsonschema.NewProperties(), | ||
} | ||
|
||
sc.Definitions = jsonschema.Definitions{ | ||
"key": new(jsonschema.Schema), | ||
} | ||
|
||
require.NoError(t, Sanitize(sc)) | ||
require.Empty(t, sc.Definitions) | ||
} |
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,56 @@ | ||
package jsonschema | ||
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
|
||
"github.com/cloudquery/plugin-sdk/v4/plugin" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
type TestCase struct { | ||
Name string | ||
Spec string | ||
Err bool | ||
} | ||
|
||
func TestJSONSchema(t *testing.T, schema string, cases []TestCase) { | ||
t.Helper() | ||
|
||
validator, err := plugin.JSONSchemaValidator(schema) | ||
require.NoError(t, err) | ||
|
||
for _, tc := range cases { | ||
t.Run(tc.Name, func(t *testing.T) { | ||
var v any | ||
require.NoErrorf(t, json.Unmarshal([]byte(tc.Spec), &v), "failed input:\n%s\n", tc.Spec) | ||
err := validator.Validate(v) | ||
if tc.Err { | ||
require.Errorf(t, err, "failed input:\n%s\n", tc.Spec) | ||
} else { | ||
require.NoErrorf(t, err, "failed input:\n%s\n", tc.Spec) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func WithRemovedKeys(t *testing.T, val any, keys ...string) string { | ||
data, err := json.Marshal(val) | ||
require.NoError(t, err) | ||
|
||
var m any | ||
require.NoError(t, json.Unmarshal(data, &m)) | ||
|
||
switch m := m.(type) { | ||
case map[string]any: | ||
for _, k := range keys { | ||
delete(m, k) | ||
} | ||
default: | ||
t.Fatalf("failed to remove JSON keys from value of type %T", m) | ||
} | ||
|
||
data, err = json.MarshalIndent(m, "", " ") | ||
require.NoError(t, err) | ||
return string(data) | ||
} |