diff --git a/index.js b/index.js index 28d61f73..81660349 100644 --- a/index.js +++ b/index.js @@ -7,6 +7,20 @@ module.exports.parse = async ({ message, defaultSchemaFormat }) => { message['x-parser-original-payload'] = message.payload; message.payload = transformed; delete message.schemaFormat; + + // remove that function when https://github.com/asyncapi/spec/issues/622 will be introduced in AsyncAPI spec + async function handleKafkaProtocolKey() { + if (message.bindings && message.bindings.kafka) { + const key = message.bindings.kafka.key; + if (key) { + const bindingsTransformed = await avroToJsonSchema(key); + message['x-parser-original-bindings-kafka-key'] = key; + message.bindings.kafka.key = bindingsTransformed; + } + } + } + + await handleKafkaProtocolKey(); }; module.exports.getMimeTypes = () => { diff --git a/tests/asyncapi-avro-1.9.0-bindings.yaml b/tests/asyncapi-avro-1.9.0-bindings.yaml new file mode 100644 index 00000000..10eba82e --- /dev/null +++ b/tests/asyncapi-avro-1.9.0-bindings.yaml @@ -0,0 +1,19 @@ +asyncapi: 2.0.0 +info: + title: My API + version: '1.0.0' +channels: + mychannel: + publish: + message: + schemaFormat: application/vnd.apache.avro;version=1.9.0 + payload: + $ref: 'schemas/Person-1.9.0-namespace.avsc' + bindings: + kafka: + key: + $ref: 'schemas/Person-1.9.0-namespace.avsc' + mqtt: + x-test: + type: string + diff --git a/tests/parse.test.js b/tests/parse.test.js index 68557317..048ef5e4 100644 --- a/tests/parse.test.js +++ b/tests/parse.test.js @@ -12,6 +12,9 @@ const outputWithAvro190 = '{"asyncapi":"2.0.0","info":{"title":"My API","version const inputWithAvro190WithNamespace = fs.readFileSync(path.resolve(__dirname, './asyncapi-avro-1.9.0-namespace.yaml'), 'utf8'); const outputWithAvro190ithNamespace = '{"asyncapi":"2.0.0","info":{"title":"My API","version":"1.0.0"},"channels":{"mychannel":{"publish":{"message":{"payload":{"type":"object","properties":{"name":{"type":"string","examples":["Donkey"],"x-parser-schema-id":""},"age":{"oneOf":[{"type":"integer","minimum":-2147483648,"maximum":2147483647,"examples":[123],"x-parser-schema-id":""},{"type":"null","x-parser-schema-id":""}],"default":null,"x-parser-schema-id":""},"favoriteProgrammingLanguage":{"type":"string","enum":["JS","Java","Go","Rust","C"],"default":"JS","x-parser-schema-id":""},"address":{"type":"object","properties":{"zipcode":{"type":"integer","minimum":-2147483648,"maximum":2147483647,"examples":[53003],"x-parser-schema-id":""}},"x-parser-schema-id":""},"someid":{"type":"string","x-parser-schema-id":""}},"x-parser-schema-id":"com.company.Person"},"x-parser-original-schema-format":"application/vnd.apache.avro;version=1.9.0","x-parser-original-payload":{"name":"Person","namespace":"com.company","type":"record","fields":[{"name":"name","type":"string","example":"Donkey"},{"name":"age","type":["null","int"],"default":null,"example":"123"},{"name":"favoriteProgrammingLanguage","type":{"name":"ProgrammingLanguage","type":"enum","symbols":["JS","Java","Go","Rust","C"],"default":"JS"}},{"name":"address","type":{"name":"Address","type":"record","fields":[{"name":"zipcode","type":"int","example":"53003"}]}},{"name":"someid","type":"uuid"}]},"schemaFormat":"application/vnd.aai.asyncapi;version=2.0.0","x-parser-message-parsed":true,"x-parser-message-name":""}}}},"x-parser-spec-parsed":true}'; +const inputWithAvro190WithBindings = fs.readFileSync(path.resolve(__dirname, './asyncapi-avro-1.9.0-bindings.yaml'), 'utf8'); +const outputWithAvro190WithBindings = '{"asyncapi":"2.0.0","info":{"title":"My API","version":"1.0.0"},"channels":{"mychannel":{"publish":{"message":{"payload":{"type":"object","properties":{"name":{"type":"string","examples":["Donkey"],"x-parser-schema-id":""},"age":{"oneOf":[{"type":"integer","minimum":-2147483648,"maximum":2147483647,"examples":[123],"x-parser-schema-id":""},{"type":"null","x-parser-schema-id":""}],"default":null,"x-parser-schema-id":""},"favoriteProgrammingLanguage":{"type":"string","enum":["JS","Java","Go","Rust","C"],"default":"JS","x-parser-schema-id":""},"address":{"type":"object","properties":{"zipcode":{"type":"integer","minimum":-2147483648,"maximum":2147483647,"examples":[53003],"x-parser-schema-id":""}},"x-parser-schema-id":""},"someid":{"type":"string","x-parser-schema-id":""}},"x-parser-schema-id":"com.company.Person"},"bindings":{"kafka":{"key":{"type":"object","properties":{"name":{"type":"string","examples":["Donkey"]},"age":{"oneOf":[{"type":"integer","minimum":-2147483648,"maximum":2147483647,"examples":[123]},{"type":"null"}],"default":null},"favoriteProgrammingLanguage":{"type":"string","enum":["JS","Java","Go","Rust","C"],"default":"JS"},"address":{"type":"object","properties":{"zipcode":{"type":"integer","minimum":-2147483648,"maximum":2147483647,"examples":[53003]}}},"someid":{"type":"string"}},"x-parser-schema-id":"com.company.Person"}},"mqtt":{"x-test":{"type":"string"}}},"x-parser-original-schema-format":"application/vnd.apache.avro;version=1.9.0","x-parser-original-payload":{"name":"Person","namespace":"com.company","type":"record","fields":[{"name":"name","type":"string","example":"Donkey"},{"name":"age","type":["null","int"],"default":null,"example":"123"},{"name":"favoriteProgrammingLanguage","type":{"name":"ProgrammingLanguage","type":"enum","symbols":["JS","Java","Go","Rust","C"],"default":"JS"}},{"name":"address","type":{"name":"Address","type":"record","fields":[{"name":"zipcode","type":"int","example":"53003"}]}},{"name":"someid","type":"uuid"}]},"x-parser-original-bindings-kafka-key":{"name":"Person","namespace":"com.company","type":"record","fields":[{"name":"name","type":"string","example":"Donkey"},{"name":"age","type":["null","int"],"default":null,"example":"123"},{"name":"favoriteProgrammingLanguage","type":{"name":"ProgrammingLanguage","type":"enum","symbols":["JS","Java","Go","Rust","C"],"default":"JS"}},{"name":"address","type":{"name":"Address","type":"record","fields":[{"name":"zipcode","type":"int","example":"53003"}]}},{"name":"someid","type":"uuid"}]},"schemaFormat":"application/vnd.aai.asyncapi;version=2.0.0","x-parser-message-parsed":true,"x-parser-message-name":""}}}},"x-parser-spec-parsed":true}'; + parser.registerSchemaParser(avroSchemaParser); describe('parse()', function() { @@ -27,4 +30,8 @@ describe('parse()', function() { const result = await parser.parse(inputWithAvro190WithNamespace, { path: __filename }); expect(JSON.stringify(result.json())).toEqual(outputWithAvro190ithNamespace); }); + it('should parse Avro schema in kafka bindings', async function() { + const result = await parser.parse(inputWithAvro190WithBindings, { path: __filename }); + expect(JSON.stringify(result.json())).toEqual(outputWithAvro190WithBindings); + }); });