riferrei / srclient

Golang Client for Schema Registry
Apache License 2.0
235 stars 70 forks source link

Using JSON with schema registry #97

Closed DenisKnezBeforeNine closed 5 months ago

DenisKnezBeforeNine commented 1 year ago

I'm using JSON for data transfer over kafka. It works great but then I wanted to add schema registry to validate the JSON. This is where I'm hoping someone could help me resolve my issue.

I see there is an AVRO example and Protobuf example and so I've looked at the AVRO example and tried that with JSON but just replace codec with JsonSchema. And I'm not able to get it to work I get the error: jsonschema: invalid jsonType: []uint8

This is the registered schema (I've checked with schema.SchemaType() and schema.Schema())

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "type": "object",
    "properties": {
      "action": {
        "type": "string"
      }
    },
    "required": [
      "action"
    ]
}

This is what I send and what I get on the consumer side

{"action":"SEND_EMAIL"}

And I've checked that with

string(kafkaMessage.Value[5:])



This is the code that is checking the json schema (I'm using the "github.com/segmentio/kafka-go" library for kafka)

// read the message
kafkaMessage, _ := r.ReadMessage(context.Background())

// print the json contents of the kafka message so I can see if the correct JSON is sent
string(kafkaMessage.Value[5:])

// extract the schema id from the kafka message
schemaID := binary.BigEndian.Uint32(kafkaMessage.Value[1:5])

// get schema with the schema id
schema, err := schemaRegistryClient.GetSchema(int(schemaID))

// print the schema type and the actual schema so I can see that it's pulling the correct schema and if it's the schema I want
fmt.Println(schema.SchemaType().String())
fmt.Println(schema.Schema())

// validate the message with the json schema
err = schema.JsonSchema().Validate(kafkaMessage.Value[5:])
if err != nil {
   // this is where the error "jsonschema: invalid jsonType: []uint8" is thrown
}


Please note this is not a copy paste of my code so if there is a syntax error it's just here not in the actual code. If you have any idea what could be wrong or if I'm using the library wrong some info/example would be great

AtakanColak commented 1 year ago

Hi @DenisKnezBeforeNine, could you investigate if the fetched json schema and the kafka message are correct? Might just be requiring you to cast into a string as well.

AtakanColak commented 5 months ago

Closing this issue due to inactivity.