Repository: Redocly/openapi-cli
Pull request #273 (2021-04-02 11:10:02)

fix:`--dereferenced` fix
`--dereferenced` was not working correctly, it did not dereference local refs.

I added a test to prevent this bug in the future.


Patch:
diff --git a/packages/core/src/bundle.ts b/packages/core/src/bundle.ts
index 66eb0dc4..64ac8f56 100755
--- a/packages/core/src/bundle.ts
+++ b/packages/core/src/bundle.ts
@@ -163,7 +163,8 @@ function makeBundleVisitor(version: OasMajorVersion, dereference: boolean, rootD
         if (
           resolved.location.source === rootDocument.source &&
           resolved.location.source === ctx.location.source &&
-          ctx.type.name !== 'scalar'
+          ctx.type.name !== 'scalar' &&
+          !dereference
         ) {
           return;
         }


Test patch:
diff --git a/packages/core/__tests__/__snapshots__/bundle.test.ts.snap b/packages/core/__tests__/__snapshots__/bundle.test.ts.snap
index d7d9c2e1..b72501be 100644
--- a/packages/core/__tests__/__snapshots__/bundle.test.ts.snap
+++ b/packages/core/__tests__/__snapshots__/bundle.test.ts.snap
@@ -79,3 +79,23 @@ components:
       name: param_b
 
 `;
+
+exports[`bundle should dereferenced correctly when used with dereference 1`] = `
+openapi: 3.0.0
+paths:
+  /pet:
+    get:
+      operationId: get
+      parameters:
+        - name: shared-a
+        - name: get_b
+    post:
+      operationId: post
+      parameters:
+        - name: shared-a
+components:
+  parameters:
+    shared_a:
+      name: shared-a
+
+`;
diff --git a/packages/core/__tests__/bundle.test.ts b/packages/core/__tests__/bundle.test.ts
index 0ea0bbc5..ea03e85f 100644
--- a/packages/core/__tests__/bundle.test.ts
+++ b/packages/core/__tests__/bundle.test.ts
@@ -10,39 +10,36 @@ import { BaseResolver } from '../src/resolve';
 describe('bundle', () => {
   expect.addSnapshotSerializer(yamlSerializer);
 
-  it('change nothing with only internal refs', async () => {
-    const document = parseYamlToDocument(
-      outdent`
-        openapi: 3.0.0
-        info:
-          contact: {}
-          license: {}
-        paths:
-          /pet:
-            get:
-              operationId: get
-              parameters:
-                - $ref: '#/components/parameters/shared_a'
-                - name: get_b
-            post:
-              operationId: post
-              parameters:
-                - $ref: '#/components/parameters/shared_a'
-        components:
-          parameters:
-            shared_a:
-              name: shared-a
-      `,
-      '',
-    );
+  const testDocument =  parseYamlToDocument(
+    outdent`
+      openapi: 3.0.0
+      paths:
+        /pet:
+          get:
+            operationId: get
+            parameters:
+              - $ref: '#/components/parameters/shared_a'
+              - name: get_b
+          post:
+            operationId: post
+            parameters:
+              - $ref: '#/components/parameters/shared_a'
+      components:
+        parameters:
+          shared_a:
+            name: shared-a
+    `,
+    '',
+  );
 
+  it('change nothing with only internal refs', async () => {
     const { bundle, problems } = await bundleDocument({
-      document,
+      document: testDocument,
       externalRefResolver: new BaseResolver(),
       config: new LintConfig({}),
     });
 
-    const origCopy = JSON.parse(JSON.stringify(document.parsed));
+    const origCopy = JSON.parse(JSON.stringify(testDocument.parsed));
 
     expect(problems).toHaveLength(0);
     expect(bundle.parsed).toEqual(origCopy);
@@ -68,4 +65,16 @@ describe('bundle', () => {
     );
     expect(res.parsed).toMatchSnapshot();
   });
+
+  it('should dereferenced correctly when used with dereference', async () => {
+    const { bundle: res, problems } = await bundleDocument({
+      externalRefResolver: new BaseResolver(),
+      config: new LintConfig({}),
+      document: testDocument,
+      dereference: true,
+    });
+
+    expect(problems).toHaveLength(0);
+    expect(res.parsed).toMatchSnapshot();
+  });
 });