Skip to content

Commit

Permalink
backend: remove json schema validation from json schema serde serialize
Browse files Browse the repository at this point in the history
  • Loading branch information
bojand committed Aug 24, 2023
1 parent fe8fc56 commit a11e3ce
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 18 deletions.
38 changes: 20 additions & 18 deletions backend/pkg/serde/json_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,6 @@ func (s JsonSchemaSerde) SerializeObject(obj any, payloadType PayloadType, opts
return nil, errors.New("no schema id specified")
}

schemaRes, err := s.SchemaSvc.GetSchemaByID(so.schemaId)
if err != nil {
return nil, fmt.Errorf("getting json schema from registry '%+v': %w", so.schemaId, err)
}

var byteData []byte
switch v := obj.(type) {
case string:
Expand All @@ -90,29 +85,36 @@ func (s JsonSchemaSerde) SerializeObject(obj any, payloadType PayloadType, opts
return nil, fmt.Errorf("first byte indicates this it not valid JSON, expected brackets")
}

// validate
// Here we would get the schema by ID and validate against schema, but
// Redpanda currently does not support JSON Schema in the schema registry so we cannot do it.
// Just add the header to the payload.

header, err := appendEncode(nil, int(so.schemaId), nil)
if err != nil {
return nil, fmt.Errorf("failed encode json schema payload: %w", err)
}

binData := append(header, trimmed...)

return binData, nil
}

func (s *JsonSchemaSerde) validate(data []byte, schemaRes *schema.SchemaResponse) error {
sch, err := s.compileJSONSchema(schemaRes)
if err != nil {
return nil, fmt.Errorf("error compiling json schema: %w", err)
return fmt.Errorf("error compiling json schema: %w", err)
}

var vObj interface{}
if err := json.Unmarshal(trimmed, &vObj); err != nil {
return nil, fmt.Errorf("error validating json schema: %w", err)
if err := json.Unmarshal(data, &vObj); err != nil {
return fmt.Errorf("error validating json schema: %w", err)
}

if err = sch.Validate(vObj); err != nil {
return nil, fmt.Errorf("error validating json schema: %w", err)
return fmt.Errorf("error validating json schema: %w", err)
}

header, err := appendEncode(nil, int(so.schemaId), nil)
if err != nil {
return nil, fmt.Errorf("failed encode json schema payload: %w", err)
}

binData := append(header, trimmed...)

return binData, nil
return nil
}

func (s *JsonSchemaSerde) compileJSONSchema(schemaRes *schema.SchemaResponse) (*jsonschema.Schema, error) {
Expand Down
8 changes: 8 additions & 0 deletions backend/pkg/serde/json_schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,8 @@ func TestJsonSchemaSerde_SerializeObject(t *testing.T) {
})

t.Run("invalid schema id", func(t *testing.T) {
t.Skip("Redpanda schema registry doesn't support JSON schema")

serde := JsonSchemaSerde{SchemaSvc: schemaSvc}

b, err := serde.SerializeObject(ProductRecord{ProductID: 11, ProductName: "foo"}, PayloadTypeValue, WithSchemaID(5567))
Expand All @@ -250,6 +252,8 @@ func TestJsonSchemaSerde_SerializeObject(t *testing.T) {
})

t.Run("dynamic validation error", func(t *testing.T) {
t.Skip("Redpanda schema registry doesn't support JSON schema")

serde := JsonSchemaSerde{SchemaSvc: schemaSvc}

actualData, err := serde.SerializeObject(ProductRecord{ProductID: 11, ProductName: "foo"}, PayloadTypeValue, WithSchemaID(1000))
Expand Down Expand Up @@ -307,6 +311,8 @@ func TestJsonSchemaSerde_SerializeObject(t *testing.T) {
})

t.Run("string invalid json", func(t *testing.T) {
t.Skip("Redpanda schema registry doesn't support JSON schema")

serde := JsonSchemaSerde{SchemaSvc: schemaSvc}

b, err := serde.SerializeObject(`{"productId":11,"price":10.25}`, PayloadTypeValue, WithSchemaID(1000))
Expand Down Expand Up @@ -367,6 +373,8 @@ func TestJsonSchemaSerde_SerializeObject(t *testing.T) {
})

t.Run("byte invalid json", func(t *testing.T) {
t.Skip("Redpanda schema registry doesn't support JSON schema")

serde := JsonSchemaSerde{SchemaSvc: schemaSvc}

b, err := serde.SerializeObject([]byte(`{"productId":"11","productName":"foo","price":10.25}`), PayloadTypeValue, WithSchemaID(1000))
Expand Down

0 comments on commit a11e3ce

Please sign in to comment.