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

[BUG] stringify does not create a valid asyncApi-document (invalid $ref format) #1074

Open
2 tasks done
vglaeser opened this issue Jan 16, 2025 · 1 comment
Open
2 tasks done
Labels
bug Something isn't working

Comments

@vglaeser
Copy link

vglaeser commented Jan 16, 2025

Describe the bug.

I want to use the parser to load, modify and save an asyncApi.yaml.
Initially, I expected stringify(parse(file)) to equal the input-file. Unfortunately, referenced objects are not placed where they would make sense (and where they originally were) but where they first appear.
Moreover, the result does not even seem to be a valid asyncApi-document.

Do you have any ideas, how to generate a valid asyncApi-file?

Expected behavior

I would expect stringify(parse(file)) to be valid and at least similar to the input.
Further, defining schemas where they first appear in the document and reference them from #/components/schemas seems wrong to me.

Screenshots

none required

How to Reproduce

Consider the following example:

asyncApi_min_v3.yaml
asyncapi: 3.0.0
info:
  title: Streetlights App
  version: '1.0.0'
  description: |
    The Smartylighting Streetlights application allows you
    to remotely manage the city lights.
  license:
    name: Apache 2.0
    url: 'https://www.apache.org/licenses/LICENSE-2.0'
servers:
  mosquitto:
    host: test.mosquitto.org
    protocol: mqtt
channels:
  lightMeasured:
    address: 'light/measured'
    servers:
      - $ref: '#/servers/mosquitto'
    messages:
      lightMeasuredMessage:
        $ref: '#/components/messages/lightMeasuredMessage'
operations:
  onLightMeasured:
    action: 'receive'
    summary: Information about environmental lighting conditions for a particular streetlight.
    channel:
      $ref: '#/channels/lightMeasured'
components:
  messages:
    lightMeasuredMessage:
      name: LightMeasured
      payload:
        $ref: '#/components/schemas/LightMeasured'
  schemas:
    LightMeasured:
      type: object
      properties:
        id:
          type: integer
          minimum: 0
          description: ID of the streetlight.
        lumens:
          type: integer
          minimum: 0
          description: Light intensity measured in lumens.
        color:
          $ref: '#/components/schemas/Color'
    Color:
      type: object
      properties:
        r:
          type: integer
          minimum: 0
          maximum: 255
        g:
          type: integer
          minimum: 0
          maximum: 255
        b:
          type: integer
          minimum: 0
          maximum: 255
      required:
        - r
        - g
        - b

When I run:

import {fromFile, Parser, stringify} from '@asyncapi/parser'
import {writeFileSync} from "fs";
import {stringify as yamlStringify} from 'yaml';

const parser = new Parser();
const parsed = await fromFile(parser, './asyncApi_min_v3.yaml').parse();
writeFileSync('./asyncApi_parsed-and-stringified.yaml', yamlStringify(JSON.parse(stringify(parsed.document)!)));

the following file is created:

asyncApi_parsed-and-stringified.yaml
asyncapi: 3.0.0
info:
  title: Streetlights App
  version: 1.0.0
  description: |
    The Smartylighting Streetlights application allows you
    to remotely manage the city lights.
  license:
    name: Apache 2.0
    url: https://www.apache.org/licenses/LICENSE-2.0
servers:
  mosquitto:
    host: test.mosquitto.org
    protocol: mqtt
channels:
  lightMeasured:
    address: light/measured
    servers:
      - $ref:$.servers.mosquitto
    messages:
      lightMeasuredMessage:
        name: LightMeasured
        payload:
          type: object
          properties:
            id:
              type: integer
              minimum: 0
              description: ID of the streetlight.
              x-parser-schema-id: <anonymous-schema-1>
            lumens:
              type: integer
              minimum: 0
              description: Light intensity measured in lumens.
              x-parser-schema-id: <anonymous-schema-2>
            color:
              type: object
              properties:
                r:
                  type: integer
                  minimum: 0
                  maximum: 255
                  x-parser-schema-id: <anonymous-schema-3>
                g:
                  type: integer
                  minimum: 0
                  maximum: 255
                  x-parser-schema-id: <anonymous-schema-4>
                b:
                  type: integer
                  minimum: 0
                  maximum: 255
                  x-parser-schema-id: <anonymous-schema-5>
              required:
                - r
                - g
                - b
              x-parser-schema-id: Color
          x-parser-schema-id: LightMeasured
        x-parser-unique-object-id: lightMeasuredMessage
    x-parser-unique-object-id: lightMeasured
operations:
  onLightMeasured:
    action: receive
    summary: Information about environmental lighting conditions for a particular
      streetlight.
    channel: $ref:$.channels.lightMeasured
    x-parser-unique-object-id: onLightMeasured
components:
  messages:
    lightMeasuredMessage: $ref:$.channels.lightMeasured.messages.lightMeasuredMessage
  schemas:
    LightMeasured: $ref:$.channels.lightMeasured.messages.lightMeasuredMessage.payload
    Color: $ref:$.channels.lightMeasured.messages.lightMeasuredMessage.payload.properties.color
x-parser-spec-parsed: true
x-parser-api-version: 3
x-parser-spec-stringified: true

This is not accepted by asyncapi studio. To be valid, I need to adjust the references from

channel: $ref:$.channels.lightMeasured

to

channel: 
    $ref: '#/channels/lightMeasured'

(add line-break, add space after $ref:, add '', use #/ instead of $.)

Please correct me, if I'm doing something wrong.

🥦 Browser

None

👀 Have you checked for similar open issues?

  • I checked and didn't find similar issue

🏢 Have you read the Contributing Guidelines?

Are you willing to work on this issue ?

None

Thank you in advance

@vglaeser vglaeser added the bug Something isn't working label Jan 16, 2025
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.

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

No branches or pull requests

1 participant