[chapter4end] Issue: PUT endpoint fails when JSON body doesn't include id field
まだ誰も着手していません。
評価
- 難易度
- 1/5
- 見積もり時間
- 1時間未満
- 初心者へのやさしさ
- 55/100
調査の方向性
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.
索引モデルが issue の本文から書いたものです。
説明
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:
- The
@PathVariable String idparameter extracts the ID from the URL path (e.g.,/coffees/1111→id = "1111"). - The
@RequestBody Coffee coffeeobject is created by Jackson from the JSON body only. - These are two separate variables/objects — the URL path parameter and the request body object are independent.
- When the JSON body doesn't include an
idfield (e.g.,{"name": "byecoffee"}), Jackson creates aCoffeeobject using the default constructor, leaving theidfield asnull. - Hibernate/JPA requires the
idfield to be set before callingsave(), causing the exception.
Steps to Reproduce
- Send a PUT request to
/coffees/1111with the following JSON body:{ "name": "byecoffee" } - 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
Coffeeentity uses@Idannotation without auto-generation, requiring manual ID assignment
- 主要言語
- 言語のデータがありません
- スター
- 119
- フォーク
- 71
- PR マージ指標
- 30日以内にマージされた PR はありません
コントリビューションガイド
このリポジトリのコントリビューションガイドは索引されていません
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
似ている issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
stratum-mining/stratum#2404 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 86/100
Diaoul/subliminal#1382 ·
-
fix(errors): EHOSTUNREACH from a happy-eyeballs connect is reported as a resolver error (STAMP-80) オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 90/100
snapshot-labs/stamp#666 ·
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
GauravKarakoti/SecureFlow#1070 · コメント 1 件 ·
-
triage/confirmed
難易度 2/5 1〜3時間 初心者へのやさしさ 88/100
agentscope-ai/agentscope#2775 ·