Skip to content
This repository has been archived by the owner on Apr 11, 2024. It is now read-only.

Cannot resolve relative references when base directory is different #67

Closed
jonaslagoni opened this issue Nov 11, 2021 · 2 comments
Closed
Labels
bug Something isn't working

Comments

@jonaslagoni
Copy link

jonaslagoni commented Nov 11, 2021

Describe the bug

If you try to use the library with an AsyncAPI file that uses relative references and you are not in the same directory (when running the library) it will fail to fetch the correct file.

How to Reproduce

If your setup is:

  • cupid
    • index.js
  • asyncapi.yaml
  • dictionary.yaml

Where asyncapi.yaml reference the dictionary.yaml, using a relative reference such as ./dictionary.yaml the library throws an error that the file cannot be found because it tries to look for it here: cupid/dictionary.yaml.

asyncapi.yaml file:

asyncapi: 2.2.0
info:
  title: Website Backend
  version: 1.0.0
channels:
  comment/liked:
    description: Notify all the services that a comment has been liked.
    subscribe: 
      message:
        $ref: './dictionary.yaml#/components/messages/commentLiked'

dictionary.yaml file:

components:
  messages:
    commentLiked: 
      payload: 
        $ref: '#/components/schemas/commentLikedPayload'
  schemas: 
    commentLikedPayload: 
      type: object
      title: commentLikedPayload
      additionalProperties: false
      properties:
        commentId: 
          allOf: 
            - $ref: '#/components/schemas/commentId'
            - description: Id of the comment that was liked
    commentId:
      type: string

Expected behavior

Expected when the asyncapi.yaml file is parsed, that it is parsed from it's base directory.

@jonaslagoni jonaslagoni added the bug Something isn't working label Nov 11, 2021
@github-actions
Copy link

Welcome to AsyncAPI. Thanks a lot for reporting your first issue. Please check out our contributors guide and the instructions about a basic recommended setup useful for opening a pull request.
Keep in mind there are also other channels you can use to interact with AsyncAPI community. For more details check out this issue.

@jonaslagoni
Copy link
Author

So this is not really a bug, more a feature request 😅

My current implementation was:

const cupid = require('@asyncapi/cupid');
const path = require('path');
const fs = require('fs');

async function getAsyncApiExamples() {
  const docs = [];
  const files = [
      path.resolve(__dirname, '../asyncapi.yaml'),
  ]
  for (const file of files) {
    const asyncApiDoc = fs.readFileSync(file, 'utf8');
    docs.push(asyncApiDoc);
  }
  try {
    const mermaidFlowchart = await cupid.getRelations(docs,{syntax:'mermaid'});
    console.log(mermaidFlowchart);
  } catch (error) {
    console.error(error);
  }
}
getAsyncApiExamples();

But altering it to pre-parse the AsyncAPI documents with the custom path option solved this problem.

const cupid = require('@asyncapi/cupid');
const path = require('path');
const fs = require('fs');
const parser = require('@asyncapi/parser');

async function getAsyncApiExamples() {
  const docs = [];
  const files = [
      path.resolve(__dirname, '../asyncapi.yaml'),
  ]
  for (const file of files) {
    const asyncApiDoc = fs.readFileSync(file, 'utf8');
    const parsedDoc = await parser.parse(asyncApiDoc, {path: file});
    docs.push(parsedDoc);
  }
  try {
    const mermaidFlowchart = await cupid.getRelations(docs,{syntax:'mermaid'});
    console.log(mermaidFlowchart);
  } catch (error) {
    console.error(error);
  }
}
getAsyncApiExamples();

This is because that we have no way to pass any options to the parser that Cupid uses.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant