From 6447ab833fa26a041b9e8f4e71bae11afdb7b1a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Germ=C3=A1n=20Schnyder?= Date: Mon, 24 Aug 2020 18:12:12 +0200 Subject: [PATCH] feat: adding support for avro 1.8.2 (#6) * Added support for 1.8.2 * Bumped up version * Added uuid test * Fixed indentation * Undo version change since pipeline will handle it --- index.js | 3 +++ tests/asyncapi-avro-1.8.2.yaml | 11 ++++++++++ ...api-avro.yaml => asyncapi-avro-1.9.0.yaml} | 2 +- tests/parse.test.js | 17 ++++++++++----- .../{Person.avsc => Person-1.8.2.avsc} | 0 tests/schemas/Person-1.9.0.avsc | 21 +++++++++++++++++++ tests/to-json-schema.test.js | 5 +++++ to-json-schema.js | 1 + 8 files changed, 54 insertions(+), 6 deletions(-) create mode 100644 tests/asyncapi-avro-1.8.2.yaml rename tests/{asyncapi-avro.yaml => asyncapi-avro-1.9.0.yaml} (81%) rename tests/schemas/{Person.avsc => Person-1.8.2.avsc} (100%) create mode 100644 tests/schemas/Person-1.9.0.avsc diff --git a/index.js b/index.js index 6b174a3c..28d61f73 100644 --- a/index.js +++ b/index.js @@ -14,5 +14,8 @@ module.exports.getMimeTypes = () => { 'application/vnd.apache.avro;version=1.9.0', 'application/vnd.apache.avro+json;version=1.9.0', 'application/vnd.apache.avro+yaml;version=1.9.0', + 'application/vnd.apache.avro;version=1.8.2', + 'application/vnd.apache.avro+json;version=1.8.2', + 'application/vnd.apache.avro+yaml;version=1.8.2' ]; }; diff --git a/tests/asyncapi-avro-1.8.2.yaml b/tests/asyncapi-avro-1.8.2.yaml new file mode 100644 index 00000000..66b60506 --- /dev/null +++ b/tests/asyncapi-avro-1.8.2.yaml @@ -0,0 +1,11 @@ +asyncapi: 2.0.0 +info: + title: My API + version: '1.0.0' +channels: + mychannel: + publish: + message: + schemaFormat: application/vnd.apache.avro;version=1.8.2 + payload: + $ref: 'schemas/Person-1.8.2.avsc' diff --git a/tests/asyncapi-avro.yaml b/tests/asyncapi-avro-1.9.0.yaml similarity index 81% rename from tests/asyncapi-avro.yaml rename to tests/asyncapi-avro-1.9.0.yaml index dcb1af69..dd10f5b8 100644 --- a/tests/asyncapi-avro.yaml +++ b/tests/asyncapi-avro-1.9.0.yaml @@ -8,4 +8,4 @@ channels: message: schemaFormat: application/vnd.apache.avro;version=1.9.0 payload: - $ref: 'schemas/Person.avsc' + $ref: 'schemas/Person-1.9.0.avsc' \ No newline at end of file diff --git a/tests/parse.test.js b/tests/parse.test.js index 37a70d61..d45840d4 100644 --- a/tests/parse.test.js +++ b/tests/parse.test.js @@ -3,14 +3,21 @@ const path = require('path'); const avroSchemaParser = require('..'); const parser = require('@asyncapi/parser'); -const inputWithAvro = fs.readFileSync(path.resolve(__dirname, './asyncapi-avro.yaml'), 'utf8'); -const outputWithAvro = '{"asyncapi":"2.0.0","info":{"title":"My API","version":"1.0.0"},"channels":{"mychannel":{"publish":{"message":{"payload":{"type":"object","properties":{"name":{"type":"string","x-parser-schema-id":""},"age":{"oneOf":[{"type":"null","x-parser-schema-id":""},{"type":"integer","minimum":-2147483648,"maximum":2147483647,"x-parser-schema-id":""}],"x-parser-schema-id":""},"favoriteProgrammingLanguage":{"type":"string","enum":["JS","Java","Go","Rust","C"],"x-parser-schema-id":""},"address":{"type":"object","properties":{"zipcode":{"type":"integer","minimum":-2147483648,"maximum":2147483647,"x-parser-schema-id":""}},"x-parser-schema-id":""}},"x-parser-schema-id":""},"x-parser-original-schema-format":"application/vnd.apache.avro;version=1.9.0","x-parser-original-payload":{"name":"Person","type":"record","fields":[{"name":"name","type":"string"},{"name":"age","type":["null","int"],"default":null},{"name":"favoriteProgrammingLanguage","type":{"name":"ProgrammingLanguage","type":"enum","symbols":["JS","Java","Go","Rust","C"]}},{"name":"address","type":{"name":"Address","type":"record","fields":[{"name":"zipcode","type":"int"}]}}]},"schemaFormat":"application/vnd.aai.asyncapi;version=2.0.0","x-parser-message-name":""}}}}}'; +const inputWithAvro190 = fs.readFileSync(path.resolve(__dirname, './asyncapi-avro-1.9.0.yaml'), 'utf8'); +const outputWithAvro190 = '{"asyncapi":"2.0.0","info":{"title":"My API","version":"1.0.0"},"channels":{"mychannel":{"publish":{"message":{"payload":{"type":"object","properties":{"name":{"type":"string","x-parser-schema-id":""},"age":{"oneOf":[{"type":"null","x-parser-schema-id":""},{"type":"integer","minimum":-2147483648,"maximum":2147483647,"x-parser-schema-id":""}],"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,"x-parser-schema-id":""}},"x-parser-schema-id":""},"someid":{"type":"string","x-parser-schema-id":""}},"x-parser-schema-id":""},"x-parser-original-schema-format":"application/vnd.apache.avro;version=1.9.0","x-parser-original-payload":{"name":"Person","type":"record","fields":[{"name":"name","type":"string"},{"name":"age","type":["null","int"],"default":null},{"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"}]}},{"name":"someid","type":"uuid"}]},"schemaFormat":"application/vnd.aai.asyncapi;version=2.0.0","x-parser-message-name":""}}}}}'; + +const inputWithAvro182 = fs.readFileSync(path.resolve(__dirname, './asyncapi-avro-1.8.2.yaml'), 'utf8'); +const outputWithAvro182 = '{"asyncapi":"2.0.0","info":{"title":"My API","version":"1.0.0"},"channels":{"mychannel":{"publish":{"message":{"payload":{"type":"object","properties":{"name":{"type":"string","x-parser-schema-id":""},"age":{"oneOf":[{"type":"null","x-parser-schema-id":""},{"type":"integer","minimum":-2147483648,"maximum":2147483647,"x-parser-schema-id":""}],"x-parser-schema-id":""},"favoriteProgrammingLanguage":{"type":"string","enum":["JS","Java","Go","Rust","C"],"x-parser-schema-id":""},"address":{"type":"object","properties":{"zipcode":{"type":"integer","minimum":-2147483648,"maximum":2147483647,"x-parser-schema-id":""}},"x-parser-schema-id":""}},"x-parser-schema-id":""},"x-parser-original-schema-format":"application/vnd.apache.avro;version=1.8.2","x-parser-original-payload":{"name":"Person","type":"record","fields":[{"name":"name","type":"string"},{"name":"age","type":["null","int"],"default":null},{"name":"favoriteProgrammingLanguage","type":{"name":"ProgrammingLanguage","type":"enum","symbols":["JS","Java","Go","Rust","C"]}},{"name":"address","type":{"name":"Address","type":"record","fields":[{"name":"zipcode","type":"int"}]}}]},"schemaFormat":"application/vnd.aai.asyncapi;version=2.0.0","x-parser-message-name":""}}}}}'; parser.registerSchemaParser(avroSchemaParser); describe('parse()', function() { - it('should parse Avro schemas', async function() { - const result = await parser.parse(inputWithAvro, { path: __filename }); - expect(JSON.stringify(result.json())).toEqual(outputWithAvro); + it('should parse Avro schema 1.9.0', async function() { + const result = await parser.parse(inputWithAvro190, { path: __filename }); + expect(JSON.stringify(result.json())).toEqual(outputWithAvro190); + }); + it('should parse Avro schema 1.8.2', async function() { + const result = await parser.parse(inputWithAvro182, { path: __filename }); + expect(JSON.stringify(result.json())).toEqual(outputWithAvro182); }); }); diff --git a/tests/schemas/Person.avsc b/tests/schemas/Person-1.8.2.avsc similarity index 100% rename from tests/schemas/Person.avsc rename to tests/schemas/Person-1.8.2.avsc diff --git a/tests/schemas/Person-1.9.0.avsc b/tests/schemas/Person-1.9.0.avsc new file mode 100644 index 00000000..01b41e29 --- /dev/null +++ b/tests/schemas/Person-1.9.0.avsc @@ -0,0 +1,21 @@ +{ + "name": "Person", + "type": "record", + "fields": [ + {"name": "name", "type": "string"}, + {"name": "age", "type": ["null", "int"], "default": null}, + { + "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"}] + } + }, + {"name": "someid", "type": "uuid"} + ] +} diff --git a/tests/to-json-schema.test.js b/tests/to-json-schema.test.js index 1028c1de..54ed71c5 100644 --- a/tests/to-json-schema.test.js +++ b/tests/to-json-schema.test.js @@ -46,6 +46,11 @@ describe('avroToJsonSchema()', function () { const result = await avroToJsonSchema({ type: 'string' }); expect(result).toEqual({ type: 'string' }); }); + + it('transforms uuid values', async function () { + const result = await avroToJsonSchema({ type: 'uuid' }); + expect(result).toEqual({ type: 'string' }); + }); it('transforms fixed values', async function () { const result = await avroToJsonSchema({ type: 'fixed', size: 5 }); diff --git a/to-json-schema.js b/to-json-schema.js index a353b3e5..f69180b8 100644 --- a/to-json-schema.js +++ b/to-json-schema.js @@ -18,6 +18,7 @@ const typeMappings = { array: 'array', enum: 'string', record: 'object', + uuid: 'string', }; module.exports.avroToJsonSchema = async function avroToJsonSchema(avroDefinition) {