Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add all recommended and must supported schema formats #365

Merged
Merged
4 changes: 1 addition & 3 deletions definitions/3.0.0/asyncapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@
"properties": {
"asyncapi": {
"type": "string",
"enum": [
"3.0.0"
],
"const": "3.0.0",
"description": "The AsyncAPI specification version of this document."
},
"id": {
Expand Down
31 changes: 30 additions & 1 deletion definitions/3.0.0/messageObject.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,36 @@
},
"properties": {
"schemaFormat": {
"type": "string"
"anyOf": [
{
"type": "string"
},
{
"description": "All MUST schema formats tooling should support",
jonaslagoni marked this conversation as resolved.
Show resolved Hide resolved
"enum": [
"application/vnd.aai.asyncapi;version=3.0.0",
"application/vnd.aai.asyncapi+json;version=3.0.0",
"application/vnd.aai.asyncapi+yaml;version=3.0.0",

"application/schema+json;version=draft-07",
"application/schema+yaml;version=draft-07"
]
},
{
"description": "All RECOMMENDED schema formats tooling should support",
jonaslagoni marked this conversation as resolved.
Show resolved Hide resolved
"enum": [
"application/vnd.oai.openapi;version=3.0.0",
"application/vnd.oai.openapi+json;version=3.0.0",
"application/vnd.oai.openapi+yaml;version=3.0.0",

"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/raml+yaml;version=1.0"
]
}
]
},
"contentType": {
"type": "string"
Expand Down
51 changes: 49 additions & 2 deletions scripts/add-new-version.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const path = require('path');
/**
* This script adds a new version of the spec, by copying the latest one as baseline.
*/
Expand All @@ -23,6 +24,47 @@ function execute(command) {
});
}

/**
* Add the new AsyncAPI schema object as values to schemaFormat
*
* If a major version change, replaces all old AsyncAPI schemaFormat values with fresh ones
* if minor or fix, it add new ones
*/
function addNewSchemaVersion(newVersion, newVersionDir, latestVersion) {
const objFile = path.resolve(newVersionDir, 'messageObject.json');
const obj = require(objFile);
let enums = [] = obj?.properties?.schemaFormat?.anyOf[1]?.enum;
const isMajorVersionChange = newVersion.charAt(0) !== latestVersion.charAt(0);

//Add new version to schemaFormat
if(enums) {
if(isMajorVersionChange) {
//Remove all old AsyncAPI schema formats
enums = enums.filter((format) => !format.includes('application/vnd.aai.asyncapi'));
}
//Add new schema formats
enums.push(`application/vnd.aai.asyncapi;version=${newVersion}`);
enums.push(`application/vnd.aai.asyncapi+json;version=${newVersion}`);
enums.push(`application/vnd.aai.asyncapi+yaml;version=${newVersion}`);
obj.properties.schemaFormat.anyOf[1].enum = enums;
} else {
throw new Error("Could not find object to add schemaFormat values to");
}

fs.writeFileSync(objFile, JSON.stringify(obj, null, 2));
}

/**
* Adapt the root title and .asyncapi property
*/
function adaptRootObject(newVersion, newVersionDir) {
const objFile = path.resolve(newVersionDir, 'asyncapi.json');
const obj = require(objFile);
obj.title = `AsyncAPI ${newVersion} schema.`;
obj.properties.asyncapi.const = newVersion;
fs.writeFileSync(objFile, JSON.stringify(obj, null, 2));
}

async function addNewVersion(newVersion) {
const newVersionDir = `./definitions/${newVersion}`;

Expand All @@ -36,8 +78,13 @@ async function addNewVersion(newVersion) {
const latestVersion = (await execute('ls -d ./definitions/* | sort -V -r | head -1 | xargs -n 1 basename')).trim();
await execute(`cp -R ./definitions/${latestVersion} ${newVersionDir}`);

// Replace old version numbers with new
await execute(`find ${newVersionDir} -name '*.json' -exec sed -i '' \"s+${latestVersion}+${newVersion}+g\" {} +`);
// Replace $ref and $id paths such as `/3.0.0/` with new version (http://asyncapi.com/definitions/3.0.0/specificationExtension.json)
await execute(`find ${newVersionDir} -name '*.json' -exec sed -i '' \"s+\/${latestVersion}\/+\/${newVersion}\/+g\" {} +`);
// Replace .asyncapi version from old to new version
// Replace old version in title with new version
adaptRootObject(newVersion, newVersionDir);
// Add new schemaFormat version entries
addNewSchemaVersion(newVersion, newVersionDir, latestVersion);

console.log(`New version added to ${newVersionDir}`)
}
Expand Down