diff --git a/app/server/appsmith-plugins/postgresPlugin/src/main/java/com/external/plugins/PostgresPlugin.java b/app/server/appsmith-plugins/postgresPlugin/src/main/java/com/external/plugins/PostgresPlugin.java index e686a851c05c..169a7a3e66e9 100644 --- a/app/server/appsmith-plugins/postgresPlugin/src/main/java/com/external/plugins/PostgresPlugin.java +++ b/app/server/appsmith-plugins/postgresPlugin/src/main/java/com/external/plugins/PostgresPlugin.java @@ -891,6 +891,8 @@ public Object substituteValueInInput(int index, break; } case STRING: { + } + case JSON_OBJECT: { preparedStatement.setString(index, value); break; } diff --git a/app/server/appsmith-plugins/postgresPlugin/src/test/java/com/external/plugins/PostgresPluginTest.java b/app/server/appsmith-plugins/postgresPlugin/src/test/java/com/external/plugins/PostgresPluginTest.java index 7375583d830d..b83fc1b6a0b1 100644 --- a/app/server/appsmith-plugins/postgresPlugin/src/test/java/com/external/plugins/PostgresPluginTest.java +++ b/app/server/appsmith-plugins/postgresPlugin/src/test/java/com/external/plugins/PostgresPluginTest.java @@ -1226,4 +1226,67 @@ public void testReadOnlyMode() { }) .verifyComplete(); } + + @Test + public void testPreparedStatementWithJsonDataType() { + DatasourceConfiguration dsConfig = createDatasourceConfiguration(); + + ActionConfiguration actionConfiguration = new ActionConfiguration(); + + String query = "INSERT INTO dataTypeTest VALUES ({{id}}, {{jsonObject1}}::json, {{jsonObject2}}::json, {{stringValue}})"; + actionConfiguration.setBody(query); + + List pluginSpecifiedTemplates = new ArrayList<>(); + pluginSpecifiedTemplates.add(new Property("preparedStatement", "true")); + actionConfiguration.setPluginSpecifiedTemplates(pluginSpecifiedTemplates); + + ExecuteActionDTO executeActionDTO = new ExecuteActionDTO(); + List params = new ArrayList<>(); + params.add(new Param("id", "10")); + params.add(new Param("jsonObject1", "{\"type\":\"racket\", \"manufacturer\":\"butterfly\"}")); + params.add(new Param("jsonObject2", "{\"country\":\"japan\", \"city\":\"kyoto\"}")); + params.add(new Param("stringValue", "Something here")); + executeActionDTO.setParams(params); + + Mono connectionCreateMono = pluginExecutor.datasourceCreate(dsConfig).cache(); + + Mono resultMono = connectionCreateMono + .flatMap(pool -> pluginExecutor.executeParameterized(pool, executeActionDTO, dsConfig, actionConfiguration)); + + StepVerifier.create(resultMono) + .assertNext(result -> { + + assertTrue(result.getIsExecutionSuccess()); + final JsonNode node = ((ArrayNode) result.getBody()).get(0); + assertEquals(node.get("affectedRows").asText(), "1"); + + List requestParams = (List) result.getRequest().getRequestParams(); + RequestParamDTO requestParamDTO = requestParams.get(0); + Map substitutedParams = requestParamDTO.getSubstitutedParams(); + for (Map.Entry substitutedParam : substitutedParams.entrySet()) { + PsParameterDTO psParameter = (PsParameterDTO) substitutedParam.getValue(); + switch (psParameter.getValue()) { + case "10" : + assertEquals(psParameter.getType(), "INTEGER"); + break; + case "{\"type\":\"racket\", \"manufacturer\":\"butterfly\"}": + + case "{\"country\":\"japan\", \"city\":\"kyoto\"}" : + assertEquals(psParameter.getType(), "JSON_OBJECT"); + break; + case "Something here" : + assertEquals(psParameter.getType(), "STRING"); + break; + } + } + + }) + .verifyComplete(); + + // Delete the newly added row to not affect any other test case + actionConfiguration.setBody("DELETE FROM users dataTypeTest id = 10"); + connectionCreateMono + .flatMap(pool -> pluginExecutor.executeParameterized(pool, executeActionDTO, dsConfig, actionConfiguration)).block(); + + } }