Skip to content

Commit

Permalink
Merge pull request #433 from tigrisdata/main
Browse files Browse the repository at this point in the history
  • Loading branch information
himank authored Aug 18, 2022
2 parents 94c924f + 3675e39 commit 8c236bf
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 1 deletion.
10 changes: 10 additions & 0 deletions schema/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,15 @@ type DefaultCollection struct {
QueryableFields []*QueryableField
}

func disableAdditionalProperties(properties map[string]*jsonschema.Schema) {
for _, p := range properties {
p.AdditionalProperties = false
if len(p.Properties) > 0 {
disableAdditionalProperties(p.Properties)
}
}
}

func NewDefaultCollection(name string, id uint32, schVer int, fields []*Field, indexes *Indexes, schema jsoniter.RawMessage, searchCollectionName string) *DefaultCollection {
url := name + ".json"
compiler := jsonschema.NewCompiler()
Expand All @@ -76,6 +85,7 @@ func NewDefaultCollection(name string, id uint32, schVer int, fields []*Field, i
// Tigris doesn't allow additional fields as part of the write requests. Setting it to false ensures strict
// schema validation.
validator.AdditionalProperties = false
disableAdditionalProperties(validator.Properties)

queryableFields := buildQueryableFields(fields)

Expand Down
57 changes: 57 additions & 0 deletions schema/collection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,3 +292,60 @@ func TestCollection_SearchSchema(t *testing.T) {
require.Equal(t, expFlattenedFields[i], f.Name)
}
}

func TestCollection_AdditionalProperties(t *testing.T) {
reqSchema := []byte(`{
"title": "t1",
"properties": {
"id": {
"type": "integer"
},
"simple_object": {
"type": "object",
"properties": {
"name": { "type": "string" }
}
},
"complex_object": {
"type": "object",
"properties": {
"name": { "type": "string" },
"obj": {
"type": "object",
"properties": {
"name": { "type": "string" }
}
}
}
}
},
"primary_key": ["id"]
}`)

cases := []struct {
document []byte
expError string
}{
{
document: []byte(`{"id": 1, "simple_object": {"name": "hello", "price": 1.01}}`),
expError: "json schema validation failed for field 'simple_object' reason 'additionalProperties 'price' not allowed'",
}, {
document: []byte(`{"id": 1, "complex_object": {"name": "hello", "price": 1.01}}`),
expError: "json schema validation failed for field 'complex_object' reason 'additionalProperties 'price' not allowed'",
}, {
document: []byte(`{"id": 1, "complex_object": {"name": "hello", "obj": {"name": "hello", "price": 1.01}}}`),
expError: "json schema validation failed for field 'complex_object/obj' reason 'additionalProperties 'price' not allowed'",
},
}
for _, c := range cases {
schFactory, err := Build("t1", reqSchema)
require.NoError(t, err)
coll := NewDefaultCollection("t1", 1, 1, schFactory.Fields, schFactory.Indexes, schFactory.Schema, "t1")

dec := jsoniter.NewDecoder(bytes.NewReader(c.document))
dec.UseNumber()
var v interface{}
require.NoError(t, dec.Decode(&v))
require.Equal(t, c.expError, coll.Validate(v).Error())
}
}
1 change: 0 additions & 1 deletion server/services/v1/observability.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ func (dd Datadog) QueryTimeSeriesMetrics(ctx context.Context, req *api.QueryTime
if err != nil {
return nil, api.Errorf(api.Code_INTERNAL, "Failed to query metrics: reason = "+err.Error())
}
fmt.Println(ddQuery)
q.Add("query", ddQuery)
ddReq.URL.RawQuery = q.Encode()
ddReq.Header.Add(AcceptHeader, ApplicationJsonHeaderVal)
Expand Down

0 comments on commit 8c236bf

Please sign in to comment.