Skip to content

Commit

Permalink
disable validation for spec v3 and test pub-sub
Browse files Browse the repository at this point in the history
  • Loading branch information
kaushik-rishi committed May 4, 2024
1 parent 88a8c66 commit 0d953a8
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 63 deletions.
2 changes: 1 addition & 1 deletion helpers/channels-topics.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export function toHermesTopic(str) {
}

export function channelNamesWithReceive(asyncapi) {
return asyncapi.channels().filterByReceive().map(channel => channel.id());
return asyncapi.channels().filterByReceive().map(channel => channel.address());
}

export function host(url) {
Expand Down
7 changes: 4 additions & 3 deletions template/src/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,16 @@ export default function indexEntrypointFile({asyncapi, params}) {
`

const channelsMiddleware = asyncapi.channels().all().map(channel => {
const channelName = channel.id();
const channelName = channel.address();
const channelOperationId = channel.id();
let channelLogic = '';
if (channel.operations().filterByReceive().length > 0) {
channelLogic += `console.log(cyan.bold.inverse(' SUB '), gray('Subscribed to'), yellow('${channelName}'));
app.use(${camelCase(channelName)});`;
app.use(${camelCase(channelOperationId)});`;
}
if (channel.operations().filterBySend().length > 0) {
channelLogic += `console.log(yellow.bold.inverse(' PUB '), gray('Will eventually publish to'), yellow('${channelName}'));
app.useOutbound(${camelCase(channelName)});`;
app.useOutbound(${camelCase(channelOperationId)});`;
}
return channelLogic;
}).join('\n');
Expand Down
110 changes: 51 additions & 59 deletions template/src/api/routes/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,36 @@
import { File } from '@asyncapi/generator-react-sdk';
import { camelCase, convertToFilename, toHermesTopic } from '../../../../helpers/index';

function receiveHandler(operation, channelName, channelAddress) {
function receiveHandler(operation, channelName, channelAddress, isSpecV3) {
if (!operation.isReceive()) {
return '';
}

const operationId = operation.id();
const message = operation.messages().all()[0];
const messageValidationLogic = (operation.messages().length > 1) ? `
/*
* TODO: If https://github.com/asyncapi/parser-js/issues/372 is addressed, simplify this
* code to just validate the message against the combined message schema which will
* include the \`oneOf\` in the JSON schema - let the JSON schema validator handle the
* oneOf semantics (rather than each generator having to emit conditional code)
*/
let nValidated = 0;
// For oneOf, only one message schema should match.
// Validate payload against each message and count those which validate
${
operation.messages().all().map(message => `try {
nValidated = await validateMessage(message.payload,'${ channelAddress }','${ message.name() }','publish', nValidated);
} catch { };`).join('\n')
}
if (nValidated === 1) {
await ${camelCase(channelName)}Handler._${operationId}({message});
next()
} else {
throw new Error(\`\${nValidated} of ${ operation.messages().length } message schemas matched when exactly 1 should match\`);
}` : `await validateMessage(message.payload,'${ channelAddress }','${ message.name() }','publish');`;

return `
${operation.hasSummary() ? `
Expand All @@ -18,93 +41,60 @@ function receiveHandler(operation, channelName, channelAddress) {
`: ''}
router.use('${toHermesTopic(channelAddress)}', async (message, next) => {
try {
${(operation.messages().length > 1)
? `
/*
* TODO: If https://github.com/asyncapi/parser-js/issues/372 is addressed, simplify this
* code to just validate the message against the combined message schema which will
* include the \`oneOf\` in the JSON schema - let the JSON schema validator handle the
* oneOf semantics (rather than each generator having to emit conditional code)
*/
let nValidated = 0;
// For oneOf, only one message schema should match.
// Validate payload against each message and count those which validate
${
operation.messages().all().map(message => `try {
nValidated = await validateMessage(message.payload,'${ channelAddress }','${ message.name() }','publish', nValidated);
} catch { };`).join('\n')
}
if (nValidated === 1) {
await ${camelCase(channelName)}Handler._${operationId}({message});
next()
} else {
throw new Error(\`\${nValidated} of ${ operation.messages().length } message schemas matched when exactly 1 should match\`);
}
`
: `
await validateMessage(message.payload,'${ channelAddress }','${ message.name() }','publish');
${isSpecV3 ? '' : messageValidationLogic}
await ${camelCase(channelName)}Handler._${ operationId }({message});
next();
`
}
} catch (e) {
next(e);
}
});
`;
}

function sendHandler(operation, channelName, channelAddress) {
function sendHandler(operation, channelName, channelAddress, isSpecV3) {
if (!operation.isSend()) {
return '';
}

const operationId = operation.id();
const message = operation.messages().all()[0];
const messageValidationLogic = (operation.messages().length > 1) ? `
let nValidated = 0;
// For oneOf, only one message schema should match.
// Validate payload against each message and count those which validate
${
operation.messages().all().map(message => `try {
nValidated = await validateMessage(message.payload,'${ channelAddress }','${ message.name() }','subscribe', nValidated);
} catch { };`).join('\n')
}
if (nValidated === 1) {
await ${camelCase(channelName)}Handler._${operationId}({message});
next()
} else {
throw new Error(\`\${nValidated} of ${ operation.messages().length } message schemas matched when exactly 1 should match\`);
}` : `await validateMessage(message.payload,'${ channelAddress }','${ message.name() }','subscribe');`

return `
${operation.hasSummary() ? `
/**
* ${ operation.summary() }
*/
`: ''}
router.use('${toHermesTopic(channelAddress)}', async (message, next) => {
router.useOutbound('${toHermesTopic(channelAddress)}', async (message, next) => {
try {
${(operation.messages().length > 1)
? `
let nValidated = 0;
// For oneOf, only one message schema should match.
// Validate payload against each message and count those which validate
${
operation.messages().all().map(message => `try {
nValidated = await validateMessage(message.payload,'${ channelAddress }','${ message.name() }','subscribe', nValidated);
} catch { };`).join('\n')
}
if (nValidated === 1) {
await ${camelCase(channelName)}Handler._${operationId}({message});
next()
} else {
throw new Error(\`\${nValidated} of ${ operation.messages().length } message schemas matched when exactly 1 should match\`);
}
`
: `
await validateMessage(message.payload,'${ channelAddress }','${ message.name() }','subscribe');
${isSpecV3 ? '' : messageValidationLogic}
await ${camelCase(channelName)}Handler._${ operationId }({message});
next();
`
}
} catch (e) {
next(e);
}
});
`;
}

function routeCode(channel) {
function routeCode(channel, isSpecV3) {
const channelName = channel.id();
const generalImport = `
const Router = require('hermesjs/lib/router');
Expand All @@ -120,18 +110,20 @@ function routeCode(channel) {

for (const operation of channel.operations()) {
if (operation.isSend()) {
routeHandler += sendHandler(operation, channel.id(), channel.address());
routeHandler += sendHandler(operation, channel.id(), channel.address(), isSpecV3);
}
if (operation.isReceive()) {
routeHandler += receiveHandler(operation, channel.id(), channel.address());
routeHandler += receiveHandler(operation, channel.id(), channel.address(), isSpecV3);
}
}

return <File name={`${convertToFilename(channelName)}.js`}>{routeHandler}</File>;
}

export default function routeRender({asyncapi}) {
let majorSpecVersion = parseInt(asyncapi.version().split('.')[0]);
let isSpecV3 = (majorSpecVersion === 3);
return asyncapi.channels().all().map(channel => {
return routeCode(channel);
return routeCode(channel, isSpecV3);
});
}

0 comments on commit 0d953a8

Please sign in to comment.