Skip to content

Commit

Permalink
fix: prevent modification of the FindOptions.relations (#7887)
Browse files Browse the repository at this point in the history
because of how we modify it as part of a recursive function
we were erasing the `relations` array when iterating over them.
this fixes that so we don't mess with the inputs like that
and instead copy it
  • Loading branch information
imnotjames committed Jul 11, 2021
1 parent 1de2e13 commit a2fcad6
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/find-options/FindOptionsUtils.ts
Expand Up @@ -111,7 +111,8 @@ export class FindOptionsUtils {
}

if (options.relations) {
const allRelations = options.relations;
// Copy because `applyRelationsRecursively` modifies it
const allRelations = [...options.relations];
this.applyRelationsRecursively(qb, allRelations, qb.expressionMap.mainAlias!.name, qb.expressionMap.mainAlias!.metadata, "");
// recursive removes found relations from allRelations array
// if there are relations left in this array it means those relations were not found in the entity structure
Expand Down
12 changes: 12 additions & 0 deletions test/github-issues/7882/entity/Example.ts
@@ -0,0 +1,12 @@
import { Entity, JoinColumn, OneToOne, PrimaryGeneratedColumn } from "../../../../src";
import { ExampleText } from "./ExampleText";

@Entity()
export class Example {
@PrimaryGeneratedColumn()
id: string;

@OneToOne(() => ExampleText)
@JoinColumn()
exampleText: ExampleText;
}
7 changes: 7 additions & 0 deletions test/github-issues/7882/entity/ExampleText.ts
@@ -0,0 +1,7 @@
import { Entity, PrimaryGeneratedColumn } from "../../../../src";

@Entity()
export class ExampleText {
@PrimaryGeneratedColumn()
id: string;
}
31 changes: 31 additions & 0 deletions test/github-issues/7882/issue-7882.ts
@@ -0,0 +1,31 @@
import "reflect-metadata";
import { expect } from "chai";
import { Connection } from "../../../src";
import { closeTestingConnections, createTestingConnections, reloadTestingDatabases } from "../../utils/test-utils";
import { Example } from "./entity/Example";
import { ExampleText } from "./entity/ExampleText";

describe.only("github issues > #7882 .findOne reduces relations to an empty array", () => {

let connections: Connection[];
before(async () => {
connections = await createTestingConnections({
enabledDrivers: [ "sqlite" ],
entities: [ Example, ExampleText ],
schemaCreate: false,
dropSchema: true
});
});
beforeEach(() => reloadTestingDatabases(connections));
after(() => closeTestingConnections(connections));

it("should delete all documents related to search pattern", () => Promise.all(connections.map(async connection => {
const relations = [ "exampleText" ];

const repo = connection.getRepository(Example);

await repo.find({ relations });

expect(relations).to.be.eql([ "exampleText" ]);
})));
});

0 comments on commit a2fcad6

Please sign in to comment.