event-catalog / generator-asyncapi

AsyncAPI generator for EventCatalog
https://www.eventcatalog.dev/docs/development/integrations/async-api/intro
Other
9 stars 3 forks source link

use fromFile reader to allow file references in asyncapi specs #19

Closed vsinger closed 1 week ago

vsinger commented 1 week ago

Hi!

Motivation

While checking EventCatalog v2 with my AsyncAPI specs I got a bunch of errors during generation. All of them of type code: 'invalid-ref' because referenced files in the AsyncAPI spec could not be found due to relative paths. When using parser.parse() function it expects all files to be relative to the EventCatalog project folder (as far as I understood it) where I run npm run generate. This can be solved by using the fromFile() function from the @asyncapi/parser NPM package which internally sets the relative paths correctly and allows to parse AsyncAPI specs with relative paths.

Let me know if there is anything else to do :)

changeset-bot[bot] commented 1 week ago

⚠️ No Changeset found

Latest commit: 6a5056d3a249e8219eab47b35f8dce8d845d608d

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

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

boyney123 commented 1 week ago

Thank you @vsinger , do you have an example AsyncAPI file I can use in this case to add to our tests?

IsmaelMartinez commented 1 week ago

Example asyncapi

It might not be fully correct, just craft it a bit quickly

---
asyncapi: 3.0.0
info:
  title: test
  description: description
  version: 1.1.0
  contact:
    name: me 
    email: myoldemail@hotmail.com

channels: 
  randomChannel:
    address: myAddress
    messages:
      eventMetadata:
        $ref: '#/components/schemas/EventMetadata/schema'
operations: 
  sendNoise:
    action: send
    channel:
      $ref: '#/channels/randomChannel'
components:
  schemas:
    EventMetadata:
      schemaFormat: 'application/schema+json;version=draft-07'
      schema:
        "$ref": './EventMetadata.schema.json'

Example JSONSchema file for EventMetadata (that should be correct)

{
    "$id": "https://plgtech.io/schemas/eventMetadata-v1",
    "$schema": "http://json-schema.org/draft-07/schema#",
    "title": "EventMetadata",
    "description": "This is the EventMetadata",
    "type": "object",
    "required": [
      "eventId",
      "traceId",
      "timestamp"
    ],
    "properties": {
      "eventId": {
        "type": "string"
      },
      "eventVersion": {
        "type": "string"
      },
      "traceId": {
        "type": "string"
      },
      "timestamp": {
        "type": "string",
        "format": "date-time"
      },
      "tenantId": {
        "type": "string"
      }
    }
  }

Error I get:

image

vsinger commented 1 week ago

@boyney123 The example of @IsmaelMartinez is one. Thanks for that @IsmaelMartinez. Any file references in the AsyncAPI spec cause this errors.

But there is still an issue with this PR. The content of the AsyncAPI which is attached to the service is wrong as it contains the relative paths. I need to check how to get the fully parsed AsyncAPI spec without the file references /relative paths.

vsinger commented 1 week ago

@boyney123 I couldn't find a better way than use the document.meta().asyncapi.parsed value and set convert it to a valid yaml without references with js-yaml. I'm not really into JS :D Let me know if this is ok :)

boyney123 commented 1 week ago

Hey @vsinger

I Found a fix #20, let me know if this works for you. I added a test too. Think it requires your external files to be in a format tho. See the tests.

boyney123 commented 1 week ago

@boyney123 The example of @IsmaelMartinez is one. Thanks for that @IsmaelMartinez. Any file references in the AsyncAPI spec cause this errors.

But there is still an issue with this PR. The content of the AsyncAPI which is attached to the service is wrong as it contains the relative paths. I need to check how to get the fully parsed AsyncAPI spec without the file references /relative paths.

Ah yeah makes sense, let me have a look too

boyney123 commented 1 week ago

Thank you for raising these @vsinger , I have fixed this in #22.

Going to update the AsyncAPI example with how this all works, and the docs too! Will ping back when done.

vsinger commented 1 week ago

@boyney123 thanks for the quick response. There is a debug console.log statement which logs the whole parsed AsyncAPI spec. Will lead to lots of logs but the fix looks good.

boyney123 commented 1 week ago

Haha, o dear! Thanks! Glad fix is looking good! Let me know if you need any more help, or if you ever wanna sync for 30min be curious what you are up too and how Catalog can help!