Skip to content

fix: Support slash-based branch names and multi-dot spec filenames (fixes #1940)#2154

Open
drsophieservice-lang wants to merge 1 commit into
asyncapi:masterfrom
drsophieservice-lang:fix/issue-1940-slash-branch-multidot-extension
Open

fix: Support slash-based branch names and multi-dot spec filenames (fixes #1940)#2154
drsophieservice-lang wants to merge 1 commit into
asyncapi:masterfrom
drsophieservice-lang:fix/issue-1940-slash-branch-multidot-extension

Conversation

@drsophieservice-lang
Copy link
Copy Markdown

Summary

This PR fixes two bugs in the AsyncAPI CLI that caused valid inputs to be rejected:

  1. GitHub URL parsing — Branches containing slashes (e.g., feature/new-validation) were parsed incorrectly, leading to 404 errors.
  2. File extension detection — Filenames with multiple dots (e.g., my.asyncapi.yaml) had the wrong extension extracted, causing valid files to be rejected.

Changes

src/domains/services/validation.service.ts

Bug (before):

// eslint-disable-next-line no-useless-escape
const githubWebPattern = /^\/\/github\.com\/([^\/]+)\/([^\/]+)\/blob\/([^\/]+)\/(.+)$/;
const match = urlWithoutFragment.match(githubWebPattern);

if (match) {
  const [, owner, repo, branch, filePath] = match;
  return `https://api.github.com/repos/${owner}/${repo}/contents/${filePath}?ref=${branch}`;
}

The third capture group ([^\/]+) stopped at the first / in branch names. For a URL like https://github.com/org/repo/blob/feature/new-validation/spec.yaml, the branch was incorrectly parsed as feature instead of feature/new-validation.

Fix (after):

// eslint-disable-next-line no-useless-escape
const githubWebPattern = /^\/\/github\.com\/([^\/]+)\/([^\/]+)\/blob\/(.+)$/;
const match = urlWithoutFragment.match(githubWebPattern);

if (match) {
  const [, owner, repo, branchAndPath] = match;
  // Convert to raw.githubusercontent.com which accepts branch+path without splitting
  return `https://raw.githubusercontent.com/${owner}/${repo}/${branchAndPath}`;
}

Since it is impossible to reliably split a branch name from a file path using only regex (when branches contain slashes), we convert to raw.githubusercontent.com format instead, which accepts the combined branch/path string directly.

src/domains/models/SpecificationFile.ts

Bug (before):

const extension = name.split('.')[1];

For a filename like my.asyncapi.yaml, split('.')[1] returns asyncapi instead of yaml.

Fix (after):

const extension = path.extname(name).replace(/^\./, '');

path.extname('my.asyncapi.yaml') correctly returns .yaml.

Test Cases

New test file: test/github-url-parsing.test.ts with 14 test cases.

GitHub URL Parsing

Input URL Before (buggy) After (fixed)
…/blob/main/spec.yaml …?ref=main raw.githubusercontent.com/…/main/spec.yaml
…/blob/feature/new-validation/spec.yaml …?ref=feature (404) raw.githubusercontent.com/…/feature/new-validation/spec.yaml
…/blob/release/v2/beta/spec/asyncapi.yaml …?ref=release (404) raw.githubusercontent.com/…/release/v2/beta/spec/asyncapi.yaml
…/blob/feat/issue-42/docs/api/spec.yaml#L10 Incorrect branch Correct, fragment stripped

File Extension Detection

Filename Before (buggy) After (fixed)
asyncapi.yaml yaml yaml
my.asyncapi.yaml asyncapi yaml
spec.v2.json v2 json
asyncapi (no ext) undefined (crash risk) '' (safe)

Testing

npx jest test/github-url-parsing.test.ts

Related Issues

Closes #1940

…ixes asyncapi#1940)

- Replace greedy regex in convertGitHubWebUrl to handle branches with slashes
  by converting to raw.githubusercontent.com format instead of GitHub API format
- Replace name.split('.')[1] with path.extname() in fileExists() to correctly
  extract the last extension from filenames with multiple dots
- Add test file test/github-url-parsing.test.ts with 14 test cases
@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented May 1, 2026

⚠️ No Changeset found

Latest commit: 076431a

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@sonarqubecloud
Copy link
Copy Markdown

sonarqubecloud Bot commented May 1, 2026

Quality Gate Failed Quality Gate failed

Failed conditions
D Maintainability Rating on New Code (required ≥ A)

See analysis details on SonarQube Cloud

Catch issues before they fail your Quality Gate with our IDE extension SonarQube for IDE

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: To Triage

Development

Successfully merging this pull request may close these issues.

[BUG] CLI fails for GitHub URLs with slash-based branches and multi-dot spec files

1 participant