Commit 83f9311d authored by Alexander David Hellwig's avatar Alexander David Hellwig

Revert "Add required/optional field name check to StrictJsonDeserializer"

This reverts commit 94e49c79
parent 94e49c79
Pipeline #109178 canceled with stages
......@@ -10,7 +10,6 @@ import java.io.FileReader;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class CliParametersLoader {
......@@ -46,19 +45,17 @@ public class CliParametersLoader {
.create();
JsonDeserializer<CliParameters> desCliParameters = new StrictJsonDeserializer<>(Arrays.asList("emadlBackend","writeTagFile","clusteringParameters"), Arrays.asList("modelsDir","outputDir","rootModel","generators"), delegateGson);
JsonDeserializer<ClusteringParameters> desClustering = new StrictJsonDeserializer<>(Arrays.asList("numberOfClusters","flatten","flattenLevel","chooseBy","algorithmParameters"), Collections.emptyList(), delegateGson);
JsonDeserializer<DynamicSpectralClusteringCliParameters> desCliParameters = new StrictJsonDeserializer<>(Arrays.asList("modelsDir","outputDir","rootModel","generators","emadlBackend","writeTagFile","clusteringParameters"), delegateGson);
JsonDeserializer<ClusteringParameters> desClustering = new StrictJsonDeserializer<>(Arrays.asList("numberOfClusters","flatten","flattenLevel","chooseBy","algorithmParameters"), delegateGson);
JsonDeserializer<DynamicSpectralClusteringCliParameters> desSpectral = new StrictJsonDeserializer<>(Arrays.asList("numberOfClusters","l","sigma"), Collections.emptyList(), delegateGson);
JsonDeserializer<DynamicMarkovCliParameters> desMarkov = new StrictJsonDeserializer<>(Arrays.asList("max_residual","gamma_exp","loop_gain","zero_max"), Collections.emptyList(), delegateGson);
JsonDeserializer<DynamicDBScanCliParameters> desDBScan = new StrictJsonDeserializer<>(Collections.emptyList() ,Arrays.asList("min_pts","radius"), delegateGson);
JsonDeserializer<DynamicAffinityPropagationCliParameters> desAff = new StrictJsonDeserializer<>(Collections.emptyList(), Collections.emptyList(), delegateGson);
JsonDeserializer<DynamicSpectralClusteringCliParameters> desSpectral = new StrictJsonDeserializer<>(Arrays.asList("numberOfClusters","l","sigma"), delegateGson);
JsonDeserializer<DynamicSpectralClusteringCliParameters> desMarkov = new StrictJsonDeserializer<>(Arrays.asList("max_residual","gamma_exp","loop_gain","zero_max"), delegateGson);
JsonDeserializer<DynamicSpectralClusteringCliParameters> desDBScan = new StrictJsonDeserializer<>(Arrays.asList("min_pts","radius"), delegateGson);
Gson gson = new GsonBuilder()
.registerTypeAdapter(DynamicSpectralClusteringCliParameters.class, desSpectral)
.registerTypeAdapter(DynamicMarkovCliParameters.class, desMarkov)
.registerTypeAdapter(DynamicDBScanCliParameters.class, desDBScan)
.registerTypeAdapter(DynamicAffinityPropagationCliParameters.class, desAff)
.registerTypeAdapter(CliParameters.class, desCliParameters)
.registerTypeAdapter(ClusteringParameters.class, desClustering)
.create();
......
......@@ -4,19 +4,15 @@ import com.google.gson.*;
import de.se_rwth.commons.logging.Log;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
public class StrictJsonDeserializer<T> implements JsonDeserializer<T> {
private final List<String> optionalFieldNames;
private final List<String> requiredFieldNames;
private final List<String> fieldNames;
private final Gson delegate;
public StrictJsonDeserializer(List<String> optionalFieldNames, List<String> requiredFieldNames, Gson delegate) {
this.optionalFieldNames = optionalFieldNames;
this.requiredFieldNames = requiredFieldNames;
public StrictJsonDeserializer(List<String> fieldNames, Gson delegate) {
this.fieldNames = fieldNames;
this.delegate = delegate;
}
......@@ -26,31 +22,12 @@ public class StrictJsonDeserializer<T> implements JsonDeserializer<T> {
Log.error("Expecting json object!");
} else {
JsonObject jsonObj = jsonElement.getAsJsonObject();
Set<String> keys = jsonObj.keySet();
checkRequiredFields(keys);
checkOnlyValidFields(keys);
}
return delegate.fromJson(jsonElement, type);
}
private void checkOnlyValidFields(Set<String> keys) {
List<String> allFieldNames = new ArrayList<>();
allFieldNames.addAll(optionalFieldNames);
allFieldNames.addAll(requiredFieldNames);
for (String s : keys) {
if (!allFieldNames.contains(s)) {
Log.error("0x9DB6E: " + s + " is not a valid field name! Valid options: " + allFieldNames);
}
}
}
private void checkRequiredFields(Set<String> keys) {
for (String requiredFieldName : requiredFieldNames) {
if (!keys.contains(requiredFieldName)){
Log.error("0x4C433: can not find required field " + requiredFieldName);
for (String s : jsonObj.keySet()) {
if (!fieldNames.contains(s)) {
Log.error("0x9DB6E: " + s + " is not a valid field name! Valid options: " + fieldNames);
}
}
}
return delegate.fromJson(jsonElement, type);
}
}
......@@ -186,19 +186,4 @@ public class ParameterLoadingTest {
}
@Test
public void testMissingRequiredField() throws FileNotFoundException {
Log.enableFailQuick(false);
Log.getFindings().clear();
loadCliParameters("missingRequiredField");
assertEquals(1, Log.getErrorCount());
assertTrue(Log.getFindings().get(0).getMsg().contains("0x4C433"));
Log.getFindings().clear();
Log.enableFailQuick(true);
}
}
{
"mmodelsDir": "src/test/resources/pacman/",
"modelsDir": "src/test/resources/pacman/",
"outputDir": "target/cliTest/allAlgos/",
"rootModel": "de.rwth.pacman.heithoff2.controller",
"generators": ["cpp","roscpp"]
......
{
"outputDir": "target/cliTest/allAlgos/",
"rootModel": "de.rwth.pacman.heithoff2.controller",
"generators": ["cpp","roscpp"]
}
\ No newline at end of file
  • Checking of required/missing parameters is done on POJO level. This change increases effort to maintain without adding value

Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment