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

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

Closed
KhudaDad414 opened this issue Oct 12, 2023 · 5 comments
Closed
Labels
bug Something isn't working stale

Comments

@KhudaDad414
Copy link
Member

Describe the bug

SecurityRequirements object acts as a javascript array in some ways but not in all of the cases.
consider this example:

import Parser from "@asyncapi/parser"
const file = await Bun.file("asyncapi.yaml").text()

const parser = new Parser()

const { document, diagnostics } = await parser.parse(file)
const securityRequirements = document?.allServers()[0].security()[0]

if (!securityRequirements) {
  process.exit(0)
}
// Works fine
for (const security of securityRequirements) {
  console.log("In a for loop: ", security.jsonPath())
}

//Works fine
securityRequirements?.forEach((securityRequirement) => {
  console.log("in a forEach: ", securityRequirement.jsonPath())
})

//Throws error
securityRequirements?.map((securityRequirement) => {
  console.log(securityRequirement.jsonPath())
  return securityRequirement.jsonPath()
})

the last case will Throw:

1 | "use strict";
2 | Object.defineProperty(exports, "__esModule", { value: true });
3 | exports.Collection = void 0;
4 | class Collection extends Array {
5 |     constructor(collections, _meta = {}) {
6 |         super(...collections);
            ^
TypeError: Spread syntax requires ...iterable[Symbol.iterator] to be a function
      at new Collection (node_modules/@asyncapi/parser/cjs/models/collection.js:6:8)
      at new SecurityRequirements (:1:32)
      at map (:1:20)
@KhudaDad414 KhudaDad414 added the bug Something isn't working label Oct 12, 2023
@jonaslagoni
Copy link
Member

@KhudaDad414 something is off here.

const securityRequirements = document?.allServers()[0].security()[0]

is no longer an array but a specific entry in the document?.allServers()[0].security() array? 🤨

How this works fine:


// Works fine
for (const security of securityRequirements) {
  console.log("In a for loop: ", security.jsonPath())
}

//Works fine
securityRequirements?.forEach((securityRequirement) => {
  console.log("in a forEach: ", securityRequirement.jsonPath())
})

Does not really make sense to me 😅

@KhudaDad414
Copy link
Member Author

@jonaslagoni that's what I am saying. 😆

asyncapi.yaml

asyncapi: 3.0.0
info:
  title: Slack Websocket and OpenAI API
  version: 1.0.0
servers:
  OpenAI_HTTPS:
    host: api.openai.com
    protocol: https
    security:
      - $ref: '#/components/securitySchemes/my'
components:
  securitySchemes: 
    my:
      type: http
      scheme: bearer

index.ts

import Parser from "@asyncapi/parser"
const file = await Bun.file("asyncapi.yaml").text()

const parser = new Parser()

const { document, diagnostics } = await parser.parse(file)
if (!document) {
  console.log("error parsing the document", diagnostics)
}
const securityRequirements = document?.allServers()[0].security()[0]

if (!securityRequirements) {
  process.exit(0)
}
// Works fine
for (const security of securityRequirements) {
  console.log("In a for loop: ", security.jsonPath())
}

//Works fine
securityRequirements?.forEach((securityRequirement) => {
  console.log("in a forEach: ", securityRequirement.jsonPath())
})

//Throws error
try {
  securityRequirements?.map((securityRequirement) => {
    console.log(securityRequirement.jsonPath())
    return securityRequirement.jsonPath()
  })
} catch (e) {
  console.log("I throw errors.")
}

Output

In a for loop:  /servers/OpenAI_HTTPS/security/0
in a forEach:  /servers/OpenAI_HTTPS/security/0
I throw errors.

yes, it behaves the same way in node as well.

@KhudaDad414
Copy link
Member Author

the studio also misbehave in showing the correct security shemes which I think takes root from this issue.

asyncapi.yml

...
servers:
  Slack_WebSocket:
    host: wss-primary.slack.com
    protocol: wss
    security:
     - type: httpApiKey
       name: ticket
       in: query
     - type: httpApiKey
       name: app_id
       in: query
...

what studio shows:

Screenshot 2023-11-27 at 15 58 07

asyncapi.yml

...
servers:
  Slack_WebSocket:
    host: wss-primary.slack.com
    protocol: wss
    security:
      - $ref: "#/components/securitySchemes/ticket"
      - $ref: "#/components/securitySchemes/app_id"
...
components:
  securitySchemes:
    ticket:
      type: httpApiKey
      name: ticket
      in: query
    app_id:
      type: httpApiKey
      name: app_id
      in: query
...

what studio shows:

Screenshot 2023-11-27 at 16 03 35

what I expect to see in both cases:

Screenshot 2023-11-27 at 15 59 59

@KhudaDad414
Copy link
Member Author

@smoya any idea why this is happening?

Copy link

This issue has been automatically marked as stale because it has not had recent activity 😴

It will be closed in 120 days if no further activity occurs. To unstale this issue, add a comment with a detailed explanation.

There can be many reasons why some specific issue has no activity. The most probable cause is lack of time, not lack of interest. AsyncAPI Initiative is a Linux Foundation project not owned by a single for-profit company. It is a community-driven initiative ruled under open governance model.

Let us figure out together how to push this issue forward. Connect with us through one of many communication channels we established here.

Thank you for your patience ❤️

@github-actions github-actions bot added the stale label Mar 27, 2024
@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Jul 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working stale
Projects
None yet
Development

No branches or pull requests

2 participants