Browse Source

Fixed silent errors on invalid Json references in schemas

Ivan Savenko 2 years ago
parent
commit
27b75dbbea
3 changed files with 28 additions and 7 deletions
  1. 1 1
      config/schemas/townBuilding.json
  2. 15 4
      lib/JsonDetail.cpp
  3. 12 2
      lib/JsonNode.cpp

+ 1 - 1
config/schemas/townBuilding.json

@@ -17,7 +17,7 @@
 			"additionalItems" :
 			{
 				"description" : "Following items that contain expression elements",
-				"$ref" : "#/definitions/buidingRequirement"				
+				"$ref" : "#/definitions/buildingRequirement"				
 			}
 		} 
 	},

+ 15 - 4
lib/JsonDetail.cpp

@@ -728,7 +728,11 @@ namespace
 			//node must be validated using schema pointed by this reference and not by data here
 			//Local reference. Turn it into more easy to handle remote ref
 			if (boost::algorithm::starts_with(URI, "#"))
-				URI = validator.usedSchemas.back() + URI;
+			{
+				const std::string name = validator.usedSchemas.back();
+				const std::string nameClean = name.substr(0, name.find('#'));
+				URI = nameClean + URI;
+			}
 			return check(URI, data, validator);
 		}
 
@@ -739,9 +743,16 @@ namespace
 			auto checker = formats.find(schema.String());
 			if (checker != formats.end())
 			{
-				std::string result = checker->second(data);
-				if (!result.empty())
-					errors += validator.makeErrorMessage(result);
+				if (data.isString())
+				{
+					std::string result = checker->second(data);
+					if (!result.empty())
+						errors += validator.makeErrorMessage(result);
+				}
+				else
+				{
+					errors += validator.makeErrorMessage("Format value must be string: " + schema.String());
+				}
 			}
 			else
 				errors += validator.makeErrorMessage("Unsupported format type: " + schema.String());

+ 12 - 2
lib/JsonNode.cpp

@@ -1279,9 +1279,19 @@ const JsonNode & JsonUtils::getSchema(const std::string & URI)
 
 	// check if json pointer if present (section after hash in string)
 	if(posHash == std::string::npos || posHash == URI.size() - 1)
-		return getSchemaByName(filename);
+	{
+		auto const & result = getSchemaByName(filename);
+		if (result.isNull())
+			logMod->error("Error: missing schema %s", URI);
+		return result;
+	}
 	else
-		return getSchemaByName(filename).resolvePointer(URI.substr(posHash + 1));
+	{
+		auto const & result = getSchemaByName(filename).resolvePointer(URI.substr(posHash + 1));
+		if (result.isNull())
+			logMod->error("Error: missing schema %s", URI);
+		return result;
+	}
 }
 
 void JsonUtils::merge(JsonNode & dest, JsonNode & source, bool ignoreOverride, bool copyMeta)