asyncapi / asyncapi/optimizer

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

Open
#273 10 comments 0 reactions 0 assignees View on GitHub
bug stale
Dominant language
TypeScript
Stars
12
Forks
11
PR merge metrics
No merged PRs in 30d

Description

### 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
```yaml
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

```yaml
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
```yaml
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:
https://github.com/asyncapi/optimizer/blob/fbc19f5450540a525f399e84711267dec3860b88/src/Optimizer.ts#L150
https://github.com/asyncapi/optimizer/blob/fbc19f5450540a525f399e84711267dec3860b88/src/ComponentProvider.ts#L32

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.