asyncapi / asyncapi/cli

[BUG] : AsyncAPI document is double-stringified in generated ZIP output

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

Description

### Describe the bug.

In `src/domains/services/archiver.service.ts`, the method `appendAsyncAPIDocument` always applies:
```
asyncapi = JSON.stringify(asyncapi);
```
even when asyncapi is already a YAML or JSON string.

Because of this:
- The original document is converted into a JSON string literal.
- Newlines are escaped (\n) and the entire content is wrapped in quotes.
- The archived asyncapi.yml therefore contains a serialized string instead of a valid AsyncAPI document.

### Expected behavior

For the test file mentioned in How to Reproduce section
The file should contain valid YAML:
```
asyncapi: 2.6.0
info:
title: Example
version: 1.0.0
```

**Proposed fix**
We can update the method to stringify only when the input is an object:
```
public appendAsyncAPIDocument(
archive: Archiver,
asyncapi: string | object,
fileName = 'asyncapi',
) {
const content =
typeof asyncapi === 'string'
? asyncapi
: JSON.stringify(asyncapi, null, 2);

const language = retrieveLangauge(content);
const extension = language === 'yaml' ? 'yml' : 'json';

archive.append(content, { name: `${fileName}.${extension}` });
}
```

### Screenshots

Image

### How to Reproduce

1. Create a test file named test-archiver.ts. Inside the test, pass an AsyncAPI document as a string:
```
import * as fs from 'fs';
import archiver from 'archiver';
import { ArchiverService } from './src/domains/services/archiver.service';

async function run() {
const output = fs.createWriteStream('test.zip');
const archive = archiver('zip');

archive.pipe(output);

const service = new ArchiverService();

const asyncapiString =
"asyncapi: 2.6.0\ninfo:\n title: Example\n version: 1.0.0";

service.appendAsyncAPIDocument(archive, asyncapiString);

await archive.finalize();

console.log('ZIP created');
}

run();
```
2. Run the test script from the project root:
```
npx ts-node test-archiver.ts
```
3. Allow the archiver service to generate the ZIP archive.
4. Open the generated ZIP file.
5. Open `asyncapi.yml` inside the archive.

### 🖥️ Device Information [optional]

- Operating System (OS): Windows 10
- Generator version : 3.0.1

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

Yes I am willing to submit a PR!

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.