@asyncapi/avro-schema-parser
Advanced tools
Comparing version 0.2.1 to 0.3.0
{ | ||
"name": "@asyncapi/avro-schema-parser", | ||
"version": "0.2.1", | ||
"version": "0.3.0", | ||
"description": "An AsyncAPI schema parser for Avro 1.x schemas.", | ||
@@ -10,4 +10,2 @@ "main": "index.js", | ||
"lint": "eslint --max-warnings 0 --config .eslintrc.yaml .", | ||
"get:version": "echo $npm_package_version", | ||
"get:name": "echo $npm_package_name", | ||
"generate:assets": "echo 'No additional assets need to be generated at the moment'", | ||
@@ -36,3 +34,3 @@ "bump:version": "npm --no-git-tag-version --allow-same-version version $VERSION" | ||
"devDependencies": { | ||
"@asyncapi/parser": "^1.5.0", | ||
"@asyncapi/parser": "^1.6.0", | ||
"@semantic-release/commit-analyzer": "^8.0.1", | ||
@@ -39,0 +37,0 @@ "@semantic-release/github": "^7.0.4", |
@@ -129,2 +129,12 @@ # Avro Schema Parser | ||
## Features | ||
### Support for extra attributes on top of Avro specification | ||
Additional attributes not defined in the [Avro Specification](https://avro.apache.org/docs/current/spec.html) are permitted and are treated as a metadata by the specification. To improve human readability of generated AsyncAPI documentation and to leverage more features from the JSON schema we included support for the extra attributes that can be added into Avro document. | ||
#### List of all supported extra attributes | ||
- `example` - Can be used to define the example value from the business domain of given field. Value will be propagated into [examples attribute](https://json-schema.org/draft/2020-12/json-schema-validation.html#rfc.section.9.5) of JSON schema and therefore will be picked for the generated "Example of payload" when using some AsyncAPI documentation generator. | ||
## Limitations | ||
@@ -131,0 +141,0 @@ |
@@ -7,6 +7,6 @@ const fs = require('fs'); | ||
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":"<anonymous-schema-2>"},"age":{"oneOf":[{"type":"null","x-parser-schema-id":"<anonymous-schema-4>"},{"type":"integer","minimum":-2147483648,"maximum":2147483647,"x-parser-schema-id":"<anonymous-schema-5>"}],"x-parser-schema-id":"<anonymous-schema-3>"},"favoriteProgrammingLanguage":{"type":"string","enum":["JS","Java","Go","Rust","C"],"default":"JS","x-parser-schema-id":"<anonymous-schema-6>"},"address":{"type":"object","properties":{"zipcode":{"type":"integer","minimum":-2147483648,"maximum":2147483647,"x-parser-schema-id":"<anonymous-schema-8>"}},"x-parser-schema-id":"<anonymous-schema-7>"},"someid":{"type":"string","x-parser-schema-id":"<anonymous-schema-9>"}},"x-parser-schema-id":"<anonymous-schema-1>"},"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-parsed":true,"x-parser-message-name":"<anonymous-message-1>"}}}},"x-parser-spec-parsed":true}'; | ||
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","examples":["Donkey"],"x-parser-schema-id":"<anonymous-schema-2>"},"age":{"oneOf":[{"type":"integer","minimum":-2147483648,"maximum":2147483647,"examples":[123],"x-parser-schema-id":"<anonymous-schema-4>"},{"type":"null","x-parser-schema-id":"<anonymous-schema-5>"}],"default":null,"x-parser-schema-id":"<anonymous-schema-3>"},"favoriteProgrammingLanguage":{"type":"string","enum":["JS","Java","Go","Rust","C"],"default":"JS","x-parser-schema-id":"<anonymous-schema-6>"},"address":{"type":"object","properties":{"zipcode":{"type":"integer","minimum":-2147483648,"maximum":2147483647,"examples":[53003],"x-parser-schema-id":"<anonymous-schema-8>"}},"x-parser-schema-id":"<anonymous-schema-7>"},"someid":{"type":"string","x-parser-schema-id":"<anonymous-schema-9>"}},"x-parser-schema-id":"<anonymous-schema-1>"},"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","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":"<anonymous-message-1>"}}}},"x-parser-spec-parsed":true}'; | ||
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":"<anonymous-schema-2>"},"age":{"oneOf":[{"type":"null","x-parser-schema-id":"<anonymous-schema-4>"},{"type":"integer","minimum":-2147483648,"maximum":2147483647,"x-parser-schema-id":"<anonymous-schema-5>"}],"x-parser-schema-id":"<anonymous-schema-3>"},"favoriteProgrammingLanguage":{"type":"string","enum":["JS","Java","Go","Rust","C"],"x-parser-schema-id":"<anonymous-schema-6>"},"address":{"type":"object","properties":{"zipcode":{"type":"integer","minimum":-2147483648,"maximum":2147483647,"x-parser-schema-id":"<anonymous-schema-8>"}},"x-parser-schema-id":"<anonymous-schema-7>"}},"x-parser-schema-id":"<anonymous-schema-1>"},"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-parsed":true,"x-parser-message-name":"<anonymous-message-1>"}}}},"x-parser-spec-parsed":true}'; | ||
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","examples":["Donkey"],"x-parser-schema-id":"<anonymous-schema-2>"},"age":{"oneOf":[{"type":"integer","minimum":-2147483648,"maximum":2147483647,"x-parser-schema-id":"<anonymous-schema-4>"},{"type":"null","x-parser-schema-id":"<anonymous-schema-5>"}],"default":null,"x-parser-schema-id":"<anonymous-schema-3>"},"favoriteProgrammingLanguage":{"type":"string","enum":["JS","Java","Go","Rust","C"],"x-parser-schema-id":"<anonymous-schema-6>"},"address":{"type":"object","properties":{"zipcode":{"type":"integer","minimum":-2147483648,"maximum":2147483647,"examples":[53003],"x-parser-schema-id":"<anonymous-schema-8>"}},"x-parser-schema-id":"<anonymous-schema-7>"}},"x-parser-schema-id":"<anonymous-schema-1>"},"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","example":"Donkey"},{"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","example":"53003"}]}}]},"schemaFormat":"application/vnd.aai.asyncapi;version=2.0.0","x-parser-message-parsed":true,"x-parser-message-name":"<anonymous-message-1>"}}}},"x-parser-spec-parsed":true}'; | ||
@@ -13,0 +13,0 @@ parser.registerSchemaParser(avroSchemaParser); |
@@ -62,3 +62,3 @@ const { avroToJsonSchema } = require('../to-json-schema'); | ||
const result = await avroToJsonSchema(['null', 'int']); | ||
expect(result).toEqual({ oneOf: [{ type: 'null' }, { type: 'integer', minimum: INT_MIN, maximum: INT_MAX }] }); | ||
expect(result).toEqual({ oneOf: [{ type: 'integer', minimum: INT_MIN, maximum: INT_MAX }, { type: 'null' }] }); | ||
}); | ||
@@ -65,0 +65,0 @@ |
@@ -24,2 +24,32 @@ const BYTES_PATTERN = '^[\u0000-\u00ff]*$'; | ||
const commonAttributesMapping = (avroDefinition, jsonSchema) => { | ||
if (avroDefinition.doc) jsonSchema.description = avroDefinition.doc; | ||
if (avroDefinition.default !== undefined) jsonSchema.default = avroDefinition.default; | ||
}; | ||
const exampleAttributeMapping = (typeInput, example, jsonSchemaInput) => { | ||
let type = typeInput; | ||
let jsonSchema = jsonSchemaInput; | ||
// Map example to first non-null type | ||
if (Array.isArray(typeInput) && typeInput.length > 0) { | ||
const pickSecondType = typeInput.length > 1 && typeInput[0] === 'null'; | ||
type = typeInput[+pickSecondType]; | ||
jsonSchema = jsonSchema.oneOf[0]; | ||
} | ||
if (example === undefined || jsonSchema.examples || Array.isArray(type)) return; | ||
switch (type) { | ||
case 'boolean': | ||
jsonSchema.examples = [example === 'true']; | ||
break; | ||
case 'int': | ||
jsonSchema.examples = [parseInt(example, 10)]; | ||
break; | ||
default: | ||
jsonSchema.examples = [example]; | ||
} | ||
}; | ||
module.exports.avroToJsonSchema = async function avroToJsonSchema(avroDefinition) { | ||
@@ -31,10 +61,16 @@ const jsonSchema = {}; | ||
jsonSchema.oneOf = []; | ||
let nullDef = null; | ||
for (const avroDef of avroDefinition) { | ||
const def = await avroToJsonSchema(avroDef); | ||
jsonSchema.oneOf.push(def); | ||
// avroDef can be { type: 'int', default: 1 } and this is why avroDef.type has priority here | ||
const defType = avroDef.type || avroDef; | ||
// To prefer non-null values in the examples skip null definition here and push it as the last element after loop | ||
if (defType === 'null') nullDef = def; else jsonSchema.oneOf.push(def); | ||
} | ||
if (nullDef) jsonSchema.oneOf.push(nullDef); | ||
return jsonSchema; | ||
} | ||
// Avro definition can be a string (e.g. "int") | ||
@@ -44,3 +80,3 @@ // or an object like { type: "int" } | ||
jsonSchema.type = typeMappings[type]; | ||
switch (type) { | ||
@@ -76,4 +112,6 @@ case 'int': | ||
const def = await avroToJsonSchema(field.type); | ||
if (field.doc) def.description = field.doc; | ||
if (field.default) def.default = field.default; | ||
commonAttributesMapping(field, def); | ||
exampleAttributeMapping(field.type, field.example, def); | ||
propsMap.set(field.name, def); | ||
@@ -85,6 +123,6 @@ } | ||
if (avroDefinition.doc) jsonSchema.description = avroDefinition.doc; | ||
if (avroDefinition.default !== undefined) jsonSchema.default = avroDefinition.default; | ||
commonAttributesMapping(avroDefinition, jsonSchema); | ||
exampleAttributeMapping(type, avroDefinition.example, jsonSchema); | ||
return jsonSchema; | ||
}; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Long strings
Supply chain riskContains long string literals, which may be a sign of obfuscated or packed code.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Long strings
Supply chain riskContains long string literals, which may be a sign of obfuscated or packed code.
Found 1 instance in 1 package
190077
263
148