Skip to content

Commit

Permalink
Unit testing - support for inline reference overrides
Browse files Browse the repository at this point in the history
  • Loading branch information
cesarParra committed Jan 24, 2023
1 parent b26e786 commit 1dc35d1
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/transpiler/openapi/parsers/ReferenceBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ type TypeBundleWithIsCollectionAndReferenceOverrides = TypeBundle & {
export class ReferenceBuilder {
build(referencedTypeName: string): Reference {
const originalTypeName = referencedTypeName;

// Checking for inline overrides of the type: [memberName:ClassOverrideName]
const regexForSchemaOverrides = /\[(.*?)]/g;
const schemaOverrides = referencedTypeName.match(regexForSchemaOverrides);
let referenceOverrides: ReferenceOverride[] = [];
Expand Down Expand Up @@ -223,6 +225,8 @@ export class ReferenceBuilder {
// For Maps, we treat them as objects but do not try to define their shape, because their keys can vary
// at runtime.
return { schema: { type: 'object' }, referenceComponents: [] };
case 'object':
return { schema: { type: 'object' }, referenceComponents: [] };
default:
// If we got here we are dealing with a non-primitive (most likely a custom class or an SObject).
const referencedType = TypesRepository.getInstance().getFromAllByName(typeName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,7 @@ describe('ReferenceBuilder', () => {
});

describe('References can override their type', () => {
it("should correctly parse a manually defined schema in the reference's ApexDoc", function () {
it('should correctly parse a reference with an overridden reference', function () {
const classMirror = new ClassMirrorBuilder()
.addFiled(
new FieldMirrorBuilder()
Expand Down Expand Up @@ -703,5 +703,49 @@ describe('ReferenceBuilder', () => {
expect(result.referenceComponents.some((ref) => ref.referencedClass === 'child')).toBe(true);
expect(result.referenceComponents.some((ref) => ref.referencedClass === 'grandchild')).toBe(true);
});

it('should correctly parse a referenced property overridden inline', function () {
const mainClassMirror = new ClassMirrorBuilder()
.withName('parent')
.addFiled(new FieldMirrorBuilder().withName('childClassMember').withType('Object').build())
.build();

const childClass = new ClassMirrorBuilder()
.withName('child')
.addFiled(
new FieldMirrorBuilder()
.withName('grandChildClassMember')
.withReferencedType({
type: 'GrandChildClass',
rawDeclaration: 'GrandChildClass',
})
.build(),
)
.build();

const grandChildClass = new ClassMirrorBuilder()
.withName('grandchild')
.addFiled(new FieldMirrorBuilder().withName('stringMember').withType('String').build())
.build();

TypesRepository.getInstance = jest.fn().mockReturnValue({
getFromAllByName: jest
.fn()
.mockReturnValueOnce({ type: mainClassMirror, isChild: false })
.mockReturnValueOnce({ type: childClass, isChild: false })
.mockReturnValueOnce({ type: grandChildClass, isChild: false }),
});

const result = new ReferenceBuilder().build('className[childClassMember:ChildClass]');

expect(result.referenceComponents).toHaveLength(3);
expect(
result.referenceComponents.some(
(ref) => ref.referencedClass === 'parent_className[childClassMember:ChildClass]',
),
).toBe(true);
expect(result.referenceComponents.some((ref) => ref.referencedClass === 'child')).toBe(true);
expect(result.referenceComponents.some((ref) => ref.referencedClass === 'grandchild')).toBe(true);
});
});
});

0 comments on commit 1dc35d1

Please sign in to comment.