fix(framework): exclude nested fields when excluding requested field (#9979)

This commit is contained in:
Carlos R. L. Rodrigues
2024-11-07 18:26:02 -03:00
committed by GitHub
parent af9eec73df
commit 6496789c65
3 changed files with 31 additions and 4 deletions

View File

@@ -0,0 +1,5 @@
---
"@medusajs/framework": patch
---
Exclude nested fields when excluding field from endpoint

View File

@@ -1,6 +1,7 @@
import { medusaIntegrationTestRunner } from "@medusajs/test-utils"
import { IStoreModuleService } from "@medusajs/types"
import { ApiKeyType, Modules, ProductStatus } from "@medusajs/utils"
import { medusaIntegrationTestRunner } from "@medusajs/test-utils"
import qs from "qs"
import {
adminHeaders,
createAdminUser,
@@ -8,7 +9,6 @@ import {
generateStoreHeaders,
} from "../../../../helpers/create-admin-user"
import { getProductFixture } from "../../../../helpers/fixtures"
import qs from "qs"
jest.setTimeout(30000)
@@ -589,6 +589,19 @@ medusaIntegrationTestRunner({
])
})
it("should list all products excluding variants", async () => {
let response = await api.get(
`/admin/products?fields=-variants`,
adminHeaders
)
expect(response.data.count).toEqual(4)
for (let product of response.data.products) {
expect(product.variants).toBeUndefined()
}
})
it("should list all products for a sales channel", async () => {
const salesChannel = await createSalesChannel(
{ name: "sales channel test" },

View File

@@ -1,4 +1,3 @@
import { pick } from "lodash"
import { FindConfig, QueryConfig, RequestQueryFields } from "@medusajs/types"
import {
isDefined,
@@ -6,6 +5,7 @@ import {
MedusaError,
stringToSelectRelationObject,
} from "@medusajs/utils"
import { pick } from "lodash"
export function pickByConfig<TModel>(
obj: TModel | TModel[],
@@ -130,7 +130,16 @@ export function prepareListQuery<T extends RequestQueryFields, TEntity>(
if (field.startsWith("+") || field.startsWith(" ")) {
allFields.add(field.trim().replace(/^\+/, ""))
} else if (field.startsWith("-")) {
allFields.delete(field.replace(/^-/, ""))
const fieldName = field.replace(/^-/, "")
for (const reqField of allFields) {
const reqFieldName = reqField.replace(/^\*/, "")
if (
reqFieldName === fieldName ||
reqFieldName.startsWith(fieldName + ".")
) {
allFields.delete(reqField)
}
}
} else {
allFields.add(field)
}