medusajs / medusa

The world's most flexible commerce platform.
https://medusajs.com
MIT License
25.95k stars 2.61k forks source link

[Bug]: Update Variant and Get Product API routes return variants in different orders #10032

Closed Betanoir closed 4 days ago

Betanoir commented 5 days ago

Package.json file

{
  "name": "medusa-starter-default",
  "version": "0.0.1",
  "description": "A starter for Medusa projects.",
  "author": "Medusa (https://medusajs.com)",
  "license": "MIT",
  "keywords": [
    "sqlite",
    "postgres",
    "typescript",
    "ecommerce",
    "headless",
    "medusa"
  ],
  "scripts": {
    "build": "medusa build",
    "seed": "medusa exec ./src/scripts/seed.ts",
    "start": "medusa start",
    "dev": "medusa develop",
    "test:integration:http": "TEST_TYPE=integration:http NODE_OPTIONS=--experimental-vm-modules jest --silent=false --runInBand --forceExit",
    "test:integration:modules": "TEST_TYPE=integration:modules NODE_OPTIONS=--experimental-vm-modules jest --silent --runInBand --forceExit",
    "test:unit": "TEST_TYPE=unit NODE_OPTIONS=--experimental-vm-modules jest --silent --runInBand --forceExit"
  },
  "dependencies": {
    "@medusajs/admin-sdk": "^2.0.3",
    "@medusajs/cli": "^2.0.3",
    "@medusajs/framework": "^2.0.3",
    "@medusajs/medusa": "^2.0.3",
    "@mikro-orm/core": "5.9.7",
    "@mikro-orm/knex": "5.9.7",
    "@mikro-orm/migrations": "5.9.7",
    "@mikro-orm/postgresql": "5.9.7",
    "awilix": "^8.0.1",
    "pg": "^8.13.0",
    "zod": "3.22.4"
  },
  "devDependencies": {
    "@medusajs/test-utils": "^2.0.3",
    "@mikro-orm/cli": "5.9.7",
    "@swc/core": "1.5.7",
    "@swc/jest": "^0.2.36",
    "@types/jest": "^29.5.13",
    "@types/node": "^20.0.0",
    "@types/react": "^18.3.2",
    "@types/react-dom": "^18.2.25",
    "jest": "^29.7.0",
    "prop-types": "^15.8.1",
    "react": "^18.2.0",
    "react-dom": "^18.2.0",
    "ts-node": "^10.9.2",
    "typescript": "^5.6.2",
    "vite": "^5.2.11"
  },
  "engines": {
    "node": ">=20"
  }
}

Node.js version

v21.7.3

Database and its version

PostgreSQL 13

Operating system name and version

Windows 11

Browser name

Chrome

What happended?

API route for update variant returns a different order of variants than what is returned from the get product route.

Expected behavior

When fetching from the get product route, the variants attribute should contain the same order as the returned product when you request the update variant route under the same product.id, considering it comes from the database.

Actual behavior

This is what is returned when loading the product page: image

Below is the variants attribute of the product that is returned when I update the variant using the SDK. This same set and order of variants is returned when I make a request to the get product route after updating the variant (separate to the returned product) image

The titles are in a different order

Link to reproduction repo

N/A

fPolic commented 4 days ago

hey @Betanoir, ordering by default is not guaranteed, you can use order query param if you want to rely on consistent ordering. E.g. /products/<product_id>/variants?limit=10&offset=0&order=created_at

Betanoir commented 4 days ago

@fPolic Okay, thank you Just one thing that was strange was that this changed after the release of v2.0.3 yesterday so that's why I created an issue. I guess it's better practice to use the order by though, thanks 🙃