Пример #1
0
        public override IEnumerable <ValidationMessage> GetValidationMessages(Dictionary <string, Dictionary <string, Operation> > entity, RuleContext context)
        {
            // get all operation objects that are either of get or post type
            ServiceDefinition serviceDefinition = (ServiceDefinition)context.Root;
            var listOperations = entity.Values.SelectMany(opDict => opDict.Where(pair => pair.Key.ToLower().Equals("get") || pair.Key.ToLower().Equals("post")));

            foreach (var opPair in listOperations)
            {
                // if the operation id is not of type _list* or does not return an array type, skip
                if (!ListRegex.IsMatch(opPair.Value.OperationId) || !ValidationUtilities.IsXmsPageableOrArrayResponseOperation(opPair.Value, serviceDefinition))
                {
                    continue;
                }

                string collType = opPair.Value.Responses["200"]?.Schema?.Reference?.StripDefinitionPath();
                // if no response type defined skip
                if (collType == null)
                {
                    continue;
                }

                var collTypeDef = serviceDefinition.Definitions[collType];
                // if collection object has 2 properties or less (x-ms-pageable objects can have the nextlink prop)
                // and the object does not have a property named "value", show the warning
                if ((collTypeDef.Properties?.Count <= 2) && collTypeDef.Properties.All(prop => !(prop.Key.ToLower().Equals("value") && prop.Value.Type == DataType.Array)))
                {
                    yield return(new ValidationMessage(new FileObjectPath(context.File, context.Path), this, collType, opPair.Value.OperationId));
                }
            }
        }
Пример #2
0
        public override IEnumerable <ValidationMessage> GetValidationMessages(Dictionary <string, Dictionary <string, Operation> > entity, RuleContext context)
        {
            // get all operation objects that are either of get or post type
            var serviceDefinition = (ServiceDefinition)context.Root;

            foreach (var pathObj in entity)
            {
                var listOpIds = pathObj.Value
                                // operation should be a get or post
                                .Where(pair => (pair.Key.ToLower().Equals("get") || pair.Key.ToLower().Equals("post")))
                                // operation id should be of the form *_list(by)
                                .Where(pair => (ListByRegex.IsMatch(pair.Value.OperationId) || pair.Value.OperationId.ToLower().EndsWith("_list")))
                                // operation is xmspageable or returns an array
                                .Where(pair => (ValidationUtilities.IsXmsPageableOrArrayResponseOperation(pair.Value, serviceDefinition)))
                                // select the operation id
                                .Select(pair => pair.Value.OperationId);

                // if there are no operations matching our conditions, skip
                if (IsNullOrEmpty(listOpIds))
                {
                    continue;
                }

                // populate valid list operation names for given path
                List <string> opNames = GetValidListOpNames(pathObj.Key);

                // if url does not match any of the predefined regexes, skip
                if (opNames == null || opNames.Count == 0)
                {
                    continue;
                }

                // find if there are any operations that violate the rule
                var errOpIds = listOpIds.Where(opId => !opNames.Contains(opId.ToLower()));

                // no violations found, skip
                if (IsNullOrEmpty(errOpIds))
                {
                    continue;
                }
                // aggregate suggested op names in a single readable string for the formatter
                var suggestedNames = string.Join(", ", opNames);
                foreach (var errOpId in errOpIds)
                {
                    yield return(new ValidationMessage(new FileObjectPath(context.File, context.Path), this, errOpId, suggestedNames));
                }
            }
        }
        public override IEnumerable <ValidationMessage> GetValidationMessages(Dictionary <string, Dictionary <string, Operation> > entity, RuleContext context)
        {
            // get all operation objects that are either of get or post type
            var serviceDefinition = (ServiceDefinition)context.Root;
            var listOperations    = entity.Values.SelectMany(opDict => opDict.Where(pair => pair.Key.ToLower().Equals("get") || pair.Key.ToLower().Equals("post")));

            foreach (var opPair in listOperations)
            {
                // if the operation id is already of the type _list we can skip this check
                if (ListRegex.IsMatch(opPair.Value.OperationId))
                {
                    continue;
                }

                if (ValidationUtilities.IsXmsPageableOrArrayResponseOperation(opPair.Value, serviceDefinition))
                {
                    yield return(new ValidationMessage(new FileObjectPath(context.File, context.Path), this, opPair.Value.OperationId));
                }
            }
        }