asyncapi / asyncapi/cli

[BUG] optimize creates invalid documents

Open
#1,990 3 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
TypeScript
Stars
272
Forks
375
Avg merge
3h 22m
Merged PRs (30d)
8

Description

### Describe the bug.

`asyncapi optimize --no-tty` creates invalid documents.

It creates references like:
```yml
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:
```yaml
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`
```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?

- [x] I checked and didn't find similar issue

### 🏢 Have you read the Contributing Guidelines?

- [x] I have read the [Contributing Guidelines](https://github.com/asyncapi/.github/blob/master/CONTRIBUTING.md)

### Are you willing to work on this issue ?

No, someone else can work on it

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.