private static string TryGetMemberComments(MemberInfo memberInfo, IEnumerable <XPathNavigator> xmlNavigators)
        {
            if (xmlNavigators == null)
            {
                return(string.Empty);
            }

            foreach (var xmlNavigator in xmlNavigators)
            {
                var nodeNameForMember = XmlCommentsNodeNameHelper.GetNodeNameForMember(memberInfo);
                var xpathNavigator1   = xmlNavigator.SelectSingleNode(
                    $"/doc/members/member[@name='{nodeNameForMember}']");
                var xpathNavigator2 = xpathNavigator1?.SelectSingleNode("summary");
                if (xpathNavigator2 != null)
                {
                    return(XmlCommentsTextHelper.Humanize(xpathNavigator2.InnerXml));
                }
            }

            return(string.Empty);
        }
        public IEnumerable <KeyValuePair <string, int> > GetEnumValuesSummary(Type type)
        {
            var lstRetn = new List <KeyValuePair <string, int> >();

            var enumTypeName = type.FullName;
            var xmlDoc       = _xmlDocs.FirstOrDefault(p =>
            {
                var xnMember = p.SelectSingleNode($"/doc/members/member[@name='T:{enumTypeName}']");
                return(xnMember != null);
            });

            var eVals = Enum.GetValues(type);

            foreach (var eVal in eVals)
            {
                var xnField = xmlDoc?.SelectSingleNode($"/doc/members/member[@name=\'F:{enumTypeName}.{eVal}\']");
                var sStr    = xnField != null?XmlCommentsTextHelper.Humanize(xnField.SelectSingleNode("summary").InnerXml) : eVal.ToString();

                lstRetn.Add(new KeyValuePair <string, int>(sStr, (int)eVal));
            }
            return(lstRetn);
        }
Beispiel #3
0
        public void Humanize_TrimsCommonSpaceIndentations()
        {
            var input = @"
                ## Heading 1

                  * list item 1

                ## Heading 2

                    POST /api/test

                    {
                      ""prop1"": {
                        ""name"": ""value""
                      }
                    }
            ";

            var output = XmlCommentsTextHelper.Humanize(input);

            Assert.Equal(
                @"## Heading 1

  * list item 1

## Heading 2

	POST /api/test

	{
	  ""prop1"": {
	    ""name"": ""value""
	  }
	}"    ,
                output,
                false,
                true
                );
        }
Beispiel #4
0
        private void ApplyPropertyTags(OpenApiRequestBody requestBody, PropertyInfo propertyInfo)
        {
            if (propertyInfo.DeclaringType != null && _excludedTypes.Any() && _excludedTypes.ToList().Contains(propertyInfo.DeclaringType))
            {
                return;
            }

            var propertyMemberName  = XmlCommentsNodeNameHelper.GetMemberNameForFieldOrProperty(propertyInfo);
            var propertySummaryNode = _xmlNavigator.SelectSingleNode($"/doc/members/member[@name='{propertyMemberName}']/{SummaryTag}");

            if (propertySummaryNode != null)
            {
                var propertyRemarksNode = _xmlNavigator.SelectSingleNode($"/doc/members/member[@name='{propertyMemberName}']/{RemarksTag}");
                if (propertyRemarksNode != null &&
                    !string.IsNullOrWhiteSpace(propertyRemarksNode.InnerXml) &&
                    !requestBody.Description.Contains(XmlCommentsTextHelper.Humanize(propertyRemarksNode.InnerXml)))
                {
                    requestBody.Description +=
                        $" ({XmlCommentsTextHelper.Humanize(propertyRemarksNode.InnerXml)})";
                }
            }
        }
Beispiel #5
0
        private void ApplyFieldOrPropertyTags(OpenApiSchema schema, MemberInfo fieldOrPropertyInfo)
        {
            if (fieldOrPropertyInfo.DeclaringType != null && _excludedTypes.Any() && _excludedTypes.ToList().Contains(fieldOrPropertyInfo.DeclaringType))
            {
                return;
            }

            var fieldOrPropertyMemberName = XmlCommentsNodeNameHelper.GetMemberNameForFieldOrProperty(fieldOrPropertyInfo);
            var fieldOrPropertyNode       = _xmlNavigator.SelectSingleNode($"/doc/members/member[@name='{fieldOrPropertyMemberName}']");
            var summaryNode = fieldOrPropertyNode?.SelectSingleNode(SummaryTag);

            if (summaryNode != null)
            {
                var remarksNode = fieldOrPropertyNode.SelectSingleNode(RemarksTag);
                if (remarksNode != null &&
                    !string.IsNullOrWhiteSpace(remarksNode.InnerXml) &&
                    !schema.Description.Contains(XmlCommentsTextHelper.Humanize(remarksNode.InnerXml)))
                {
                    schema.Description +=
                        $" ({XmlCommentsTextHelper.Humanize(remarksNode.InnerXml)})";
                }
            }
        }
        /// <summary>
        /// Apply filter.
        /// </summary>
        /// <param name="swaggerDoc"><see cref="OpenApiDocument"/>.</param>
        /// <param name="context"><see cref="DocumentFilterContext"/>.</param>
        public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context)
        {
            // Collect (unique) controller names and types in a dictionary
            var controllerNamesAndTypes = context.ApiDescriptions
                                          .Select(apiDesc => apiDesc.ActionDescriptor as ControllerActionDescriptor)
                                          .SkipWhile(actionDesc => actionDesc == null)
                                          .GroupBy(actionDesc => actionDesc.ControllerName)
                                          .Select(group => new KeyValuePair <string, Type>(group.Key, group.First().ControllerTypeInfo.AsType()));

            foreach (var nameAndType in controllerNamesAndTypes)
            {
                if (_excludedTypes.Any() && _excludedTypes.ToList().Contains(nameAndType.Value))
                {
                    continue;
                }

                var memberName = XmlCommentsNodeNameHelper.GetMemberNameForType(nameAndType.Value);
                var typeNode   = _xmlNavigator.SelectSingleNode(string.Format(MemberXPath, memberName));

                var summaryNode = typeNode?.SelectSingleNode(SummaryTag);
                if (summaryNode != null)
                {
                    var remarksNode = typeNode.SelectSingleNode(RemarksTag);
                    if (remarksNode != null && !string.IsNullOrWhiteSpace(remarksNode.InnerXml))
                    {
                        var tag = swaggerDoc.Tags.FirstOrDefault(t => t.Name.Equals(nameAndType.Key));
                        if (tag != null &&
                            !tag.Description.Contains(XmlCommentsTextHelper.Humanize(remarksNode.InnerXml)))
                        {
                            swaggerDoc.Tags.First(t => t.Name.Equals(nameAndType.Key)).Description +=
                                $" ({XmlCommentsTextHelper.Humanize(remarksNode.InnerXml)})";
                        }
                    }
                }
            }
        }
Beispiel #7
0
 private void ApplyPropertiesXmlToPropertyParameters(IList <IParameter> parameters, ApiDescription apiDescription)
 {
     if (parameters == null)
     {
         return;
     }
     foreach (IParameter parameter1 in (IEnumerable <IParameter>)parameters)
     {
         IParameter parameter = parameter1;
         ApiParameterDescription parameterDescription = apiDescription.ParameterDescriptions.Where <ApiParameterDescription>((Func <ApiParameterDescription, bool>)(p =>
         {
             if (p.ModelMetadata?.ContainerType != (Type)null)
             {
                 return(p.ModelMetadata?.PropertyName != null);
             }
             return(false);
         })).FirstOrDefault <ApiParameterDescription>((Func <ApiParameterDescription, bool>)(p => parameter.Name.Equals(p.Name, StringComparison.OrdinalIgnoreCase)));
         if (parameterDescription != null)
         {
             ModelMetadata modelMetadata = parameterDescription.ModelMetadata;
             MemberInfo    memberInfo    = ((IEnumerable <MemberInfo>)modelMetadata.ContainerType.GetMember(modelMetadata.PropertyName)).FirstOrDefault <MemberInfo>();
             if (!(memberInfo == (MemberInfo)null))
             {
                 XPathNavigator xpathNavigator1 = this._xmlNavigator.SelectSingleNode(string.Format("/doc/members/member[@name='{0}']", (object)XmlCommentsMemberNameHelper.GetMemberNameForMember(memberInfo)));
                 if (xpathNavigator1 != null)
                 {
                     XPathNavigator xpathNavigator2 = xpathNavigator1.SelectSingleNode("summary");
                     if (xpathNavigator2 != null)
                     {
                         parameter.Description = XmlCommentsTextHelper.Humanize(xpathNavigator2.InnerXml);
                     }
                 }
             }
         }
     }
 }
        private void ApplyFieldOrPropertyTags(OpenApiSchema schema, MemberInfo fieldOrPropertyInfo)
        {
            var fieldOrPropertyMemberName = XmlCommentsNodeNameHelper.GetMemberNameForFieldOrProperty(fieldOrPropertyInfo);
            var fieldOrPropertyNode       = _xmlNavigator.SelectSingleNode($"/doc/members/member[@name='{fieldOrPropertyMemberName}']");

            if (fieldOrPropertyNode == null)
            {
                return;
            }

            var summaryNode = fieldOrPropertyNode.SelectSingleNode("summary");

            if (summaryNode != null)
            {
                schema.Description = XmlCommentsTextHelper.Humanize(summaryNode.InnerXml);
            }

            var exampleNode = fieldOrPropertyNode.SelectSingleNode("example");

            if (exampleNode != null)
            {
                schema.Example = JsonMapper.CreateFromJson(exampleNode.InnerXml);
            }
        }
Beispiel #9
0
 private void ApplyResponsesXmlToResponses(IDictionary <string, Response> responses, XPathNodeIterator responseNodes)
 {
     while (responseNodes.MoveNext())
     {
         string attribute = responseNodes.Current.GetAttribute("code", "");
         (responses.ContainsKey(attribute) ? responses[attribute] : (responses[attribute] = new Response())).Description = XmlCommentsTextHelper.Humanize(responseNodes.Current.InnerXml);
     }
 }