Skip to content

Commit

Permalink
fix: empty objects being hydrated when eager loading relations that h…
Browse files Browse the repository at this point in the history
…ave a `@VirtualColumn` (#10432)

* test: add scenario for #10431

* fix: empty objects being hydrated by unselected virtual properties
  • Loading branch information
tolgap committed Dec 29, 2023
1 parent 2dc9624 commit b53e410
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/query-builder/SelectQueryBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2882,6 +2882,11 @@ export class SelectQueryBuilder<Entity extends ObjectLiteral>
})
})
} else {
if (column.isVirtualProperty) {
// Do not add unselected virtual properties to final select
return
}

finalSelects.push({
selection: selectionPath,
aliasName: DriverUtils.buildAlias(
Expand Down
26 changes: 26 additions & 0 deletions test/github-issues/10431/entity/Category.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import {
Column,
Entity,
ManyToMany,
PrimaryGeneratedColumn,
VirtualColumn,
} from "../../../../src"
import { Product } from "./Product"

@Entity()
export class Category {
@PrimaryGeneratedColumn()
id: number

@VirtualColumn({
query: (alias) =>
`SELECT COUNT(*) FROM category WHERE id = ${alias}.id`,
})
randomVirtualColumn: number

@ManyToMany(() => Product, (product: Product) => product.categories)
products?: Product[]

@Column("varchar")
name: string
}
25 changes: 25 additions & 0 deletions test/github-issues/10431/entity/Product.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import {
Column,
Entity,
JoinTable,
ManyToMany,
PrimaryGeneratedColumn,
} from "../../../../src"
import { Category } from "./Category"

@Entity()
export class Product {
@PrimaryGeneratedColumn()
id: number

@Column("varchar")
name: string

@ManyToMany(() => Category, (category: Category) => category.products, {
eager: true,
cascade: ["insert", "update", "remove"],
orphanedRowAction: "delete",
})
@JoinTable()
categories: Category[]
}
2 changes: 2 additions & 0 deletions test/github-issues/10431/entity/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./Category"
export * from "./Product"
42 changes: 42 additions & 0 deletions test/github-issues/10431/issue-10431.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import "reflect-metadata"
import {
createTestingConnections,
closeTestingConnections,
reloadTestingDatabases,
} from "../../utils/test-utils"
import { DataSource } from "../../../src"
import { expect } from "chai"

import { Category, Product } from "./entity"

describe("github issues > #10431 When requesting nested relations on foreign key primary entities, relation becomes empty entity rather than null", () => {
let connections: DataSource[]
before(
async () =>
(connections = await createTestingConnections({
entities: [Category, Product],
schemaCreate: true,
dropSchema: true,
})),
)
beforeEach(() => reloadTestingDatabases(connections))
after(() => closeTestingConnections(connections))

it("should return [] when requested nested relations are empty on ManyToMany relation with @VirtualColumn definitions", () =>
Promise.all(
connections.map(async (connection) => {
const productRepo = connection.getRepository(Product)
const testProduct = new Product()
testProduct.name = "foo"
await productRepo.save(testProduct)
const foundProduct = await productRepo.findOne({
where: {
id: testProduct.id,
},
relations: { categories: true },
})
expect(foundProduct?.name).eq("foo")
expect(foundProduct?.categories).eql([])
}),
))
})

0 comments on commit b53e410

Please sign in to comment.