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

Exception is thrown if $ref value is not a string #92

Closed
aeworxet opened this issue Oct 31, 2022 · 2 comments
Closed

Exception is thrown if $ref value is not a string #92

aeworxet opened this issue Oct 31, 2022 · 2 comments

Comments

@aeworxet
Copy link
Collaborator

Issue is reported by user in
https://asyncapi.slack.com/archives/CQVJXFNQL/p1666700935030749

Shani Alaluf Shamah
We have an issue in bundle version 0.3.4 using parser 1.17.1
When adding the feature referenceIntoComponents: true
we get the following error:

TypeError: Cannot read property 'startsWith' of undefined
    at isExternalReference (/Users/shani.shamah/IdeaProjects/data-model-schemas/node_modules/@asyncapi/bundler/lib/parser.js:41:17)
    at /Users/shani.shamah/IdeaProjects/data-model-schemas/node_modules/@asyncapi/bundler/lib/parser.js:58:13
    at Array.forEach (<anonymous>)
    at resolveExternalRefs (/Users/shani.shamah/IdeaProjects/data-model-schemas/node_modules/@asyncapi/bundler/lib/parser.js:56:8)
    at parse (/Users/shani.shamah/IdeaProjects/data-model-schemas/node_modules/@asyncapi/bundler/lib/parser.js:80:43)
    at async resolve (/Users/shani.shamah/IdeaProjects/data-model-schemas/node_modules/@asyncapi/bundler/lib/util.js:62:13)
    at async bundle (/Users/shani.shamah/IdeaProjects/data-model-schemas/node_modules/@asyncapi/bundler/lib/index.js:42:27)
    at async createApiSpecforService (/Users/shani.shamah/IdeaProjects/data-model-schemas/scripts/generate-api-spec.js:12:15) {stack: 'TypeError: Cannot read property 'startsWith' …l-schemas/scripts/generate-api-spec.js:12:15)', message: 'Cannot read property 'startsWith' of undefined'}

it doesn’t work with 0.3.3 either

With 0.2.3 I am getting the following error:

TypeError: bundle is not a function

I’m using JS, require (CJS) syntax:

const bundle = require('@asyncapi/bundler').default;

switched to 0.2.3 with

const bundle = require("@asyncapi/bundler")

but still getting the Cannot read property 'startsWith' of undefined error.

Maciej Urbańczyk
https://github.com/asyncapi/bundler/blob/master/src/parser.ts#L44 we should check if $ref exists and then check if starts with #
in old code this same https://github.com/Souvikns/bundler/blob/ef574b8620f12c075d89bfc742070748941a45e0/lib/parser.js#L51 😅

Lukasz Gornicki
@shani Alaluf Shamah
ok so mistery solved
imho change is to just extend condition from https://github.com/asyncapi/bundler/blob/master/src/parser.ts#L64, to if (ref && isExternalReference(ref))

Maciej Urbańczyk
we use isExternalReference in several places, we should check if $ref exist inside isExternalReference function:

function isExternalReference(ref?: string): boolean {
  return typeof ref === 'string' && !ref.startsWith('#');
}

Lukasz Gornicki
makes sense 👍:skin-tone-2:

@aeworxet
Copy link
Collaborator Author

Shani Alaluf Shamah
Can you please clarify what value are you passing to $ref, because I have tried passing to $ref in YAML

  • array, YAML object, number, boolean, but all I get is
TypeError: ref.startsWith is not a function
  • with absence of value (NOT '') and null I get
TypeError: Cannot read properties of null (reading 'startsWith')
  • with an empty string '' the value is simply ignored (no exception thrown, execution successful)

  • with ./, /, https..., function, undefined Node.js gives various errors of failing to open or fetch file.

But no TypeError: Cannot read property 'startsWith' of undefined

So for now I cannot reproduce exactly your bug, to begin fixing it, on Node.js v16.17.0 and Node.js v18.9.0.

Maciej Urbańczyk solution

function isExternalReference(ref: string): boolean {
  return typeof ref === 'string' && !ref.startsWith('#');
}

works - it makes bundler ignore any value that is not a string, thus might fix exactly your issue indirectly, so it definitely should be merged.

After this change is merged, please comment if exactly your issue was fixed.

aeworxet added a commit to aeworxet/asyncapi-bundler that referenced this issue Nov 1, 2022
aeworxet added a commit to aeworxet/asyncapi-bundler that referenced this issue Nov 7, 2022
aeworxet added a commit to aeworxet/asyncapi-bundler that referenced this issue Nov 7, 2022
aeworxet added a commit to aeworxet/asyncapi-bundler that referenced this issue Nov 7, 2022
aeworxet added a commit to aeworxet/asyncapi-bundler that referenced this issue Nov 8, 2022
@aeworxet
Copy link
Collaborator Author

User did not provide feedback, assuming issue fixed, resolved and closed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant