Skip to content

Optimizer doesn't remove component that has a period in its name #273

Description

@olivierhsta

Describe the bug.

I ran the optimizer on my valid asyncapi file and the output file still contained unused messages. I tried to reduce the issue to the minimum and realized it seems to be an issue with periods (.) in component names.

Here is a asyncapi file that reproduces the bug

asyncapi: 3.0.0
info:
  title: my-app
  version: 1.0.0
channels:
  some_channel:
    address: some_address
    messages:
      Some-Event:
        $ref: '#/components/messages/Some-Event'
operations:
  receiveEvent:
    channel:
      $ref: '#/channels/some_channel'
    action: receive
components:
  messages:
    Some-Event:
      payload:
        type: object
        properties:
          id:
            type: string
    Unused-Event-1.0:
      payload:
        type: object
        properties:
          foo:
            type: object
            $ref: '#/components/schemas/Unused-Schema'
          bar:
            type: array
            items:
              $ref: '#/components/schemas/Unused-Schema-2.0'
  schemas:
    Unused-Schema:
      type: object
      properties:
        name:
          type: string
    Unused-Schema-2.0:
      type: object
      properties:
        name:
          type: string

When running the optimizer, it correctly reports that some components can be removed

3 unused components can be removed.
the following changes will be made:
remove components.messages.Unused-Event-1.0.
remove components.schemas.Unused-Schema-1.0.
remove components.schemas.Unused-Schema.

But when applying (with the option "remove components"), the resulting yaml is as follows

asyncapi: 3.0.0
info:
  title: my-app
  version: 1.0.0
channels:
  some_channel:
    address: some_address
    messages:
      Some-Event:
        $ref: '#/components/messages/Some-Event'
operations:
  receiveEvent:
    channel:
      $ref: '#/channels/some_channel'
    action: receive
components:
  messages:
    Some-Event:
      payload:
        type: object
        properties:
          id:
            type: string
    Unused-Event-1.0:
      payload:
        type: object
        properties:
          foo:
            type: object
            $ref: '#/components/schemas/Unused-Schema'
          bar:
            type: array
            items:
              $ref: '#/components/schemas/Unused-Schema-2.0'
  schemas:
    Unused-Schema-2.0:
      type: object
      properties:
        name:
          type: string

(Notice it deleted the "Unused-Schema" but its reference is still in the messages because Unused-Event-1.0 wasn't deleted. this means the whole file is invalid)

I have not seen any mention in the asyncapi specification that says component names should not contain periods, so I think this should be expected to work.

Expected behavior

The output file should only contain used components

asyncapi: 3.0.0
info:
  title: my-app
  version: 1.0.0
channels:
  some_channel:
    address: some_address
    messages:
      Some-Event:
        $ref: '#/components/messages/Some-Event'
operations:
  receiveEvent:
    channel:
      $ref: '#/channels/some_channel'
    action: receive
components:
  messages:
    Some-Event:
      payload:
        type: object
        properties:
          id:
            type: string

How to Reproduce

Run the optimiser on the given AsyncAPI file.

Are you willing to work on this issue ?

No, but I did notice there is periods used as delimiter in the code. Might be a lead

For instance:

const parentPath = childPath.substring(0, childPath.lastIndexOf('.'))

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions