Skip to content

fix: support native array methods on collections - #1217

Merged
princerajpoot20 merged 2 commits into
asyncapi:masterfrom
AayushSaini101:874
Aug 6, 2026
Merged

fix: support native array methods on collections#1217
princerajpoot20 merged 2 commits into
asyncapi:masterfrom
AayushSaini101:874

Conversation

@AayushSaini101

Copy link
Copy Markdown
Collaborator

closes: #874

Summary

  • Fix native Array methods (.map(), .filter(), .slice()) on Collection subclasses such as SecurityRequirements
  • Root cause: Array transforms construct results via the subclass constructor with a length number; Collection then does super(...collections) and throws
  • Fix: set Symbol.species to Array so transforms return plain arrays (same pattern as all() / filterBy())

Problem

securityRequirements.forEach(...) // works
securityRequirements.map(...)     // throws
// TypeError: Spread syntax requires ...iterable[Symbol.iterator] to be a function

Workaround today: securityRequirements.all().map(...).

This affects every Collection subclass (Servers, Channels, Messages, SecurityRequirements, …), not only security.

Solution

In packages/parser/src/models/collection.ts:

static get [Symbol.species]() {
  return Array;
}

Native transforms no longer reconstruct a Collection with an invalid constructor signature.

Test plan

  • collection.spec.ts.map() / .filter() / .slice() return plain arrays; source/meta() unchanged
  • security-requirements.spec.ts.map() does not throw (issue reproduction)
  • Existing Collection helpers (all, filterBy, get, has) still pass
  • CI green on PR

Changeset

Patch for @asyncapi/parser: fix: support native array methods on collections

References

Co-authored-by: Cursor <cursoragent@cursor.com>
@changeset-bot

changeset-bot Bot commented Aug 6, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: cf16e8b

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@asyncapi/parser Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@sonarqubecloud

sonarqubecloud Bot commented Aug 6, 2026

Copy link
Copy Markdown

❌ The last analysis has failed.

See analysis details on SonarQube Cloud

@princerajpoot20
princerajpoot20 merged commit 03d869c into asyncapi:master Aug 6, 2026
10 checks passed
@github-actions github-actions Bot added the microgrant Participation in the Microgrant Program label Aug 7, 2026
@aeworxet

aeworxet commented Aug 7, 2026

Copy link
Copy Markdown
Contributor

@asyncapi/microgrant_team

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

microgrant Participation in the Microgrant Program

Projects

Status: Completed

Development

Successfully merging this pull request may close these issues.

javascript map() function doesn't work on SecurityRequirements object.

3 participants