Skip to content

Commit

Permalink
fix(framework): exclude nested fields when excluding requested field (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
carlos-r-l-rodrigues authored Nov 7, 2024
1 parent af9eec7 commit 6496789
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/gentle-baboons-camp.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@medusajs/framework": patch
---

Exclude nested fields when excluding field from endpoint
17 changes: 15 additions & 2 deletions integration-tests/http/__tests__/product/store/product.spec.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
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,
generatePublishableKey,
generateStoreHeaders,
} from "../../../../helpers/create-admin-user"
import { getProductFixture } from "../../../../helpers/fixtures"
import qs from "qs"

jest.setTimeout(30000)

Expand Down Expand Up @@ -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" },
Expand Down
13 changes: 11 additions & 2 deletions packages/core/framework/src/http/utils/get-query-config.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { pick } from "lodash"
import { FindConfig, QueryConfig, RequestQueryFields } from "@medusajs/types"
import {
isDefined,
isPresent,
MedusaError,
stringToSelectRelationObject,
} from "@medusajs/utils"
import { pick } from "lodash"

export function pickByConfig<TModel>(
obj: TModel | TModel[],
Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit 6496789

Please sign in to comment.