Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: wrong discriminator mapping when bundling with derefernce #1666

Merged
merged 2 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading