Repository: Redocly/redocly-cli
Pull request #1226 (2023-08-14 10:46:33)

Fix/wrong location in no trailing slash rule
## What/Why/How?

Fixed the wrong location pointer when reporting on the 'no-path-trailing-slash' rule. Previously it was pointing on a value instead of a key when using $refs.

## Reference

## Testing

## Screenshots (optional)

## Check yourself

- [x] Code is linted
- [ ] Tested with redoc/reference-docs/workflows
- [x] All new/updated code is covered with tests

## Security

- [x] Security impact of change has been considered
- [x] Code follows company security practices and guidelines


changeset-bot[bot]: ###  🦋  Changeset detected

Latest commit: 7c1fca016017f0830d1b03c0d40b15a2164f07ac

**The changes in this PR will be included in the next version bump.**

<details><summary>This PR includes changesets to release 2 packages</summary>

  | Name                  | Type  |
| --------------------- | ----- |
| @redocly/openapi-core | Patch |
| @redocly/cli          | Patch |

</details>

Not sure what this means? [Click here  to learn what changesets are](https://github.com/changesets/changesets/blob/main/docs/adding-a-changeset.md).

[Click here if you're a maintainer who wants to add another changeset to this PR](https://github.com/Redocly/redocly-cli/new/fix/wrong-location-in-no-trailing-slash-rule?filename=.changeset/kind-experts-fly.md&value=---%0A%22%40redocly%2Fopenapi-core%22%3A%20patch%0A---%0A%0AFix%2Fwrong%20location%20in%20no%20trailing%20slash%20rule%0A)


github-actions[bot]: | Command | Mean [s] | Min [s] | Max [s] | Relative |
|:---|---:|---:|---:|---:|
| `redocly lint packages/core/src/benchmark/benches/rebilly.yaml` | 1.288 ± 0.032 | 1.247 | 1.336 | 1.00 |
| `redocly-next lint packages/core/src/benchmark/benches/rebilly.yaml` | 1.317 ± 0.030 | 1.278 | 1.360 | 1.02 ± 0.03 |

<!-- thollander/actions-comment-pull-request "hyperfine-comparison" -->
github-actions[bot]: <!-- jest coverage report action for options with hash 3339ae53a38c44382c0fb4491bdb282d -->

## Coverage report 



| <div title="Status of coverage:&#10; 🟢 - ok&#10; 🟡 - slightly more than threshold&#10; 🔴 - under the threshold">St.<sup>:grey_question:</sup></div> | Category   | Percentage | Covered / Total |
| :----------------------------------------------------------------------------------------------------------------------------------------------------: | :--------- | :--------- | :-------------: |
|                                                                           🟡                                                                           | Statements | 75.42%     |    3783/5016    |
|                                                                           🟡                                                                           | Branches   | 66.33%     |    2098/3163    |
|                                                                           🟡                                                                           | Functions  | 68.53%     |     636/928     |
|                                                                           🟡                                                                           | Lines      | 75.57%     |    3533/4675    |


## Test suite run success
623 tests passing in 91 suites.

<p align="right">Report generated by <a href="https://github.com/ArtiomTr/jest-coverage-report-action">🧪jest coverage report action</a> from 7c1fca016017f0830d1b03c0d40b15a2164f07ac</p>


Patch:
diff --git a/.changeset/loud-forks-speak.md b/.changeset/loud-forks-speak.md
new file mode 100644
index 00000000..c174ef1f
--- /dev/null
+++ b/.changeset/loud-forks-speak.md
@@ -0,0 +1,6 @@
+---
+'@redocly/openapi-core': patch
+'@redocly/cli': patch
+---
+
+Fixed the wrong location pointer when reporting on the 'no-path-trailing-slash' rule.
diff --git a/packages/core/src/rules/common/no-path-trailing-slash.ts b/packages/core/src/rules/common/no-path-trailing-slash.ts
index af1f569d..aa195e50 100644
--- a/packages/core/src/rules/common/no-path-trailing-slash.ts
+++ b/packages/core/src/rules/common/no-path-trailing-slash.ts
@@ -3,11 +3,11 @@ import { UserContext } from '../../walk';
 
 export const NoPathTrailingSlash: Oas3Rule | Oas2Rule = () => {
   return {
-    PathItem(_path: any, { report, key, location }: UserContext) {
+    PathItem(_path: any, { report, key, rawLocation }: UserContext) {
       if ((key as string).endsWith('/') && key !== '/') {
         report({
           message: `\`${key}\` should not have a trailing slash.`,
-          location: location.key(),
+          location: rawLocation.key(),
         });
       }
     },


Test patch:
diff --git a/packages/core/src/rules/common/__tests__/no-path-trailing-slash.test.ts b/packages/core/src/rules/common/__tests__/no-path-trailing-slash.test.ts
index 09d111d9..d56cb6c4 100644
--- a/packages/core/src/rules/common/__tests__/no-path-trailing-slash.test.ts
+++ b/packages/core/src/rules/common/__tests__/no-path-trailing-slash.test.ts
@@ -41,6 +41,47 @@ describe('no-path-trailing-slash', () => {
     `);
   });
 
+  it('should report on trailing slash in path on key when referencing', async () => {
+    const document = parseYamlToDocument(
+      outdent`
+        openapi: 3.0.0
+        paths:
+          '/bad/':
+            $ref: '#/components/pathItems/MyItem'
+        components:
+          pathItems:
+            MyItem:
+              get:
+                summary: List all pets
+        `,
+      'foobar.yaml'
+    );
+
+    const results = await lintDocument({
+      externalRefResolver: new BaseResolver(),
+      document,
+      config: await makeConfig({ 'no-path-trailing-slash': 'error' }),
+    });
+
+    expect(replaceSourceWithRef(results)).toMatchInlineSnapshot(`
+      Array [
+        Object {
+          "location": Array [
+            Object {
+              "pointer": "#/paths/~1bad~1",
+              "reportOnKey": true,
+              "source": "foobar.yaml",
+            },
+          ],
+          "message": "\`/bad/\` should not have a trailing slash.",
+          "ruleId": "no-path-trailing-slash",
+          "severity": "error",
+          "suggest": Array [],
+        },
+      ]
+    `);
+  });
+
   it('should not report on if no trailing slash in path', async () => {
     const document = parseYamlToDocument(
       outdent`