Skip to content

Commit 72ccf7d

Browse files
maxwellpetersonJLekawaDaryna-del
authored
fix(join): correctly prefix discriminator mapping refs (#2642)
* fix(join): correctly prefix discriminator mapping refs * Update .changeset/fuzzy-drinks-warn.md --------- Co-authored-by: Jacek Łękawa <164185257+JLekawa@users.noreply.github.com> Co-authored-by: Daryna Pastushenko <77724851+Daryna-del@users.noreply.github.com>
1 parent cf38af1 commit 72ccf7d

File tree

3 files changed

+47
-1
lines changed

3 files changed

+47
-1
lines changed

.changeset/fuzzy-drinks-warn.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@redocly/cli': patch
3+
---
4+
5+
Fixed an issue where `join --prefix-components-with-info-prop` would incorrectly rewrite discriminator mapping refs.
6+
This issue occurred when schema names contained the same substring as the prefix.

packages/cli/src/__tests__/commands/join.test.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
} from '@redocly/openapi-core';
99
import { yellow } from 'colorette';
1010

11+
import { replace$Refs } from '../../commands/join/helpers/replace-$-refs.js';
1112
import { handleJoin } from '../../commands/join/index.js';
1213
import { exitWithError } from '../../utils/error.js';
1314
import {
@@ -464,4 +465,43 @@ describe('handleJoin', () => {
464465
expect(joinedDef.paths['/bar'].servers).toEqual([{ url: 'https://foo.com/api/v1/second' }]);
465466
});
466467
});
468+
469+
describe('replace$Refs', () => {
470+
it('should prefix discriminator mapping refs when schema name contains prefix substring', () => {
471+
const doc = {
472+
components: {
473+
schemas: {
474+
CreateSomethingRequest: {
475+
type: 'object',
476+
properties: {
477+
kind: { type: 'string' },
478+
},
479+
},
480+
Wrapper: {
481+
discriminator: {
482+
propertyName: 'kind',
483+
mapping: {
484+
create: '#/components/schemas/CreateSomethingRequest',
485+
alreadyPrefixed: '#/components/schemas/Something_CreateSomethingRequest',
486+
},
487+
},
488+
oneOf: [
489+
{ $ref: '#/components/schemas/CreateSomethingRequest' },
490+
{ $ref: '#/components/schemas/Something_CreateSomethingRequest' },
491+
],
492+
},
493+
},
494+
},
495+
};
496+
497+
replace$Refs(doc, 'Something');
498+
499+
expect(doc.components.schemas.Wrapper.discriminator.mapping.create).toBe(
500+
'#/components/schemas/Something_CreateSomethingRequest'
501+
);
502+
expect(doc.components.schemas.Wrapper.discriminator.mapping.alreadyPrefixed).toBe(
503+
'#/components/schemas/Something_CreateSomethingRequest'
504+
);
505+
});
506+
});
467507
});

packages/cli/src/commands/join/helpers/replace-$-refs.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export function replace$Refs(obj: unknown, componentsPrefix: string) {
1616
mapping[name] = mappingPointer
1717
.split('/')
1818
.map((name, i, arr) => {
19-
return arr.length - 1 === i && !name.includes(componentsPrefix)
19+
return arr.length - 1 === i && !name.startsWith(componentsPrefix + '_')
2020
? componentsPrefix + '_' + name
2121
: name;
2222
})

0 commit comments

Comments
 (0)