All files / lib/model/oasis CsdlSchema.js

98.98% Statements 98/99
100% Branches 38/38
97.05% Functions 33/34
98.93% Lines 93/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 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374    4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x         4x                                                                                                             24x 175x     24x 111x     24x 3x     24x 24x 2x     24x 27x     24x 24x 24x                     2x 2x 1x         1x                   3x                         6x 6x 39x 8x   6x 1x     5x                                       64x 62x   434x       62x 2x 60x 1x         59x 59x 1x         58x                   29x 25x 25x   25x   24x   25x                                             64x   63x       1x         62x                       6x 6x 3x           3x 2x     6x                         24x 22x   2x 2x   1x             21x 20x 20x   1x                               23x 161x   75x     161x 642x           21x 147x 147x 56x           70x   70x 1x     69x       6x 6x           6x                 64x           63x                 4x  
"use strict";
 
const _ = require("lodash");
const Action = require("./schema/Action");
const ComplexType = require("./schema/ComplexType");
const CollectionType = require("./schema/CollectionType");
const EdmSimpleType = require("./schema/EdmSimpleType");
const EntityType = require("./schema/EntityType");
const FunctionType = require("./schema/Function");
const EntityContainer = require("./dataSource/EntityContainer");
const EnumType = require("./schema/EnumType");
const TypeDefinition = require("./schema/TypeDefinition");
const Extender = require("./extensions/Extender");
 
// Schema can contain elements edm:Annotation and edm:Term. They are used to define annotation vocabulary.
// These elements are used in schemas in services edmx:References. We currently do not validate service schema
// against them so edm:Annotation and edm:Term will not be implemented in schema for time being.
const childCollections = [
  {
    name: "enumTypes",
    sourceElement: "EnumType",
    Class: EnumType,
  },
  {
    name: "typeDefinitions",
    sourceElement: "TypeDefinition",
    Class: TypeDefinition,
  },
  {
    name: "complexTypes",
    sourceElement: "ComplexType",
    Class: ComplexType,
  },
  {
    name: "entityTypes",
    sourceElement: "EntityType",
    Class: EntityType,
  },
  {
    name: "actions",
    sourceElement: "Action",
    Class: Action,
  },
  {
    name: "functions",
    sourceElement: "Function",
    Class: FunctionType,
  },
  {
    name: "entityContainers",
    sourceElement: "EntityContainer",
    Class: EntityContainer,
  },
];
 
/**
 * Top-level conceptual schema definition language (CSDL) construct that allows creation of a namespace.
 * OASIS
 * 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 {EdmxModel} metaModel root point of metadata in-memory structure
   *
   * @memberof CsdlSchema
   */
  constructor(rawMetadata, settings, metaModel) {
    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,
    });
 
    CsdlSchema.initChildProperties(this, metaModel);
    CsdlSchema.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 : true));
  }
 
  /**
   * 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 = CsdlSchema.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.
   *
   * 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);
    let 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 = CsdlSchema.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,
        this.enumTypes,
        this.typeDefinitions,
      ];
    } 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}`
        );
      }
    }
  }
 
  /**
   * Initialize metadata collections (EntityType, Action,...)
   *
   * @param {CsdlSchema} schema - instance of the schema to append collections
   * @param {EdmxModel} metaModel - instance of metadata model class. The instance
   *        is passed to child instances for cross reference usage like basetype
   *        and entity types from differnet schemas
   */
  static initChildProperties(schema, metaModel) {
    childCollections.forEach((collection) => {
      let child = _.get(schema.raw, collection.sourceElement, []).map(
        (typeRawMetadata) => {
          return new collection.Class(typeRawMetadata, metaModel);
        }
      );
      Object.defineProperty(schema, collection.name, {
        get: () => child,
      });
    });
  }
 
  static initSchemaDependentProperties(schema) {
    childCollections.forEach((collection) => {
      let items = schema[collection.name];
      if (items.length > 0 && items[0].initSchemaDependentProperties) {
        items.forEach((item) => item.initSchemaDependentProperties(schema));
      }
    });
  }
 
  static matchPath(regex, path, name) {
    let matches = regex.exec(path);
 
    if (!matches) {
      throw new Error(`Unknown ${name} format: ${path}`);
    }
 
    return matches;
  }
 
  static parseTypePath(targetPath) {
    let collectionMatches = /Collection\((.+)\)/.exec(targetPath);
    let matches = CsdlSchema.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,
    };
  }
 
  static parseModelPath(targetPath) {
    let matches = CsdlSchema.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],
    };
  }
}
 
module.exports = CsdlSchema;