Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: [server side change only] convert Mongo's form data to raw / native query #10058

Merged
merged 11 commits into from
Apr 1, 2022
Prev Previous commit
Next Next commit
add TC for find
  • Loading branch information
sumitsum committed Dec 28, 2021
commit 8fcd84ed2b6c32cec023c79873c16ad9cce0ae90
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.appsmith.external.models.ActionConfiguration;
import com.appsmith.external.models.DatasourceStructure;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
Expand Down Expand Up @@ -194,6 +196,15 @@ public String getRawQuery() {
query.put("skip", this.skip);
}

return query.toString();
String queryString = "";
try {
queryString = objectMapper.readTree(query.toString()).toPrettyString();
} catch (JsonProcessingException e) {
// TODO: add comment
e.printStackTrace();
queryString = "";
}

return queryString;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1929,4 +1929,47 @@ public void testSmartSubstitutionWithObjectIdInSingleQuotes() {
.verifyComplete();

}

@Test
public void testFormToNativeQueryConversionForFindCommand() {
ActionConfiguration actionConfiguration = new ActionConfiguration();

Map<String, Object> configMap = new HashMap<>();
setValueSafelyInFormData(configMap, SMART_SUBSTITUTION, Boolean.TRUE);
setValueSafelyInFormData(configMap, COMMAND, "FIND");
setValueSafelyInFormData(configMap, FIND_QUERY, "{{Input1.text}}");
setValueSafelyInFormData(configMap, FIND_SORT, "{ id: 1 }");
setValueSafelyInFormData(configMap, COLLECTION, "users");
actionConfiguration.setFormData(configMap);

ExecuteActionDTO executeActionDTO = new ExecuteActionDTO();
List<Param> params = new ArrayList<>();
Param param1 = new Param();
param1.setKey("Input1.text");
param1.setValue("{ age: { \"$gte\": 30 } }");
params.add(param1);
executeActionDTO.setParams(params);

pluginExecutor.extractAndSetNativeQueryFromFormData(actionConfiguration);
setValueSafelyInFormData(configMap, COMMAND, "RAW");
actionConfiguration.setBody(getValueSafelyFromFormData(configMap, "formToNativeQuery", String.class));

DatasourceConfiguration dsConfig = createDatasourceConfiguration();
Mono<MongoClient> dsConnectionMono = pluginExecutor.datasourceCreate(dsConfig);
Mono<Object> executeMono = dsConnectionMono.flatMap(conn -> pluginExecutor.executeParameterized(conn, executeActionDTO,
dsConfig, actionConfiguration));
StepVerifier.create(executeMono)
.assertNext(obj -> {
ActionExecutionResult result = (ActionExecutionResult) obj;
assertNotNull(result);
assertTrue(result.getIsExecutionSuccess());
assertNotNull(result.getBody());
assertEquals(2, ((ArrayNode) result.getBody()).size());
assertEquals(
List.of(new ParsedDataType(JSON), new ParsedDataType(RAW)).toString(),
result.getDataTypes().toString()
);
})
.verifyComplete();
}
}