Hacktoberfest 2026: the issues maintainers tagged for October, open and beginner-friendly. Browse Hacktoberfest issues

[chapter4end] Issue: PUT endpoint fails when JSON body doesn't include id field

Open
#5 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
1/5
Estimated time
Under an hour
Newbie friendliness
55/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Stale
Tech stack
java, spring-boot
Domain
api, backend, database

Research direction

Start at the PUT /coffees/{id} endpoint and reproduce the request with the id in the URL but not the JSON body. Verify that the path id is assigned to the Coffee object before coffeeRepository.save(coffee), and confirm the request creates or updates successfully instead of returning the JPA exception.

Written by the indexing model from the issue text.

Description

Problem Description

The PUT /coffees/{id} endpoint throws a JpaSystemException when the request body doesn't include the id field, even though the id is provided in the URL path.

Error Message
org.hibernate.id.IdentifierGenerationException: Identifier of entity 'com.thehecklers.sbur_rest_demo.Coffee' must be manually assigned before calling 'persist()'
Root Cause

The issue occurs because:

  1. The @PathVariable String id parameter extracts the ID from the URL path (e.g., /coffees/1111id = "1111").
  2. The @RequestBody Coffee coffee object is created by Jackson from the JSON body only.
  3. These are two separate variables/objects — the URL path parameter and the request body object are independent.
  4. When the JSON body doesn't include an id field (e.g., {"name": "byecoffee"}), Jackson creates a Coffee object using the default constructor, leaving the id field as null.
  5. Hibernate/JPA requires the id field to be set before calling save(), causing the exception.
Steps to Reproduce
  1. Send a PUT request to /coffees/1111 with the following JSON body:
    {
      "name": "byecoffee"
    }
    
  2. The request fails with a 500 Internal Server Error.
Expected Behavior

The endpoint should use the id from the URL path to update or create the entity, regardless of whether the JSON body includes the id field.

Suggested Fix

Add coffee.setId(id); before calling coffeeRepository.save(coffee) to ensure the entity has the correct ID from the URL path:

@PutMapping("/{id}")
ResponseEntity<Coffee> putCoffee(@PathVariable String id,
                                 @RequestBody Coffee coffee) {
    coffee.setId(id);  // Add this line
    return (!coffeeRepository.existsById(id))
            ? new ResponseEntity<>(coffeeRepository.save(coffee),
            HttpStatus.CREATED)
            : new ResponseEntity<>(coffeeRepository.save(coffee),
            HttpStatus.OK);
}
Additional Context
  • Spring Boot version: 3.5.8
  • Using JPA with Hibernate
  • The Coffee entity uses @Id annotation without auto-generation, requiring manual ID assignment
Dominant language
No language data
Stars
119
Forks
71
PR merge metrics
No merged PRs in 30d

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Similar issues

More Backend & API Design issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.