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

Open Beginner friendly
#2,026 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
72/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Quiet
Tech stack
typescript
Domain
cli

Research direction

Start in src/domains/services/archiver.service.ts at appendAsyncAPIDocument, then reproduce the issue with test-archiver.ts using npx ts-node test-archiver.ts. Done means the generated ZIP's asyncapi.yml preserves the supplied YAML document as valid YAML rather than a quoted, escaped string.

Written by the indexing model from the issue text.

Description

bug stale
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();
  1. Run the test script from the project root:
npx ts-node test-archiver.ts
  1. Allow the archiver service to generate the ZIP archive.
  2. Open the generated ZIP file.
  3. 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?
  • I checked and didn't find similar issue
🏢 Have you read the Contributing Guidelines?
Are you willing to work on this issue ?

Yes I am willing to submit a PR!

Dominant language
TypeScript
Stars
274
Forks
376
Avg merge
12h 11m
Merged PRs (30d)
11

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from asyncapi/cli

All issues in asyncapi/cli

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.