{"record":{"id":"120f123573be009a","repo":"quarkusio/quarkus","slug":"id-was-invalidly-set-on-request-120f12","errorCode":null,"errorMessage":"Id was invalidly set on request.","messagePattern":"Id was invalidly set on request\\.","errorType":"http","errorClass":"WebApplicationException","httpStatus":422,"severity":"error","filePath":"integration-tests/hibernate-search-orm-elasticsearch-tenancy/src/main/java/io/quarkus/it/hibernate/search/orm/elasticsearch/multitenancy/fruit/FruitResource.java","lineNumber":62,"sourceCode":"    }\n\n    @GET\n    @Path(\"/{id}\")\n    @Transactional\n    public Fruit findById(int id) {\n        Fruit entity = entityManager.find(Fruit.class, id);\n        if (entity == null) {\n            throw new WebApplicationException(\"Fruit with id of \" + id + \" does not exist.\", 404);\n        }\n        return entity;\n    }\n\n    @POST\n    @Path(\"/\")\n    @Transactional\n    public Response create(@NotNull Fruit fruit) {\n        if (fruit.getId() != null) {\n            throw new WebApplicationException(\"Id was invalidly set on request.\", 422);\n        }\n        LOG.debugv(\"Create {0}\", fruit.getName());\n        entityManager.persist(fruit);\n        return Response.ok(fruit).status(Response.Status.CREATED).build();\n    }\n\n    @PUT\n    @Path(\"/{id}\")\n    @Transactional\n    public Fruit update(@NotNull @PathParam(\"id\") int id, @NotNull Fruit fruit) {\n        if (fruit.getName() == null) {\n            throw new WebApplicationException(\"Fruit Name was not set on request.\", 422);\n        }\n\n        Fruit entity = entityManager.find(Fruit.class, id);\n        if (entity == null) {\n            throw new WebApplicationException(\"Fruit with id of \" + id + \" does not exist.\", 404);\n        }","sourceCodeStart":44,"sourceCodeEnd":80,"githubUrl":"https://github.com/quarkusio/quarkus/blob/e1c734241f34c7919086ceb4c9262b4a58f6de44/integration-tests/hibernate-search-orm-elasticsearch-tenancy/src/main/java/io/quarkus/it/hibernate/search/orm/elasticsearch/multitenancy/fruit/FruitResource.java#L44-L80","documentation":"This REST resource rejects POST requests whose Fruit payload already carries a non-null id. The server assigns identities on create, so a client-supplied id would silently override or collide with the generated key; the resource throws WebApplicationException with HTTP 422 (Unprocessable Entity) to force the client to omit it.","triggerScenarios":"POST /fruits with a JSON body containing a non-null \"id\" field, e.g. re-posting an entity previously fetched via GET instead of sending a fresh object.","commonSituations":"Clients copying an existing JSON record and re-posting it; test code reusing a fixture object after persisting it once; deserializers that default numeric id fields to 0/null inconsistently; frontend forms that include the id input even in create mode.","solutions":["Remove the id field (or set it to null) from the JSON body before POSTing","In the client, keep separate DTOs/models for create (no id) and update (with id)","Use JsonInclude.NON_NULL / @JsonInclude so an unset id is not serialized","Clear the id on the object before persisting: fruit.setId(null)"],"exampleFix":"// before\nPOST /fruits\n{\"id\": 5, \"name\": \"Apple\"}\n\n// after\nPOST /fruits\n{\"name\": \"Apple\"}","handlingStrategy":"validation","validationCode":"if (fruit.getId() != null) {\n    throw new IllegalArgumentException(\"id must not be set when creating a fruit\");\n}","typeGuard":"boolean isValidForCreate(Fruit f) {\n    return f != null && f.getId() == null && f.getName() != null;\n}","tryCatchPattern":"try {\n    Response r = target.request().post(Entity.json(newFruit));\n    if (r.getStatus() == 422) { /* strip id and retry */ }\n} catch (WebApplicationException e) {\n    if (e.getResponse().getStatus() == 422) { fruit.setId(null); }\n}","preventionTips":["Use a separate create-DTO without an id field","Configure Jackson to omit nulls (@JsonInclude(NON_NULL))","Never reuse fetched entities for create calls","Add client-side check before POST"],"tags":["rest","validation","http-422","jpa"],"backgroundTag":"id-set-on-create-request","analyzedSha":"e1c734241f34c7919086ceb4c9262b4a58f6de44","analyzedAt":"2026-09-05T17:01:29.979Z","contentChangedAt":"2026-09-05T17:01:29.979Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}