generator icon indicating copy to clipboard operation
generator copied to clipboard

[BUG] Discriminator helper drops valid falsy values

Open Khush7Chauhan opened this issue 3 weeks ago • 4 comments

Describe the bug

getMessageDiscriminatorData() treats valid falsy discriminator values as missing.

In packages/helpers/src/discriminators.js, the helper currently uses:

if (!discriminator_value) {
  return null;
}

This incorrectly rejects valid schema constants such as false, 0, or an empty string. As a result, messages using these discriminator values are omitted from generated discriminator metadata.

Expected behavior

Valid discriminator constants should be preserved, including false, 0, and empty strings.

The helper should return discriminator metadata whenever the constant is defined. It should return null only when the constant is undefined or null.

Screenshots

No screenshot is necessary for this logic-only bug.

How to Reproduce

  1. Open packages/helpers/src/discriminators.js.
  2. Call getMessageDiscriminatorData() with a message whose payload has a discriminator property with a constant value of false or 0.
  3. Observe that the helper returns null.
  4. The value is incorrectly discarded because the implementation checks if (!discriminator_value).

For example, a payload schema containing:

discriminator: kind
properties:
  kind:
    const: false

should produce discriminator metadata, but currently does not.

🖥️ Device Information [optional]

  • Operating System: Windows 11
  • Browser: Brave
  • Browser Version: Latest

👀 Have you checked for similar open issues?

  • [x] I checked and didn't find similar issue

🏢 Have you read the Contributing Guidelines?

Are you willing to work on this issue ?

Yes I am willing to submit a PR!

Khush7Chauhan avatar Nov 07 '26 17:11 Khush7Chauhan

One regression-test detail for your PR: packages/helpers/test/discriminators.test.js currently asserts typeof disc.value === 'string' for every aggregated discriminator. If you add boolean or numeric constants to the shared fixture, that assertion will also need updating to check the expected values and preserve their types. [1]

I'd add parameterized cases for false, 0, and '', plus null and undefined as negative cases, and check both exported helpers. getMessageDiscriminatorsFromOperations() checks the returned metadata object, not its .value, so once the inner guard rejects only null/undefined, the aggregation path should retain these values without another implementation change. [2]

To reply, just mention @dosu.


Share context across your team and agents. Try Dosu.

Leave Feedback Learn about generator with Dosu Add Dosu to your team

dosubot[bot] avatar Nov 07 '26 17:11 dosubot[bot]

Hi maintainers (@Adi-204), I would like to work on this issue.

Root Cause Analysis:

In packages/helpers/src/discriminators.js (getMessageDiscriminatorData), the helper currently evaluates:

if (!discriminator_value) {
  return null;
}

This check treats valid schema constants that evaluate to falsy (such as boolean false, numeric 0, and empty string "") as missing, incorrectly returning null and omitting them from discriminator metadata.

Proposed Solution & Verification:

  • Update the condition in packages/helpers/src/discriminators.js to strictly check for missing values:
if (discriminator_value === undefined || discriminator_value === null) {
  return null;
}
  • Add unit test coverage in packages/helpers/test/discriminators.test.js verifying that boolean false, integer 0, empty string "", and null values are handled accurately.
  • All 7 test suites (109 tests) in @asyncapi/generator-helpers pass cleanly.
  • Add changeset patch for @asyncapi/generator-helpers.

Could you please review and /approve this issue so I can submit the PR? Thank you!

rajverrr avatar Nov 20 '26 13:11 rajverrr

Hi @rajverrr — thanks for the interest, but I raised this issue and the template already states that I’m willing to submit the PR. The usual flow is: find/report the bug, ask a maintainer to assign the issue to you, and only then open a PR — the bot enforces that. Please don’t claim an issue that’s already in progress.

Khush7Chauhan avatar Nov 22 '26 18:11 Khush7Chauhan

Thanks for letting me know, @Khush7Chauhan! Apologies for the overlap—all yours, and good luck with the PR!

rajverrr avatar Nov 23 '26 13:11 rajverrr