Skip to content

[BUG] optimize creates invalid documents #1990

@black-snow

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    Status

    To Triage

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions