Skip to content

Commit

Permalink
fix: support for AsyncAPI docs with only publish or subscribe ops (#33)
Browse files Browse the repository at this point in the history
The Demo class depends on channel.value.publish being null to
identify a channel without a publish operation. This assumption
doesn't work, because even for channels without a publish op
channel.value.publish is a function that returns null - it isn't
null itself. This results in uncaught exceptions when generating
code for AsyncAPI docs with only a publish or subscribe operation

This commit corrects that by replacing the checks with the
hasPublish() and hasSubscribe() methods which is a safer way to
identify if the operations exist.

Signed-off-by: Dale Lane <[email protected]>
  • Loading branch information
dalelane authored May 23, 2022
1 parent 9134232 commit 75569bb
Showing 1 changed file with 23 additions and 16 deletions.
39 changes: 23 additions & 16 deletions components/Demo/Demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,20 @@ export function Demo(asyncapi, params) {
const channels = Object.entries(asyncapi.channels()).map(([key, value]) => ({key,value}));

const foundPubAndSub = channels.filter((el) => {
return el.value.publish && el.value.subscribe;
return el.value.hasPublish() && el.value.hasSubscribe();
});

const foundPubOrSub = channels.filter((el) => {
return el.value.publish || el.value.subscribe;
return el.value.hasPublish() || el.value.hasSubscribe();
});

// Prioritise channel with both, fallback to an OR
const channel = foundPubAndSub.length ? foundPubAndSub[0] : foundPubOrSub[0];
const channelName = channel.key;

// Get payload from either publish or subscribe
const targetMessageName = channel.value.publish ? channel.value.publish().message().uid() : channel.value.subscribe().message().uid();
const targetPayloadProperties = channel.value.publish ? channel.value.publish().message().payload().properties() : channel.value.subscribe().message().payload().properties();
const targetMessageName = channel.value.hasPublish() ? channel.value.publish().message().uid() : channel.value.subscribe().message().uid();
const targetPayloadProperties = channel.value.hasPublish() ? channel.value.publish().message().payload().properties() : channel.value.subscribe().message().payload().properties();

const messageNameTitleCase = toJavaClassName(targetMessageName);

Expand All @@ -48,15 +48,22 @@ export function Demo(asyncapi, params) {
const className = toJavaClassName(channelName);

const constructorArgs = createJavaConstructorArgs(targetPayloadProperties).join(', ');
return [(
<File name={producerPath}>
<PackageDeclaration path={params.package} />
<DemoProducer params={params} messageName={messageNameTitleCase} className={className} constructorArgs={constructorArgs}></DemoProducer>
</File>
), (
<File name={subscriberPath}>
<PackageDeclaration path={params.package} />
<DemoSubscriber params={params} className={className}></DemoSubscriber>
</File>
)];
}
const generatedClasses = [];
if (channel.value.hasPublish()) {
generatedClasses.push(
<File name={producerPath}>
<PackageDeclaration path={params.package} />
<DemoProducer params={params} messageName={messageNameTitleCase} className={className} constructorArgs={constructorArgs}></DemoProducer>
</File>
);
}
if (channel.value.hasSubscribe()) {
generatedClasses.push(
<File name={subscriberPath}>
<PackageDeclaration path={params.package} />
<DemoSubscriber params={params} className={className}></DemoSubscriber>
</File>
);
}
return generatedClasses;
}

0 comments on commit 75569bb

Please sign in to comment.