From fe94fd31845a658f7b3e76eca09b2a00c8df64b1 Mon Sep 17 00:00:00 2001 From: Harish Kumara Marappa Date: Fri, 4 Mar 2016 01:54:50 +0530 Subject: [PATCH] Fix for SVACE and Klocwork issues in RAML parser module. This includes code changes for following issues 1. SVACE reported issues. 2. Klocwork reported issues. 3. Setting array minimum, maximum, unique and additional items property properly. Change-Id: I7ffd8f985ff6db02c1469c6c86884f9c5baadaf5 Signed-off-by: Harish Kumara Marappa Reviewed-on: https://gerrit.iotivity.org/gerrit/5371 Tested-by: jenkins-iotivity Reviewed-by: Madan Lanka --- .../raml/jsonSchemaParser/JsonSchema.cpp | 31 +- .../raml/jsonSchemaParser/Properties.cpp | 23 +- .../ramlparser/raml/jsonSchemaParser/Properties.h | 451 ++++++++++----------- service/simulator/ramlparser/raml/model/Schema.h | 65 +-- 4 files changed, 278 insertions(+), 292 deletions(-) diff --git a/service/simulator/ramlparser/raml/jsonSchemaParser/JsonSchema.cpp b/service/simulator/ramlparser/raml/jsonSchemaParser/JsonSchema.cpp index bc69828..0207404 100755 --- a/service/simulator/ramlparser/raml/jsonSchemaParser/JsonSchema.cpp +++ b/service/simulator/ramlparser/raml/jsonSchemaParser/JsonSchema.cpp @@ -510,15 +510,15 @@ namespace RAML { if (itemValues->type == 5) { - int item_size = cJSON_GetArraySize(itemValues); + //int item_size = cJSON_GetArraySize(itemValues); int item_index = 0; - do - { - cJSON *item = cJSON_GetArrayItem(itemValues, item_index); - readItems(item, property); - break; - } - while ( ++item_index < item_size); + //do + //{ + cJSON *item = cJSON_GetArrayItem(itemValues, item_index); + readItems(item, property); + //break; + //} + //while ( ++item_index < item_size); } else { @@ -527,7 +527,7 @@ namespace RAML } cJSON *itemsMax = cJSON_GetObjectItem(childProperties, "maxItems"); int min = INT_MIN, max = INT_MAX; - bool unique = cJSON_True, addItems = cJSON_True; + bool unique = cJSON_False, addItems = cJSON_False; if (itemsMax) { cJSON *exclusiveMax = cJSON_GetObjectItem(childProperties, "exclusiveMaximum"); @@ -649,6 +649,7 @@ namespace RAML { readAllowedValues(allowedvalues, prop, type); } + readValues(item, prop, type); prop->setTypeString(type); property->setValue(*prop); } @@ -718,6 +719,11 @@ namespace RAML void JsonSchema::readFile(std::string &fileName , JsonParameters ¶m) { + std::string name = fileName; + std::transform(name.begin(), name.end(), name.begin(), ::tolower); + if (name.compare("oic.baseresource.json") == 0) + return; + cJSON *json = m_includeResolver->readToJson(fileName); JsonSchemaPtr Refparser = std::make_shared(json, m_includeResolver); @@ -728,6 +734,11 @@ namespace RAML void JsonSchema::readFile(std::string &fileName , std::string &defName , JsonParameters ¶m) { + std::string name = fileName; + std::transform(name.begin(), name.end(), name.begin(), ::tolower); + if (name.compare("oic.baseresource.json") == 0) + return; + cJSON *json = m_includeResolver->readToJson(fileName); JsonSchemaPtr Refparser = std::make_shared(json, m_includeResolver); @@ -747,7 +758,7 @@ namespace RAML std::string fileName; if (! ref.empty()) { - std::size_t pos = ref.find(delimiter1); + std::size_t pos = std::string::npos; if ( (pos = ref.find(delimiter1)) != std::string::npos) { fileName = ref.substr(0, pos); diff --git a/service/simulator/ramlparser/raml/jsonSchemaParser/Properties.cpp b/service/simulator/ramlparser/raml/jsonSchemaParser/Properties.cpp index bc29606..8905525 100755 --- a/service/simulator/ramlparser/raml/jsonSchemaParser/Properties.cpp +++ b/service/simulator/ramlparser/raml/jsonSchemaParser/Properties.cpp @@ -137,10 +137,8 @@ namespace RAML m_typeString = type; } - Properties::TypeInfo::TypeInfo( - VariantType type = VariantType::UNKNOWN, - VariantType baseType = VariantType::UNKNOWN, - int depth = 0) + Properties::TypeInfo::TypeInfo(VariantType type, + VariantType baseType, int depth) : m_type (type), m_baseType(baseType), m_depth(depth) {} VariantType Properties::TypeInfo::type() const @@ -338,23 +336,24 @@ namespace RAML m_min(INT_MIN), m_max(INT_MAX), m_multipleOf(INT_MAX), - m_minItems(INT_MIN), - m_maxItems(INT_MAX), + m_minItems(1), + m_maxItems(20), m_unique(false), m_additionalItems(false) { if (type == ValueProperty::Type::ARRAY) { m_type = ValueProperty::Type::ARRAY; - m_minItems = minItems; - m_maxItems = maxItems; + + if (minItems > 0) + m_minItems = minItems; + + if (maxItems != INT_MAX && maxItems > m_minItems) + m_maxItems = maxItems; + m_unique = unique; m_additionalItems = additionalItems; } - else - { - m_type = ValueProperty::Type::UNKNOWN; - } } ValueProperty::Type ValueProperty::type() const diff --git a/service/simulator/ramlparser/raml/jsonSchemaParser/Properties.h b/service/simulator/ramlparser/raml/jsonSchemaParser/Properties.h index 04e9dc8..dd784d8 100755 --- a/service/simulator/ramlparser/raml/jsonSchemaParser/Properties.h +++ b/service/simulator/ramlparser/raml/jsonSchemaParser/Properties.h @@ -63,10 +63,11 @@ namespace RAML ARRAY, OBJECT }; + /** - * @class ValueProperty - * @brief This class provides data Model for Json Schema Value Property. - */ + * @class ValueProperty + * @brief This class provides data Model for Json Schema Value Property. + */ class ValueProperty { public: @@ -82,146 +83,145 @@ namespace RAML }; /** - * Constructor of ValueProperty. - */ + * Constructor of ValueProperty. + */ ValueProperty(); - /** - * Copy Constructor of ValueProperty. - * - * @param ValueProperty. - */ + ValueProperty(const ValueProperty &) = default; - /** - * Assignment operator for ValueProperty. - * - * @param ValueProperty. - */ + ValueProperty &operator=(const ValueProperty &) = default; - /** - * Copy Constructor of ValueProperty. - * - * @param ValueProperty. - */ + ValueProperty(ValueProperty &&) = default; - /** - * Assignment operator for ValueProperty. - * - * @param ValueProperty. - */ + ValueProperty &operator=(ValueProperty &&) = default; /** - * explicit Constructor of ValueProperty for Type RANGE. - * - * @param min - minimum value of property. - * @param max- maximum value of property. - * @param multipleOf- multipleOf value of property. - */ + * explicit Constructor of ValueProperty for Type RANGE. + * + * @param min - minimum value of property. + * @param max- maximum value of property. + * @param multipleOf- multipleOf value of property. + */ explicit ValueProperty(double min, double max, int multipleOf); + /** - * explicit Constructor of ValueProperty for Type VALUE_SET. - * - * @param valueSet - allowed values in the Properties. - */ + * explicit Constructor of ValueProperty for Type VALUE_SET. + * + * @param valueSet - allowed values in the Properties. + */ explicit ValueProperty(const std::vector &valueSet); + /** - * explicit Constructor of ValueProperty for Type VALUE_SET. - * - * @param valueSet - allowed values in the Properties. - */ + * explicit Constructor of ValueProperty for Type VALUE_SET. + * + * @param valueSet - allowed values in the Properties. + */ explicit ValueProperty(const std::vector &valueSet); + /** - * explicit Constructor of ValueProperty for Type VALUE_SET. - * - * @param valueSet - allowed values in the Properties. - */ + * explicit Constructor of ValueProperty for Type VALUE_SET. + * + * @param valueSet - allowed values in the Properties. + */ explicit ValueProperty(const std::vector &valueSet); + /** - * explicit Constructor of ValueProperty for Type VALUE_SET. - * - * @param valueSet - allowed values in the Properties. - */ + * explicit Constructor of ValueProperty for Type VALUE_SET. + * + * @param valueSet - allowed values in the Properties. + */ explicit ValueProperty(const std::vector &valueSet); + /** - * explicit Constructor of ValueProperty for Type VALUE_SET. - * - * @param valueSet - allowed values in the Properties. - */ + * explicit Constructor of ValueProperty for Type VALUE_SET. + * + * @param valueSet - allowed values in the Properties. + */ explicit ValueProperty(const std::vector &valueSet); + /** - * explicit Constructor of ValueProperty for Type PATTERN or FORMAT. - * - * @param type - ValueProperty Type. - * @param value - value for the pattern or format. - */ + * explicit Constructor of ValueProperty for Type PATTERN or FORMAT. + * + * @param type - ValueProperty Type. + * @param value - value for the pattern or format. + */ explicit ValueProperty(Type type, std::string value); + /** - * explicit Constructor of ValueProperty for Type ARRAY. - * - * @param type - ValueProperty Type. - * @param minItems - minimum elements in the Array property. - * @param maxItems - maximum elements in the Array property. - * @param unique - unique elements in the Array property. - * @param additionalItems - additional elements in the Array property. - */ + * explicit Constructor of ValueProperty for Type ARRAY. + * + * @param type - ValueProperty Type. + * @param minItems - minimum elements in the Array property. + * @param maxItems - maximum elements in the Array property. + * @param unique - unique elements in the Array property. + * @param additionalItems - additional elements in the Array property. + */ explicit ValueProperty(Type type, int minItems, int maxItems, bool unique, bool additionalItems); /** - * This method is for getting type of ValueProperty. - * - * @return Type of ValueProperty - */ + * This method is for getting type of ValueProperty. + * + * @return Type of ValueProperty + */ Type type() const; + /** - * This method is for getting minimum value of ValueProperty. - * - * @return min value of ValueProperty - */ + * This method is for getting minimum value of ValueProperty. + * + * @return min value of ValueProperty + */ double min() const; + /** - * This method is for getting maximum value of ValueProperty. - * - * @return max value of ValueProperty - */ + * This method is for getting maximum value of ValueProperty. + * + * @return max value of ValueProperty + */ double max() const; + /** - * This method is for getting multipleOf value of ValueProperty. - * - * @return multipleOf value of ValueProperty - */ + * This method is for getting multipleOf value of ValueProperty. + * + * @return multipleOf value of ValueProperty + */ int multipleOf() const; + /** - * This method is for getting pattern value of ValueProperty. - * - * @return pattern value of ValueProperty - */ + * This method is for getting pattern value of ValueProperty. + * + * @return pattern value of ValueProperty + */ std::string pattern() const; + /** - * This method is for getting format value of ValueProperty. - * - * @return format value of ValueProperty - */ + * This method is for getting format value of ValueProperty. + * + * @return format value of ValueProperty + */ std::string format() const; + /** - * This method is for getting valueSet of ValueProperty. - * - * @return valueSet of ValueProperty - */ + * This method is for getting valueSet of ValueProperty. + * + * @return valueSet of ValueProperty + */ int valueSetSize() const; + /** - * This method is for getting valueSet of ValueProperty. - * - * @return valueSet of ValueProperty - */ + * This method is for getting valueSet of ValueProperty. + * + * @return valueSet of ValueProperty + */ std::vector valueSet() const; + /** - * This method is for getting valueArray of ValueProperty. - * - * @param minItems - reference to get minimum elements in the Array property. - * @param maxItems - reference to get maximum elements in the Array property. - * @param unique - reference to get unique elements in the Array property. - * @param additionalItems - reference to get additional elements in the Array property. - */ + * This method is for getting valueArray of ValueProperty. + * + * @param minItems - reference to get minimum elements in the Array property. + * @param maxItems - reference to get maximum elements in the Array property. + * @param unique - reference to get unique elements in the Array property. + * @param additionalItems - reference to get additional elements in the Array property. + */ void valueArray(int &minItems, int &maxItems, bool &unique, bool &additionalItems) const; private: @@ -246,78 +246,64 @@ namespace RAML { public: /** - * @class TypeInfo - * @brief This class provides type information of Json Properties. - */ + * @class TypeInfo + * @brief This class provides type information of Json Properties. + */ class TypeInfo { public: /** - * Constructor of TypeInfo. - * - * @param VariantType - type of property. - * @param VariantType - type of parent property. - * @param int - depth of property. - */ - TypeInfo(VariantType, VariantType, int); - /** - * Copy Constructor of TypeInfo. - * - * @param TypeInfo. - */ + * Constructor of TypeInfo. + * + * @param VariantType - type of property. + * @param VariantType - type of parent property. + * @param int - depth of property. + */ + TypeInfo(VariantType type = VariantType::UNKNOWN, + VariantType baseType = VariantType::UNKNOWN, + int depth = 0); + TypeInfo(const TypeInfo &) = default; - /** - * Assignment operator for TypeInfo. - * - * @param TypeInfo. - */ + TypeInfo &operator=(const TypeInfo &) = default; - /** - * Copy Constructor of TypeInfo. - * - * @param TypeInfo. - */ + TypeInfo(TypeInfo &&) = default; - /** - * Assignment operator for TypeInfo. - * - * @param TypeInfo. - */ + TypeInfo &operator=(TypeInfo &&) = default; - /** - * Constructor of TypeInfo. - */ - TypeInfo() = default; /** - * This method is for getting type of properties. - * - * @return VariantType of Property - */ + * This method is for getting type of properties. + * + * @return VariantType of Property + */ VariantType type() const; + /** - * This method is for getting base or parent type of properties. - * - * @return VariantType of parent Property - */ + * This method is for getting base or parent type of properties. + * + * @return VariantType of parent Property + */ VariantType baseType() const; + /** - * This method is for getting depth of properties. - * - * @return depth as int - */ + * This method is for getting depth of properties. + * + * @return depth as int + */ int depth() const; + /** - * operator for TypeInfo to check equality. - * - * @param TypeInfo. - */ + * operator for TypeInfo to check equality. + * + * @param TypeInfo. + */ bool operator ==(const TypeInfo &) const; + /** - * operator for TypeInfo to check inequality. - * - * @param TypeInfo. - */ + * operator for TypeInfo to check inequality. + * + * @param TypeInfo. + */ bool operator !=(const TypeInfo &) const; private: @@ -327,85 +313,69 @@ namespace RAML }; /** - * Constructor of Properties. - * - * @param name - Properties name as string. - */ + * Constructor of Properties. + * + * @param name - Properties name as string. + */ Properties(const std::string &name) : m_name(name) {} - /** - * Constructor of Properties. - */ + Properties() = default; - /** - * Copy Constructor of Properties. - * - * @param Properties. - */ + Properties(const Properties &) = default; - /** - * Assignment operator for Properties. - * - * @param Properties. - */ + Properties &operator=(const Properties &) = default; - /** - * Copy Constructor of Properties. - * - * @param Properties. - */ + Properties(Properties &&) = default; - /** - * Assignment operator for Properties. - * - * @param Properties. - */ + Properties &operator=(Properties &&) = default; /** - * This method is for getting TypeInfo of Properties. - * - * @return Properties TypeInfo - */ + * This method is for getting TypeInfo of Properties. + * + * @return Properties TypeInfo + */ TypeInfo getType() const; /** - * This method is for setting type of Properties. - * - * @param type -Propertie's Type - */ + * This method is for setting type of Properties. + * + * @param type -Propertie's Type + */ void setTypeString(const std::string &type); /** - * This method is for getting Name from Properties. - * - * @return Properties name as string - */ + * This method is for getting Name from Properties. + * + * @return Properties name as string + */ std::string getName() const; + /** - * This method is for setting name to Properties - * - * @param name - Properties name as string. - */ + * This method is for setting name to Properties + * + * @param name - Properties name as string. + */ void setName(const std::string &name); + /** - * This method is for getting Description from Properties. - * - * @return Description as string - */ + * This method is for getting Description from Properties. + * + * @return Description as string + */ std::string getDescription() const; /** - * This method is for setting Description to Properties - * - * @param description - Description as string. - */ + * This method is for setting Description to Properties + * + * @param description - Description as string. + */ void setDescription(const std::string &description); /** - * This method is for setting Value to Properties. - * - * @param value - Value of Properties - */ + * This method is for setting Value to Properties. + * + * @param value - Value of Properties + */ template void setValue(const T &value) { @@ -413,17 +383,17 @@ namespace RAML } /** - * This method is for getting Value from Properties. - * - * @return Properties Value - */ + * This method is for getting Value from Properties. + * + * @return Properties Value + */ ValueVariant getValue() const; /** - * This method is for getting Value from Properties. - * - * @return Properties Value - */ + * This method is for getting Value from Properties. + * + * @return Properties Value + */ template T getValue() const { @@ -431,38 +401,39 @@ namespace RAML } /** - * This method is for checking if default Value exists in the Properties. - * - * @return true if present and false if not present - */ + * This method is for checking if default Value exists in the Properties. + * + * @return true if present and false if not present + */ bool isDefaultValue() const { return ((m_value != nullptr) ? true : false); } + /** - * This method is for getting ValueProperty from Properties. - * - * @return vector of pointer to ValueProperty - */ + * This method is for getting ValueProperty from Properties. + * + * @return vector of pointer to ValueProperty + */ std::vector > const &getValueProperties() const; /** - * This method is for setting ValueProperty to Properties - * - * @param value - pointer to ValueProperty - */ + * This method is for setting ValueProperty to Properties + * + * @param value - pointer to ValueProperty + */ void setValueProperty(const std::shared_ptr &value); /** - * This method is for getting RequiredValue from Properties. - * - * @return list of RequiredValue as string - */ + * This method is for getting RequiredValue from Properties. + * + * @return list of RequiredValue as string + */ std::vector const &getRequiredValues() const; /** - * This method is for setting RequiredValue to Properties - * - * @param reqValue - RequiredValue as string. - */ + * This method is for setting RequiredValue to Properties + * + * @param reqValue - RequiredValue as string. + */ void setRequiredValue(const std::string &reqValue); private: diff --git a/service/simulator/ramlparser/raml/model/Schema.h b/service/simulator/ramlparser/raml/model/Schema.h index c1c95d3..b50233e 100755 --- a/service/simulator/ramlparser/raml/model/Schema.h +++ b/service/simulator/ramlparser/raml/model/Schema.h @@ -43,61 +43,66 @@ namespace RAML { public: /** - * This method is for getting CJson object of schema. - * - * @return pointer to cJSON. - */ + * This method is for getting CJson object of schema. + * + * @return pointer to cJSON. + */ virtual cJSON *getJson() const; /** - * This method is for setting schema as CJson object. - * - * @param cjson - Cjson pointer. - */ + * This method is for setting schema as CJson object. + * + * @param cjson - Cjson pointer. + */ virtual void setJson(cJSON *cjson); /** - * This method is for getting schema as string. - * - * @return string. - */ + * This method is for getting schema as string. + * + * @return string. + */ virtual std::string getSchema() const; /** - * This method is for setting schema as string. - * - * @param schema - schema string. - */ + * This method is for setting schema as string. + * + * @param schema - schema string. + */ virtual void setSchema(const std::string &schema); /** - * This method is for getting Properties from JsonSchema. - * - * @return pointer to JsonSchema. - */ + * This method is for getting Properties from JsonSchema. + * + * @return pointer to JsonSchema. + */ virtual JsonSchemaPtr const &getProperties() const; /** - * Constructor of Schema. - * - * @param schema - contents of schema to be parsed - * @param includeResolver - Reference to IncludeResolver for reading external files - * - */ + * Constructor of Schema. + * + * @param schema - contents of schema to be parsed + * @param includeResolver - Reference to IncludeResolver for reading external files + * + */ Schema(const std::string &schema, const IncludeResolverPtr &includeResolver): m_schema(schema) , m_cjson(cJSON_Parse(schema.c_str())), m_resProperties(std::make_shared(m_cjson, includeResolver) ) , m_includeResolver(includeResolver) {} /** - * Constructor of Schema. - */ + * Constructor of Schema. + */ Schema(): m_cjson(NULL), m_resProperties(std::make_shared()), m_includeResolver(NULL) {} + Schema(const Schema&) = delete; + Schema& operator=(const Schema&) = delete; + Schema(Schema&&) = delete; + Schema& operator=(Schema&&) = delete; + /** - * Destructor of Schema. - */ + * Destructor of Schema. + */ ~Schema() { cJSON_Delete(m_cjson); } private: -- 2.7.4