fix(framework): exclude nested fields when excluding requested field (#9979)
This commit is contained in:
committed by
GitHub
parent
af9eec73df
commit
6496789c65
5
.changeset/gentle-baboons-camp.md
Normal file
5
.changeset/gentle-baboons-camp.md
Normal file
@@ -0,0 +1,5 @@
|
||||
---
|
||||
"@medusajs/framework": patch
|
||||
---
|
||||
|
||||
Exclude nested fields when excluding field from endpoint
|
||||
@@ -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" },
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user