diff options
author | Lester Solbakken <lesters@oath.com> | 2023-08-29 15:08:25 +0200 |
---|---|---|
committer | Lester Solbakken <lesters@oath.com> | 2023-08-29 15:08:25 +0200 |
commit | b53c16aa4bf60e05f75adab715f1a3bf278c2ad0 (patch) | |
tree | 99d50e68c71434f384554bf2aa4b9926c49f9cbf /model-integration | |
parent | 639deae84372d6f38944cf9a6b663ce8924a3bd7 (diff) |
Better error message when importing models with illegal names
Diffstat (limited to 'model-integration')
-rw-r--r-- | model-integration/src/main/java/ai/vespa/rankingexpression/importer/configmodelview/ImportedMlModels.java | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/model-integration/src/main/java/ai/vespa/rankingexpression/importer/configmodelview/ImportedMlModels.java b/model-integration/src/main/java/ai/vespa/rankingexpression/importer/configmodelview/ImportedMlModels.java index 05ff1aba877..f0cd0b01fa5 100644 --- a/model-integration/src/main/java/ai/vespa/rankingexpression/importer/configmodelview/ImportedMlModels.java +++ b/model-integration/src/main/java/ai/vespa/rankingexpression/importer/configmodelview/ImportedMlModels.java @@ -15,6 +15,7 @@ import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Future; +import java.util.regex.Pattern; /** * All models imported from the models/ directory in the application package. @@ -86,6 +87,7 @@ public class ImportedMlModels { Arrays.stream(dir.listFiles()).sorted().forEach(child -> { Optional<MlModelImporter> importer = findImporterOf(child, importers); if (importer.isPresent()) { + validateModelPath(child); String name = toName(child); Future<ImportedMlModel> existing = models.get(name); if (existing != null) { @@ -139,4 +141,27 @@ public class ImportedMlModels { return result.substring(0, result.length()-1); } + private static void validateModelPath(File modelFile) { + Pattern nameRegexp = Pattern.compile("[A-Za-z0-9_.]*"); + + Path path = Path.fromString(modelFile.toString()); + if (modelFile.isFile()) + path = stripFileEnding(path); + + boolean afterModels = false; + for (String element : path.elements()) { + if (afterModels) { + if ( ! nameRegexp.matcher(element).matches()) { + throw new IllegalArgumentException("When Vespa imports a model from the 'models' directory, it " + + "uses the directory structure under 'models' to determine the " + + "name of the model. The directory or file name '" + element + "' " + + "is not valid. Please rename this to only contain letters, " + + "numbers or underscores."); + } + } else if (element.equals("models")) { + afterModels = true; + } + } + } + } |