Skip to content

Latest commit

 

History

History
137 lines (99 loc) · 3.98 KB

ORDER_PLACEMENT_NULL_ID_FIX.md

File metadata and controls

137 lines (99 loc) · 3.98 KB

Fix for "The given id must not be null" Order Placement Error

Problem Description

Users were experiencing the error "Failed to place order: The given id must not be null" when attempting to place orders after adding items to the cart.

Root Cause

The issue was in frontend/src/context/CartContext.jsx where the vendorId was being incorrectly converted to a Number when loading from localStorage:

// BEFORE (Line 24) - INCORRECT
if (savedVendorId) {
  setVendorId(Number(savedVendorId));
}

Why This Caused the Problem

  1. Backend Expectation: The backend CreateOrderRequest DTO expects vendorId to be a UUID type
  2. UUID Format: UUIDs are strings like "550e8400-e29b-41d4-a716-446655440000", not numbers
  3. Number Conversion: Converting a UUID string to a Number results in NaN (Not a Number)
  4. JSON Serialization: When JavaScript serializes NaN to JSON, it becomes null
  5. Backend Validation: Spring Boot's findById(null) throws "The given id must not be null" error

The Flow of the Bug

UUID String: "550e8400-e29b-41d4-a716-446655440000"
       ↓ Number() conversion
NaN (Not a Number)
       ↓ JSON.stringify()
null
       ↓ Backend receives
vendorRepository.findById(null)
       ↓
IllegalArgumentException: "The given id must not be null"

Solution

Remove the Number() conversion and keep the vendorId as a string (UUID format):

// AFTER (Line 24) - CORRECT
if (savedVendorId) {
  setVendorId(savedVendorId); // Keep as string (UUID)
}

Files Modified

  • frontend/src/context/CartContext.jsx (1 line changed)

Impact

  • ✅ Orders can now be placed successfully
  • ✅ vendorId is correctly passed as UUID string to backend
  • ✅ No breaking changes to other functionality
  • ✅ Cart persistence still works correctly
  • ✅ Vendor comparison logic still works (comparing UUID strings)

Testing

Before Fix

  1. Add items to cart
  2. Refresh page (cart loads from localStorage)
  3. Proceed to checkout
  4. Place order
  5. Error: "Failed to place order: The given id must not be null"

After Fix

  1. Add items to cart
  2. Refresh page (cart loads from localStorage)
  3. Proceed to checkout
  4. Place order
  5. Success: Order placed successfully, redirected to success page

Technical Details

Backend UUID Handling

Spring Boot's Jackson JSON deserializer expects UUID fields to be sent as strings in JSON:

{
  "vendorId": "550e8400-e29b-41d4-a716-446655440000",
  "slotId": "660e8400-e29b-41d4-a716-446655440001",
  "items": [...]
}

Jackson automatically converts these strings to Java UUID objects.

Frontend UUID Handling

JavaScript doesn't have a native UUID type, so UUIDs are always strings. The fix ensures they remain strings throughout the application lifecycle:

  1. Received from API: UUID as string
  2. Stored in cart context: UUID as string
  3. Saved to localStorage: UUID as string
  4. Loaded from localStorage: UUID as string (FIXED)
  5. Sent to API: UUID as string

Compatibility

This fix is fully backward compatible:

  • Existing carts in localStorage will work after the fix
  • No database migrations required
  • No backend changes required
  • No API contract changes

Verification

To verify the fix is working:

# Build frontend
cd frontend
npm install
npm run build

# Check for build errors - should complete successfully

The frontend should build without errors, confirming the change is syntactically correct.

Related Files

No Changes Needed In:

  • backend/src/main/java/com/srmiggy/dto/CreateOrderRequest.java (already expects UUID)
  • backend/src/main/java/com/srmiggy/service/OrderService.java (already handles UUIDs correctly)
  • frontend/src/pages/Checkout.jsx (already sends UUIDs as strings)

Conclusion

This was a simple but critical bug where a type conversion was inadvertently corrupting UUID values. The fix is minimal (1 line changed) and surgical, addressing exactly the root cause without affecting other functionality.