All files / lib/model/nw CsdlSchema.js

100% Statements 98/98
100% Branches 38/38
100% Functions 35/35
100% Lines 94/94

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348    1x 1x 1x 1x 1x 1x 1x 1x       1x                                               44x 176x 74x   176x 519x           44x 176x 176x 74x           89x   89x 1x     88x       14x 14x           14x                 75x           74x                                                     44x 224x     44x 144x     44x 2x     44x 44x 8x     44x 27x     44x 75x     44x 44x 44x                     3x 3x 1x         2x                   26x 26x                           14x 14x 169x 14x   14x 1x     13x                                       75x 73x   292x       73x 2x 71x 1x         70x 70x 1x         69x                   59x 24x 24x   24x   23x   24x                     7x                       75x   74x       1x         73x                       14x 14x 2x 12x 11x     14x                         23x 21x   2x 2x   1x             20x 19x 19x   1x               1x  
"use strict";
 
const _ = require("lodash");
const Association = require("./schema/Association");
const CollectionType = require("./schema/CollectionType");
const ComplexType = require("./schema/ComplexType");
const EntityType = require("./schema/EntityType");
const EdmSimpleType = require("./schema/EdmSimpleType");
const EntityContainer = require("./dataSource/EntityContainer");
const Extender = require("./extensions/Extender");
 
// schema level elements collections
// order is important (MS-CSDL requires this order), entity container elements references associations and types
const childCollections = [
  {
    name: "associations",
    sourceElement: "Association",
    Class: Association,
  },
  {
    name: "complexTypes",
    sourceElement: "ComplexType",
    Class: ComplexType,
  },
  {
    name: "entityTypes",
    sourceElement: "EntityType",
    Class: EntityType,
  },
  {
    name: "entityContainers",
    sourceElement: "EntityContainer",
    Class: EntityContainer,
  },
];
 
function initChildProperties(schema) {
  childCollections.forEach((collection) => {
    let child = _.get(schema.raw, collection.sourceElement, []).map(
      (t) => new collection.Class(t, schema.model)
    );
    Object.defineProperty(schema, collection.name, {
      get: () => child,
    });
  });
}
 
function initSchemaDependentProperties(schema) {
  childCollections.forEach((collection) => {
    let items = schema[collection.name];
    if (items.length > 0 && items[0].initSchemaDependentProperties) {
      items.forEach((item) => item.initSchemaDependentProperties(schema));
    }
  });
}
 
function matchPath(regex, path, name) {
  let matches = regex.exec(path);
 
  if (!matches) {
    throw new Error(`Unknown ${name} format: ${path}`);
  }
 
  return matches;
}
 
function parseTypePath(targetPath) {
  let collectionMatches = /Collection\((.+)\)/.exec(targetPath);
  let matches = matchPath(
    /([a-z0-9_\.]+)\.([a-z0-9_]+)$/i,
    collectionMatches ? collectionMatches[1] : targetPath,
    "type path"
  );
 
  return {
    path: matches[0],
    namespace: matches[1],
    name: matches[2],
    isCollection: !!collectionMatches,
  };
}
 
function parseModelPath(targetPath) {
  let matches = matchPath(
    /^([a-z0-9_\.]+)\.([a-z0-9_]+)(\/|\/([a-z0-9_]+))?$/i,
    targetPath,
    "model path"
  );
 
  return {
    path: matches[0],
    namespace: matches[1],
    element: matches[2],
    subElement: matches[4],
  };
}
 
/**
 * Top-level conceptual schema definition language (CSDL) construct that allows creation of a namespace.
 *
 * https://docs.microsoft.com/en-us/openspecs/windows_protocols/mc-csdl/f7d95765-3b64-4c77-b144-9d28862b0403
 * http://docs.oasis-open.org/odata/odata-csdl-xml/v4.01/cs01/odata-csdl-xml-v4.01-cs01.html
 *
 * @class CsdlSchema
 */
class CsdlSchema {
  /**
   * Creates an instance of CsdlSchema.
   *
   * @param {Object} rawMetadata raw metadata object for schema
   * @param {Object} [settings] (normalized) settings for the metadata, e.g. { strict: false } to ignore non critical errors
   * @param {Object} model reference to model which owns the schema
   *
   * @memberof CsdlSchema
   */
  constructor(rawMetadata, settings, model) {
    Object.defineProperty(this, "raw", {
      get: () => rawMetadata,
    });
 
    Object.defineProperty(this, "namespace", {
      get: () => rawMetadata.$.Namespace,
    });
 
    Object.defineProperty(this, "alias", {
      get: () => rawMetadata.$.Alias,
    });
 
    let extensions = [];
    Object.defineProperty(this, "extensions", {
      get: () => extensions,
    });
 
    Object.defineProperty(this, "settings", {
      get: () => settings,
    });
 
    Object.defineProperty(this, "model", {
      get: () => model,
    });
 
    initChildProperties(this);
    initSchemaDependentProperties(this);
    this.applyAnnotations(rawMetadata.Annotations);
  }
 
  /**
   * Gets an EntityType defined in schema
   *
   * @param {string} [name] type name
   * @returns {EntityType} type with given name
   * @memberof CsdlSchema
   */
  getEntityType(name) {
    let type = this.entityTypes.find((t) => t.name === name);
    if (!type) {
      throw new Error(
        `EntityType '${name}' not found in namespace '${this.namespace}'`
      );
    }
 
    return type;
  }
 
  /**
   * Gets entity container with given name (or default container).
   *
   * @param {string} [name] (optional) name of the container.
   * @returns {Object} entity container
   */
  getEntityContainer(name) {
    return this.entityContainers.find((c) =>
      name ? c.name === name : c.isDefault
    );
  }
 
  /**
   * Gets a Type available in schema
   *
   * Enumeration types, collection types and Untyped are not implemented.
   *
   * @param {string} [name] namespace qualified type name
   * @returns {EntityType|ComplexType|SimpleType} type with given name
   * @memberof CsdlSchema
   */
  getType(name) {
    let target = parseTypePath(name);
    let type = this._getTypeCollections(target.namespace)
      .map((types) => types.find((t) => t.name === target.name))
      .find((t) => !!t);
 
    if (!type) {
      throw new Error(`Unknown type '${name}'.`);
    }
 
    return target.isCollection ? new CollectionType(type) : type;
  }
 
  /**
   * Resolves model path expression.
   *
   * A model path is used within Annotation Path, Model Element Path, Navigation Property Path,
   * and Property Path expressions to traverse the model of a service and resolves to the model
   * element identified by the path
   *
   * Implemented only needed scope (OASIS-CSDL) and associations (MC-CSDL) (https://docs.microsoft.com/en-us/openspecs/windows_protocols/mc-csdl/77d7ccbb-bda8-444a-a160-f4581172322f).
   *
   * http://docs.oasis-open.org/odata/odata-csdl-xml/v4.01/cs01/odata-csdl-xml-v4.01-cs01.html#sec_Target
   * http://docs.oasis-open.org/odata/odata-csdl-xml/v4.01/cs01/odata-csdl-xml-v4.01-cs01.html#sec_PathExpressions
   *
   * @param {string} path model path expression
   * @returns {Object} schema element
   * @memberof CsdlSchema
   */
  resolveModelPath(path) {
    let target = this._parseModelPath(path);
    var child = childCollections
      .map((collection) =>
        this[collection.name].find((item) => item.name === target.element)
      )
      .filter(Boolean);
 
    if (child.length === 0) {
      throw new Error(`Can't find schema element for path "${path}".`);
    } else if (child.length > 1) {
      throw new Error(
        `Schema error: "${path}" matched ${child.length} schema elememts.`
      );
    }
 
    let element = child[0].resolveModelPath(target.subElement, this);
    if (!element) {
      throw new Error(
        `Can't resolve '${path}' model path. '${target.element}' found, but '${target.subElement}' did not resolve.`
      );
    }
 
    return element;
  }
 
  /**
   * Applies annotations to target elements structures.
   *
   * @param {Object[]} annotationsData raw annotaions data
   * @memberof CsdlSchema
   */
  applyAnnotations(annotationsData) {
    if (annotationsData && this.raw.EntityContainer) {
      let validItems = annotationsData.filter((annotation) =>
        _.get(annotation, "$.Target")
      );
      let annotationGroups = _.groupBy(
        validItems,
        (annotation) => annotation.$.Target
      );
      _.each(annotationGroups, this._applyAnnotationsToPath.bind(this));
    }
  }
 
  /**
   * Applies annotation based vendor extensions.
   *
   * @param {Object} [settings] parsing settings
   * @memberof CsdlSchema
   */
  applyExtensions(settings) {
    Extender.apply(this, settings);
  }
 
  /**
   * Creates path structure from model path.
   *
   * @param {string} [path] annotation target model path
   * @returns {Object} structure describing annotation target
   * @memberof CsdlSchema
   * @private
   */
  _parseModelPath(path) {
    let target = parseModelPath(path);
 
    if (
      target.namespace !== this.namespace &&
      target.namespace !== this.alias
    ) {
      throw new Error(
        `Annotation namespace/alias mismatch. (schema: '${this.namespace}'/'${this.alias}' vs annotation: '${target.namespace}'`
      );
    }
 
    return target;
  }
 
  /**
   * Gets type collections for a namespace.
   *
   * @param {string} [namespace] type namespace
   * @returns {Object[]} array of type collections
   * @memberof CsdlSchema
   * @private
   */
  _getTypeCollections(namespace) {
    let typeCollections = [];
    if (namespace === this.namespace) {
      typeCollections = [this.entityTypes, this.complexTypes];
    } else if (namespace === "Edm") {
      typeCollections = [EdmSimpleType.instances];
    }
 
    return typeCollections;
  }
 
  /**
   * Applies annotations to specific path. Error handling is done according to schema settings.
   *
   * @param {Object[]} [annotations] annotations to apply
   * @param {string} [path] target path
   * @memberof CsdlSchema
   * @private
   */
  _applyAnnotationsToPath(annotations, path) {
    let target;
    if (this.settings.strict) {
      target = this.resolveModelPath(path);
    } else {
      try {
        target = this.resolveModelPath(path);
      } catch (error) {
        this.settings.logger.warn(
          `Can't find annotation target for path '${path}'.`,
          error
        );
      }
    }
 
    if (target) {
      try {
        target.applyAnnotations(annotations);
      } catch (e) {
        throw new Error(
          `Error occured while applying annotations for '${path}': ${e.message}\n${e.stack}`
        );
      }
    }
  }
}
 
module.exports = CsdlSchema;