Skip to content

Commit

Permalink
fix: wrong discriminator mapping when bundling with derefernce (#1666)
Browse files Browse the repository at this point in the history
* fix: wrong discriminator mapping when bundling with derefernce

* add changeset
  • Loading branch information
tatomyr authored Oct 18, 2024
1 parent 6522bc0 commit 5a493e7
Show file tree
Hide file tree
Showing 8 changed files with 144 additions and 8 deletions.
6 changes: 6 additions & 0 deletions .changeset/proud-chairs-lie.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@redocly/openapi-core": patch
"@redocly/cli": patch
---

Fixed bundling with the `--dereferenced` option. Previously, references to external files were not substituted with references to components, causing them to become invalid.
6 changes: 6 additions & 0 deletions __tests__/bundle/discriminator-mapping/bar.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
type: object
properties:
discriminatedProp:
type: string
bar:
type: boolean
6 changes: 6 additions & 0 deletions __tests__/bundle/discriminator-mapping/foo.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
type: object
properties:
discriminatedProp:
type: string
foo:
type: string
19 changes: 19 additions & 0 deletions __tests__/bundle/discriminator-mapping/main.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
openapi: 3.1.0
info: {}
paths:
/test:
get:
responses:
default:
content:
application/json:
schema:
type: object
discriminator:
propertyName: discriminatedProp
mapping:
Foo: ./foo.yaml
Bar: ./bar.yaml
oneOf:
- $ref: ./foo.yaml
- $ref: ./bar.yaml
3 changes: 3 additions & 0 deletions __tests__/bundle/discriminator-mapping/redocly.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
apis:
main:
root: ./main.yaml
87 changes: 87 additions & 0 deletions __tests__/bundle/discriminator-mapping/snapshot.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`E2E bundle discriminator-mapping 1`] = `
openapi: 3.1.0
info: {}
paths:
/test:
get:
responses:
default:
content:
application/json:
schema:
type: object
discriminator:
propertyName: discriminatedProp
mapping:
Foo: '#/components/schemas/foo'
Bar: '#/components/schemas/bar'
oneOf:
- $ref: '#/components/schemas/foo'
- $ref: '#/components/schemas/bar'
components:
schemas:
foo:
type: object
properties:
discriminatedProp:
type: string
foo:
type: string
bar:
type: object
properties:
discriminatedProp:
type: string
bar:
type: boolean
bundling ./main.yaml...
📦 Created a bundle for ./main.yaml at stdout <test>ms.
`;

exports[`E2E bundle with option: dereferenced discriminator mapping should be replaced with correct references to components 1`] = `
openapi: 3.1.0
info: {}
paths:
/test:
get:
responses:
default:
content:
application/json:
schema:
type: object
discriminator:
propertyName: discriminatedProp
mapping:
Foo: '#/components/schemas/foo'
Bar: '#/components/schemas/bar'
oneOf:
- type: object
properties: &ref_0
discriminatedProp:
type: string
foo:
type: string
- type: object
properties: &ref_1
discriminatedProp:
type: string
bar:
type: boolean
components:
schemas:
foo:
type: object
properties: *ref_0
bar:
type: object
properties: *ref_1
bundling main.yaml...
📦 Created a bundle for main.yaml at stdout <test>ms.
`;
11 changes: 11 additions & 0 deletions __tests__/commands.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,17 @@ describe('E2E', () => {
const result = getCommandOutput(args, folderPath);
(<any>expect(cleanupOutput(result))).toMatchSpecificSnapshot(join(folderPath, 'snapshot.js'));
});

it('discriminator mapping should be replaced with correct references to components', () => {
const folderPath = join(__dirname, `bundle/discriminator-mapping`);
const args = getParams('../../../packages/cli/src/index.ts', 'bundle', [
'main.yaml',
'--dereferenced',
]);

const result = getCommandOutput(args, folderPath);
(<any>expect(cleanupOutput(result))).toMatchSpecificSnapshot(join(folderPath, 'snapshot.js'));
});
});

describe('bundle with long description', () => {
Expand Down
14 changes: 6 additions & 8 deletions packages/core/src/bundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -427,11 +427,7 @@ function makeBundleVisitor(
}

const componentType = mapTypeToComponent('Schema', version)!;
if (dereference) {
saveComponent(componentType, resolved, ctx);
} else {
mapping[name] = saveComponent(componentType, resolved, ctx);
}
mapping[name] = saveComponent(componentType, resolved, ctx);
}
},
};
Expand All @@ -456,9 +452,11 @@ function makeBundleVisitor(
components[componentType] = components[componentType] || {};
const name = getComponentName(target, componentType, ctx);
components[componentType][name] = target.node;
if (version === SpecMajorVersion.OAS3) {
return `#/components/${componentType}/${name}`;
} else if (version === SpecMajorVersion.Async2 || version === SpecMajorVersion.Async3) {
if (
version === SpecMajorVersion.OAS3 ||
version === SpecMajorVersion.Async2 ||
version === SpecMajorVersion.Async3
) {
return `#/components/${componentType}/${name}`;
} else {
return `#/${componentType}/${name}`;
Expand Down

1 comment on commit 5a493e7

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage report

St.
Category Percentage Covered / Total
🟡 Statements 78.57% 4991/6352
🟡 Branches 67.24% 2061/3065
🟡 Functions 72.98% 824/1129
🟡 Lines 78.86% 4709/5971

Test suite run success

809 tests passing in 121 suites.

Report generated by 🧪jest coverage report action from 5a493e7

Please sign in to comment.