Skip to content

[BUG] optimize creates invalid documents #1990

Open

Description

@black-snow

Describe the bug.

asyncapi optimize --no-tty creates invalid documents.

It creates references like:

channels:
  A:
    $ref: '#/components/channels/A'
    messages:
      M:
        payload:
          allOf:
            - allOf:
                - $ref: '#/components/schemas/[0]'
# ...
components:
  schemas:
    '0':
      allOf:
        - $ref: '#/components/schemas/[0]'
        - $ref: '#/components/schemas/[1]'

But asyncapi validate won't accept this:

1978:17 error invalid-ref '#/components/schemas/[0]' does not exist components.schemas[0].allOf[0].$ref

And the self reference also doesn't seem right (schemas.0 to schemas.0).

It also creates schemas like this:

      type: object
      properties:
        type:
          $ref: '#/components/schemas/type'

Where it does, in fact, not create components.schemas.type.

I'll try to provide a minimal repro in a second.

Expected behavior

It should produce valid docs (3.0.0).

Screenshots

How to Reproduce

repro.yaml

asyncapi: 3.0.0
id: "urn:com:example"
info:
  title: x
  version: 1.4.0
operations:
  A:
    action: "send"
    description: ""
    channel:
      $ref: "#/channels/A"
    messages:
      - $ref: "#/channels/A/messages/A"
channels:
  A:
    address: ""
    title: ""
    messages:
      A:
        $ref: "#/components/messages/A"
components:
  messages:
    A:
      contentType: "application/json"
      payload:
        allOf:
          - $ref: "#/components/schemas/EventEnvelope"
          - $ref: "#/components/schemas/EnvelopeOverrides"
          - type: object
            properties:
              data:
                $ref: "#/components/schemas/A"
  schemas:
    A:
      description: ""
      type: object
      properties:
        a:
          type: string
          minLength: 1
          examples: [ "669e01d59ba47d8e3e21cf13" ]
      required: [ "a" ]
    EnvelopeOverrides:
      description: Envelope overrides for this event
      type: object
      properties:
        type:
          type: string
          const: "a"
      required: [ "type" ]
    EventEnvelope:
      allOf:
        - $ref: "https://raw.githubusercontent.com/cloudevents/spec/v1.0.2/cloudevents/formats/cloudevents.json"

asyncapi optimize --no-tty repro.yaml -o new-file

🖥️ Device Information [optional]

  • Operating System (OS): macOS
  • Browser: WF
  • Browser Version: latest

👀 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 ?

No, someone else can work on it

Activity

  1. Varadraj75 commented on Feb 1, 2026

    @Varadraj75
    Contributor

    hey @Shurtu-gal @Souvikns @AayushSaini101 ,

    Understanding

    • This appears to be a genuine bug in asyncapi optimize, not a usage issue.
    • The optimizer is generating invalid $ref values such as #/components/schemas/[0], which is not valid JSON Pointer syntax and is correctly rejected by asyncapi validate.
    • There are also related issues:
      • Missing guards against self-referencing schemas.
      • $refs being emitted without ensuring the referenced schema exists in components.schemas (e.g. #/components/schemas/type).

    Possible Approach

    • Replace index-based schema references with stable schema identifiers.
    • Prevent self-references during $ref generation.
    • Ensure referenced schemas are always materialized in components.schemas (or inline them when needed).

    Happy to help further once there is a minimal reproduction or guidance from maintainers.

  2. black-snow commented on Feb 2, 2026

    @black-snow
    Author

    Probably not strictly "minimal", but added a reproducible example.

    With asyncapi 6.0.0 (and 5.x) this produces:

    asyncapi: 3.0.0
    id: urn:com:example
    info:
      title: x
      version: 1.4.0
    operations:
      A:
        $ref: '#/components/operations/A'
    channels:
      A:
        $ref: '#/components/channels/A'
        messages:
          A:
            payload:
              allOf:
                - allOf:
                    - properties:
                        datacontenttype:
                          $ref: '#/components/schemas/datacontenttype'
                        id:
                          $ref: '#/components/schemas/id'
    components:
      messages:
        A:
          contentType: application/json
          payload:
            allOf:
              - $ref: '#/components/schemas/EventEnvelope'
              - $ref: '#/components/schemas/EnvelopeOverrides'
              - type: object
                properties:
                  data:
                    $ref: '#/components/schemas/A'
      schemas:
        '0':
          properties:
            datacontenttype:
              $ref: '#/components/schemas/datacontenttype'
            specversion:
              $ref: '#/components/schemas/specversion'
            data_base64:
              $ref: '#/components/schemas/data_base64'
            dataschema:
              $ref: '#/components/schemas/dataschema'
            subject:
              $ref: '#/components/schemas/subject'
            source:
              $ref: '#/components/schemas/source'
            type:
              $ref: '#/components/schemas/type'
            time:
              $ref: '#/components/schemas/time'
            data:
              $ref: '#/components/schemas/data'
            id:
              $ref: '#/components/schemas/id'
        '2':
          properties:
            data:
              properties:
                a:
                  $ref: '#/components/schemas/a'
        A:
          description: ''
          type: object
          properties:
            a:
              type: string
              minLength: 1
              examples:
                - 669e01d59ba47d8e3e21cf13
          required:
            - a
        EnvelopeOverrides:
          description: Envelope overrides for this event
          type: object
          properties:
            type:
              type: string
              const: a
          required:
            - type
        EventEnvelope:
          allOf:
            - $ref: >-
                https://raw.githubusercontent.com/cloudevents/spec/v1.0.2/cloudevents/formats/cloudevents.json
      operations:
        A:
          action: send
          description: ''
          channel:
            $ref: '#/channels/A'
          messages:
            - $ref: '#/channels/A/messages/A'
      channels:
        A:
          address: ''
          title: ''
          messages:
            A:
              $ref: '#/components/messages/A'
              payload:
                allOf:
                  - allOf:
                      - $ref: '#/components/schemas/[0]'
                  - properties:
                      type:
                        $ref: '#/components/schemas/type'
                  - $ref: '#/components/schemas/[2]'
    
  3. aeworxet commented on Apr 20, 2026

    @aeworxet
    Contributor

    This is a bug in https://github.com/asyncapi/optimizer and the issue should be moved there.

    It is related to the schemas functionality of Parser, which should be examined and likely fixed in https://github.com/asyncapi/parser-js.

    The current behavior of the schemas functionality in Parser is a known blocker for several issues in Optimizer already: asyncapi/optimizer#291 (comment).


    Workaround: Disabling schemas in most cases instructs Optimizer to produce correct output (for YAMLs whose property names do not contain a ., which is the subject of asyncapi/optimizer#273 and asyncapi/optimizer#291).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions