コード例 #1
0
        /// <summary>
        /// Verify the code rule
        /// </summary>
        /// <param name="context">Service context</param>
        /// <param name="info">out paramater to return violation information when rule fail</param>
        /// <returns>true if rule passes; false otherwise</returns>
        public override bool?Verify(ServiceContext context, out ExtensionRuleViolationInfo info)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            bool?passed = null;

            info = null;

            var     edmxHelper = new EdmxHelper(XElement.Parse(context.MetadataDocument));
            var     segments   = ResourcePathHelper.GetPathSegments(context);
            UriType uriType;
            var     target = edmxHelper.GetTargetType(segments, out uriType);

            // to check this rule only when returned resource is a ComplexType
            if (uriType == UriType.URI3 || uriType == UriType.URI12)
            {
                JObject    jo      = JObject.Parse(context.ResponsePayload);
                var        version = JsonParserHelper.GetPayloadODataVersion(jo);
                string     name    = (uriType == UriType.URI3) ? ((EdmProperty)target).Name : ((ComplexType)target).Name;
                string     jsCore  = string.Format(fmtoreSchema, name);
                string     jSchema = JsonSchemaHelper.WrapJsonSchema(jsCore, version);
                TestResult testResult;
                passed = JsonParserHelper.ValidateJson(jSchema, context.ResponsePayload, out testResult);
                if (passed.HasValue && !passed.Value)
                {
                    info = new ExtensionRuleViolationInfo(this.ErrorMessage, context.Destination, context.ResponsePayload, testResult != null ? testResult.LineNumberInError : -1);
                }
            }

            return(passed);
        }
コード例 #2
0
        /// <summary>
        /// Verify the code rule
        /// </summary>
        /// <param name="context">Service context</param>
        /// <param name="info">out paramater to return violation information when rule fail</param>
        /// <returns>true if rule passes; false otherwise</returns>
        public override bool?Verify(ServiceContext context, out ExtensionRuleViolationInfo info)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            bool?passed = null;

            info = null;

            var     segments   = ResourcePathHelper.GetPathSegments(context).ToArray();
            var     edmxHelper = new EdmxHelper(XElement.Parse(context.MetadataDocument));
            UriType uriType;
            var     target = edmxHelper.GetTargetType(segments, out uriType);

            if (uriType == UriType.URI4 || uriType == UriType.URI5)
            {
                string  property = segments.LastOrDefault(s => !s.StartsWith("$"));
                JObject jo       = JObject.Parse(context.ResponsePayload);
                var     version  = JsonParserHelper.GetPayloadODataVersion(jo);

                string     coreSchema = string.Format(fmtCore, property);
                string     jSchema    = JsonSchemaHelper.WrapJsonSchema(coreSchema, version);
                TestResult testResult;
                passed = JsonParserHelper.ValidateJson(jSchema, context.ResponsePayload, out testResult);
                if (passed.HasValue && !passed.Value)
                {
                    info = new ExtensionRuleViolationInfo(this.ErrorMessage, context.Destination, context.ResponsePayload, testResult != null ? testResult.LineNumberInError : -1);
                }
            }

            return(passed);
        }
コード例 #3
0
        /// <summary>
        /// Verify the rule
        /// </summary>
        /// <param name="context">Service context</param>
        /// <param name="info">out paramater to return violation information when rule fail</param>
        /// <returns>true if rule passes; false otherwise</returns>
        public override bool?Verify(ServiceContext context, out ExtensionRuleViolationInfo info)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            bool?passed = null;

            info = null;

            var    qs        = HttpUtility.ParseQueryString(context.Destination.Query);
            string qExpand   = qs["$expand"];
            var    branches  = ResourcePathHelper.GetBranchedSegments(qExpand);
            var    brHeaders = from br in branches select br.FirstOrDefault();

            // to get collection of navigatio properties which is not expanded
            List <string> navProps = new List <string>();
            XElement      meta     = XElement.Parse(context.MetadataDocument);
            const string  fmtXPathToTypeProperty = "//*[local-name()='Schema' and @Namespace='{0}']/*[local-name()='EntityType' and @Name='{1}']/*[local-name()='NavigationProperty' and @Name]";
            string        xpath = string.Format(fmtXPathToTypeProperty, ResourcePathHelper.GetNamespaceName(context.EntityTypeFullName), ResourcePathHelper.GetBaseName(context.EntityTypeFullName));
            var           nodes = meta.XPathSelectElements(xpath);

            foreach (var n in nodes)
            {
                var name = ResourcePathHelper.GetBaseName(n.Attribute("Name").Value);
                if (!brHeaders.Any(x => x.Equals(name, StringComparison.Ordinal)))
                {
                    navProps.Add(name);
                }
            }

            if (navProps.Count() > 0)
            {
                JObject jo      = JObject.Parse(context.ResponsePayload);
                var     version = JsonParserHelper.GetPayloadODataVersion(jo);

                var    propSchemas    = navProps.Select(x => string.Format(fmtCore, x));
                string coreProperties = string.Join("\r\n,", propSchemas);
                string CoreObject     = string.Format(fmtObj, coreProperties);
                string jSchema        = JsonSchemaHelper.WrapJsonSchema(CoreObject, version);

                TestResult result;
                passed = JsonParserHelper.ValidateJson(jSchema, context.ResponsePayload, out result);
                if (!passed.HasValue)
                {
                    info = new ExtensionRuleViolationInfo(this.ErrorMessage, context.Destination, context.ResponsePayload, result != null ? result.LineNumberInError : -1);
                }
            }

            return(passed);
        }
コード例 #4
0
        /// <summary>
        /// Verify the code rule
        /// </summary>
        /// <param name="context">Service context</param>
        /// <param name="info">out paramater to return violation information when rule fail</param>
        /// <returns>true if rule passes; false otherwise</returns>
        public override bool?Verify(ServiceContext context, out ExtensionRuleViolationInfo info)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            bool?passed = null;

            info = null;

            var     edmxHelper = new EdmxHelper(XElement.Parse(context.MetadataDocument));
            var     segments   = ResourcePathHelper.GetPathSegments(context);
            UriType uriType;
            var     target = edmxHelper.GetTargetType(segments, out uriType);

            // to check this rule only when returned resource is a ComplexType
            if (uriType == UriType.URI3 || uriType == UriType.URI12)
            {
                JObject jo      = JObject.Parse(context.ResponsePayload);
                var     version = JsonParserHelper.GetPayloadODataVersion(jo);

                ComplexType ct      = (uriType == UriType.URI12) ? (ComplexType)target : (ComplexType)((EdmProperty)target).TypeUsage.EdmType;
                var         props   = from p in ct.Properties select p.Name;
                var         jsProps = props.Select(x => string.Format(@"""{0}"" : {{""required"" : false }}", x));
                string      jsCore  = string.Format(fmtCoreSchema, ct.Name, string.Join(",", jsProps.ToArray()));
                string      jSchema = JsonSchemaHelper.WrapJsonSchema(jsCore, version);
                TestResult  result;
                passed = JsonParserHelper.ValidateJson(jSchema, context.ResponsePayload, out result);
                if (passed.HasValue && !passed.Value)
                {
                    info = new ExtensionRuleViolationInfo(this.ErrorMessage, context.Destination, context.ResponsePayload, result != null ? result.LineNumberInError : -1);
                }
            }

            return(passed);
        }