{"record":{"id":"39cf398d0527ff52","repo":"quarkusio/quarkus","slug":"id-was-invalidly-set-on-request-39cf39","errorCode":null,"errorMessage":"Id was invalidly set on request.","messagePattern":"Id was invalidly set on request\\.","errorType":"http","errorClass":"WebApplicationException","httpStatus":422,"severity":"warning","filePath":"integration-tests/hibernate-orm-tenancy/schema-mariadb/src/main/java/io/quarkus/it/hibernate/multitenancy/fruit/FruitResource.java","lineNumber":91,"sourceCode":"    }\n\n    @POST\n    @Transactional\n    @Path(\"fruits\")\n    public Response createDefault(@NotNull Fruit fruit) {\n        return create(fruit);\n    }\n\n    @POST\n    @Transactional\n    @Path(\"{tenant}/fruits\")\n    public Response createTenant(@NotNull Fruit fruit) {\n        return create(fruit);\n    }\n\n    private 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(201).build();\n    }\n\n    @PUT\n    @Path(\"fruits/{id}\")\n    @Transactional\n    public Fruit updateDefault(@PathParam(\"id\") int id, @NotNull Fruit fruit) {\n        return update(id, fruit);\n    }\n\n    @PUT\n    @Path(\"{tenant}/fruits/{id}\")\n    @Transactional\n    public Fruit updateTenant(@PathParam(\"id\") int id, @NotNull Fruit fruit) {\n        return update(id, fruit);","sourceCodeStart":73,"sourceCodeEnd":109,"githubUrl":"https://github.com/quarkusio/quarkus/blob/e1c734241f34c7919086ceb4c9262b4a58f6de44/integration-tests/hibernate-orm-tenancy/schema-mariadb/src/main/java/io/quarkus/it/hibernate/multitenancy/fruit/FruitResource.java#L73-L109","documentation":"Thrown by the create helper of the schema-mariadb FruitResource when the JSON body of a POST /fruits request already contains an id. The endpoint only supports creating new entities where the database generates the id, so a pre-set id is rejected with HTTP 422 (Unprocessable Entity) to prevent accidental upsert-like behavior.","triggerScenarios":"POSTing a Fruit payload that includes \"id\": <value> (non-null), e.g. client echoes back a previously fetched fruit object instead of sending a fresh one.","commonSituations":"Client deserializes a GET response into the same object it then re-POSTs; tests reusing a fixture Fruit instance that already has an id; frontend form bound to an existing entity when the user intended 'new'.","solutions":["Strip/null the id field before POSTing: fruit.setId(null)","Use a dedicated DTO/request object without an id for creation","If you meant to update an existing fruit, use PUT /fruits/{id} instead of POST","In tests, build a fresh Fruit (new Fruit(\"name\")) rather than reusing one loaded from the DB"],"exampleFix":"// before\nFruit existing = given().get(\"/fruits/1\").as(Fruit.class);\ngiven().body(existing).post(\"/fruits\"); // 422 - id already set\n// after\nFruit toCreate = new Fruit(existing.getName()); // id is null\ngiven().contentType(ContentType.JSON).body(toCreate).post(\"/fruits\"); // 201","handlingStrategy":"validation","validationCode":"if (fruit.getId() != null) {\n    fruit = new Fruit(fruit.getName()); // drop id before POST\n}\ngiven().contentType(ContentType.JSON).body(fruit).post(\"/fruits\");","typeGuard":"static Fruit withoutId(Fruit f) {\n    return f.getId() == null ? f : new Fruit(f.getName());\n}","tryCatchPattern":"try {\n    given().body(fruit).post(\"/fruits\");\n} catch (WebApplicationException e) {\n    if (e.getResponse().getStatus() == 422) { fruit.setId(null); /* retry POST */ }\n    else throw e;\n}","preventionTips":["Use a create-DTO without an id field for POST bodies","Never re-POST an entity fetched via GET; clone it and null the id","Distinguish POST (create) vs PUT (update) semantics in clients","Assert 201 (not 422) in tests that create fruit"],"tags":["rest","jaxrs","http-422","validation","post"],"backgroundTag":"invalid-request-payload","analyzedSha":"e1c734241f34c7919086ceb4c9262b4a58f6de44","analyzedAt":"2026-09-05T17:01:29.979Z","contentChangedAt":"2026-09-05T17:01:29.979Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}