From e78ebe966385d663a1550accf191f67474d0b615 Mon Sep 17 00:00:00 2001 From: Ludovic Dussart Date: Fri, 8 Oct 2021 13:04:44 +0200 Subject: [PATCH] fixup: codacy codesmells --- tests/parse.test.js | 1 - tests/to-json-schema.test.js | 5 ----- to-json-schema.js | 37 +++++++++++++++++++++++------------- 3 files changed, 24 insertions(+), 19 deletions(-) diff --git a/tests/parse.test.js b/tests/parse.test.js index cd385b9f..0f035f3f 100644 --- a/tests/parse.test.js +++ b/tests/parse.test.js @@ -18,7 +18,6 @@ const outputWithAvro190WithBindings = '{"asyncapi":"2.0.0","info":{"title":"My A const inputWithAvroAdditionalAttributes = fs.readFileSync(path.resolve(__dirname, './asyncapi-avro-1.9.0-additional-attributes.yaml'), 'utf8'); const outputWithAvroAdditionalAttributes = '{"asyncapi":"2.1.0","info":{"title":"My API","version":"1.0.0"},"channels":{"mychannel":{"publish":{"message":{"payload":{"type":"object","required":["name","serialNo","favoriteProgrammingLanguage","certifications","address","weight","height","someid"],"properties":{"name":{"type":"string","examples":["Donkey"],"minLength":0,"x-parser-schema-id":""},"serialNo":{"type":"string","minLength":0,"maxLength":50,"x-parser-schema-id":""},"email":{"oneOf":[{"type":"string","examples":["donkey@asyncapi.com"],"pattern":"^[\\\\w-\\\\.]+@([\\\\w-]+\\\\.)+[\\\\w-]{2,4}$","x-parser-schema-id":""},{"type":"null","x-parser-schema-id":""}],"x-parser-schema-id":""},"age":{"oneOf":[{"type":"integer","minimum":-2147483648,"maximum":2147483647,"examples":[123],"exclusiveMinimum":0,"exclusiveMaximum":200,"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":""},"certifications":{"type":"array","items":{"type":"string","x-parser-schema-id":""},"minItems":1,"maxItems":500,"uniqueItems":true,"x-parser-schema-id":""},"address":{"type":"object","required":["zipcode"],"properties":{"zipcode":{"type":"integer","minimum":-2147483648,"maximum":2147483647,"examples":[53003],"x-parser-schema-id":""},"country":{"oneOf":[{"type":"string","x-parser-schema-id":""},{"type":"null","x-parser-schema-id":""}],"x-parser-schema-id":""}},"x-parser-schema-id":""},"weight":{"type":"number","format":"float","examples":["65.0"],"minimum":0,"maximum":500,"x-parser-schema-id":""},"height":{"type":"number","format":"double","examples":["1.85"],"minimum":0,"maximum":3,"x-parser-schema-id":""},"someid":{"type":"string","format":"uuid","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","minLength":0},{"name":"serialNo","type":"string","minLength":0,"maxLength":50},{"name":"email","type":["null","string"],"example":"donkey@asyncapi.com","pattern":"^[\\\\w-\\\\.]+@([\\\\w-]+\\\\.)+[\\\\w-]{2,4}$"},{"name":"age","type":["null","int"],"default":null,"example":"123","exclusiveMinimum":0,"exclusiveMaximum":200},{"name":"favoriteProgrammingLanguage","type":{"name":"ProgrammingLanguage","type":"enum","symbols":["JS","Java","Go","Rust","C"],"default":"JS"}},{"name":"certifications","type":{"type":"array","items":"string","minItems":1,"maxItems":500,"uniqueItems":true}},{"name":"address","type":{"name":"Address","type":"record","fields":[{"name":"zipcode","type":"int","example":53003},{"name":"country","type":["null","string"]}]}},{"name":"weight","type":"float","example":"65.0","minimum":0,"maximum":500},{"name":"height","type":"double","example":"1.85","minimum":0,"maximum":3},{"name":"someid","type":"string","logicalType":"uuid"}]},"schemaFormat":"application/vnd.aai.asyncapi;version=2.1.0","x-parser-message-parsed":true,"x-parser-message-name":""}}}},"x-parser-spec-parsed":true}'; - parser.registerSchemaParser(avroSchemaParser); describe('parse()', function() { diff --git a/tests/to-json-schema.test.js b/tests/to-json-schema.test.js index 1df73285..35b6801e 100644 --- a/tests/to-json-schema.test.js +++ b/tests/to-json-schema.test.js @@ -248,11 +248,6 @@ describe('additionalAttributesMapping()', function () { expect(result).toEqual({type: 'string'}); }); - it('support pattern, minLength and maxLength for fixed', async function () { - const result = await avroToJsonSchema({type: 'fixed', pattern: '$pattern^', minLength: 1, maxLength: 10}); - expect(result).toEqual({type: 'string', pattern: '$pattern^', minLength: 1, maxLength: 10}); - }); - it('support minItems and maxItems for array', async function () { const result = await avroToJsonSchema({type: 'array', items: 'long', minItems: 0, maxItems: 10}); expect(result).toMatchObject({type: 'array', items: {type: 'integer'}, minItems: 0, maxItems: 10}); diff --git a/to-json-schema.js b/to-json-schema.js index 6d2cf385..a94ec552 100644 --- a/to-json-schema.js +++ b/to-json-schema.js @@ -95,28 +95,39 @@ const additionalAttributesMapping = (typeInput, avroDefinition, jsonSchemaInput) exampleAttributeMapping(type, avroDefinition.example, jsonSchema); + function setAdditionalAttribute(name) { + let isValueCoherent = true; + if (name === 'minLength' || name === 'maxLength') { + isValueCoherent = avroDefinition[name] > -1; + } else if (name === 'multipleOf') { + isValueCoherent = avroDefinition[name] > 0; + } + if (avroDefinition[name] !== undefined && isValueCoherent) jsonSchema[name] = avroDefinition[name]; + } + switch (type) { case 'int': case 'long': case 'float': case 'double': - if (avroDefinition.minimum !== undefined) jsonSchema.minimum = avroDefinition.minimum; - if (avroDefinition.multipleOf !== undefined && avroDefinition.multipleOf > 0) jsonSchema.multipleOf = avroDefinition.multipleOf; - if (avroDefinition.maximum !== undefined) jsonSchema.maximum = avroDefinition.maximum; - if (avroDefinition.exclusiveMinimum !== undefined) jsonSchema.exclusiveMinimum = avroDefinition.exclusiveMinimum; - if (avroDefinition.exclusiveMaximum !== undefined) jsonSchema.exclusiveMaximum = avroDefinition.exclusiveMaximum; + setAdditionalAttribute('minimum'); + setAdditionalAttribute('maximum'); + setAdditionalAttribute('exclusiveMinimum'); + setAdditionalAttribute('exclusiveMaximum'); + setAdditionalAttribute('multipleOf'); break; case 'string': - jsonSchema.format= avroDefinition.logicalType; - case 'fixed': - if (avroDefinition.pattern) jsonSchema.pattern = avroDefinition.pattern; - if (avroDefinition.minLength !== undefined && avroDefinition.minLength > -1) jsonSchema.minLength = avroDefinition.minLength; - if (avroDefinition.maxLength !== undefined && avroDefinition.maxLength > -1) jsonSchema.maxLength = avroDefinition.maxLength; + jsonSchema.format = avroDefinition.logicalType; + setAdditionalAttribute('pattern'); + setAdditionalAttribute('minLength'); + setAdditionalAttribute('maxLength'); break; case 'array': - if (avroDefinition.minItems !== undefined && avroDefinition.minItems > -1) jsonSchema.minItems = avroDefinition.minItems; - if (avroDefinition.maxItems !== undefined && avroDefinition.maxItems > -1) jsonSchema.maxItems = avroDefinition.maxItems; - if (avroDefinition.uniqueItems !== undefined) jsonSchema.uniqueItems = avroDefinition.uniqueItems; + setAdditionalAttribute('minItems'); + setAdditionalAttribute('maxItems'); + setAdditionalAttribute('uniqueItems'); + break; + default: break; } };