fix(framework): exclude nested fields when excluding requested field (#9979)
This commit is contained in:
1 parent
af9eec73df
commit
6496789c65
3 files changed
+31
-4
No files matched your search
@@ -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 { IStoreModuleService } from "@medusajs/types"
|
||||||
import { ApiKeyType, Modules, ProductStatus } from "@medusajs/utils"
|
import { ApiKeyType, Modules, ProductStatus } from "@medusajs/utils"
|
||||||
import { medusaIntegrationTestRunner } from "@medusajs/test-utils"
|
import qs from "qs"
|
||||||
import {
|
import {
|
||||||
adminHeaders,
|
adminHeaders,
|
||||||
createAdminUser,
|
createAdminUser,
|
||||||
@@ -8,7 +9,6 @@ import {
|
|||||||
generateStoreHeaders,
|
generateStoreHeaders,
|
||||||
} from "../../../../helpers/create-admin-user"
|
} from "../../../../helpers/create-admin-user"
|
||||||
import { getProductFixture } from "../../../../helpers/fixtures"
|
import { getProductFixture } from "../../../../helpers/fixtures"
|
||||||
import qs from "qs"
|
|
||||||
|
|
||||||
jest.setTimeout(30000)
|
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 () => {
|
it("should list all products for a sales channel", async () => {
|
||||||
const salesChannel = await createSalesChannel(
|
const salesChannel = await createSalesChannel(
|
||||||
{ name: "sales channel test" },
|
{ name: "sales channel test" },
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
import { pick } from "lodash"
|
|
||||||
import { FindConfig, QueryConfig, RequestQueryFields } from "@medusajs/types"
|
import { FindConfig, QueryConfig, RequestQueryFields } from "@medusajs/types"
|
||||||
import {
|
import {
|
||||||
isDefined,
|
isDefined,
|
||||||
@@ -6,6 +5,7 @@ import {
|
|||||||
MedusaError,
|
MedusaError,
|
||||||
stringToSelectRelationObject,
|
stringToSelectRelationObject,
|
||||||
} from "@medusajs/utils"
|
} from "@medusajs/utils"
|
||||||
|
import { pick } from "lodash"
|
||||||
|
|
||||||
export function pickByConfig<TModel>(
|
export function pickByConfig<TModel>(
|
||||||
obj: TModel | TModel[],
|
obj: TModel | TModel[],
|
||||||
@@ -130,7 +130,16 @@ export function prepareListQuery<T extends RequestQueryFields, TEntity>(
|
|||||||
if (field.startsWith("+") || field.startsWith(" ")) {
|
if (field.startsWith("+") || field.startsWith(" ")) {
|
||||||
allFields.add(field.trim().replace(/^\+/, ""))
|
allFields.add(field.trim().replace(/^\+/, ""))
|
||||||
} else if (field.startsWith("-")) {
|
} 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 {
|
} else {
|
||||||
allFields.add(field)
|
allFields.add(field)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user