private OpenApiParameter AddPrimitiveParameter(
            string name, OperationProcessorContext context, ContextualParameterInfo contextualParameter)
        {
            var operationParameter = context.DocumentGenerator.CreatePrimitiveParameter(name, contextualParameter);

            operationParameter.Kind       = OpenApiParameterKind.Query;
            operationParameter.IsRequired = operationParameter.IsRequired || contextualParameter.ParameterInfo.HasDefaultValue == false;

            if (contextualParameter.ParameterInfo.HasDefaultValue)
            {
                var defaultValue = context.SchemaGenerator.ConvertDefaultValue(
                    contextualParameter, contextualParameter.ParameterInfo.DefaultValue);

                if (_settings.SchemaType == SchemaType.Swagger2)
                {
                    operationParameter.Default = defaultValue;
                }
                else if (operationParameter.Schema.HasReference)
                {
                    operationParameter.Schema = new JsonSchema
                    {
                        Default = defaultValue,
                        OneOf   = { operationParameter.Schema }
                    };
                }
                else
                {
                    operationParameter.Schema.Default = defaultValue;
                }
            }

            context.OperationDescription.Operation.Parameters.Add(operationParameter);
            return(operationParameter);
        }
Esempio n. 2
0
        /// <summary>Gets the description of the given member (based on the DescriptionAttribute, DisplayAttribute or XML Documentation).</summary>
        /// <param name="parameter">The parameter.</param>
        /// <returns>The description or null if no description is available.</returns>
        public static string GetDescription(this ContextualParameterInfo parameter)
        {
            dynamic descriptionAttribute = parameter.ContextAttributes.FirstAssignableToTypeNameOrDefault("System.ComponentModel.DescriptionAttribute");

            if (descriptionAttribute != null && !string.IsNullOrEmpty(descriptionAttribute.Description))
            {
                return(descriptionAttribute.Description);
            }
            else
            {
                dynamic displayAttribute = parameter.ContextAttributes.FirstAssignableToTypeNameOrDefault("System.ComponentModel.DataAnnotations.DisplayAttribute");
                if (displayAttribute != null && !string.IsNullOrEmpty(displayAttribute.Description))
                {
                    return(displayAttribute.Description);
                }

                if (parameter != null)
                {
                    var summary = parameter.GetXmlDocs();
                    if (summary != string.Empty)
                    {
                        return(summary);
                    }
                }
            }

            return(null);
        }
        private OpenApiParameter AddFileParameter(OperationProcessorContext context, ContextualParameterInfo contextualParameter, bool isFileArray)
        {
            // TODO: Check if there is a way to control the property name
            var parameterDocumentation = contextualParameter.GetDescription(_settings);
            var operationParameter     = context.DocumentGenerator.CreatePrimitiveParameter(
                contextualParameter.Name, parameterDocumentation, contextualParameter);

            InitializeFileParameter(operationParameter, isFileArray);
            context.OperationDescription.Operation.Parameters.Add(operationParameter);
            return(operationParameter);
        }
        private OpenApiParameter TryAddFileParameter(
            OperationProcessorContext context, JsonTypeDescription typeInfo, ContextualParameterInfo contextualParameter)
        {
            var isFileArray             = IsFileArray(contextualParameter.Type, typeInfo);
            var hasSwaggerFileAttribute = contextualParameter.Attributes
                                          .FirstAssignableToTypeNameOrDefault("SwaggerFileAttribute", TypeNameStyle.Name) != null;

            if (typeInfo.Type == JsonObjectType.File ||
                typeInfo.Format == JsonFormatStrings.Binary ||
                hasSwaggerFileAttribute ||
                isFileArray)
            {
                return(AddFileParameter(context, contextualParameter, isFileArray));
            }

            return(null);
        }
        /// <summary>Gets the description of the given member (based on the DescriptionAttribute, DisplayAttribute or XML Documentation).</summary>
        /// <param name="parameter">The parameter.</param>
        /// <returns>The description or null if no description is available.</returns>
        public static string GetDescription(this ContextualParameterInfo parameter)
        {
            var description = GetDescription(parameter.ContextAttributes);

            if (description != null)
            {
                return(description);
            }

            var summary = parameter.GetXmlDocs();

            if (summary != string.Empty)
            {
                return(summary);
            }

            return(null);
        }
Esempio n. 6
0
        /// <summary>Gets the description of the given member (based on the DescriptionAttribute, DisplayAttribute or XML Documentation).</summary>
        /// <param name="parameter">The parameter.</param>
        /// <param name="xmlDocsSettings">The XML Docs settings.</param>
        /// <returns>The description or null if no description is available.</returns>
        public static string GetDescription(this ContextualParameterInfo parameter, IXmlDocsSettings xmlDocsSettings)
        {
            var description = GetDescription(parameter.ContextAttributes);

            if (description != null)
            {
                return(description);
            }

            if (xmlDocsSettings.UseXmlDocumentation)
            {
                var summary = parameter.GetXmlDocs(xmlDocsSettings.ResolveExternalXmlDocumentation);
                if (summary != string.Empty)
                {
                    return(summary);
                }
            }

            return(null);
        }
Esempio n. 7
0
        /// <summary>Creates a primitive parameter for the given parameter information reflection object.</summary>
        /// <param name="name">The name.</param>
        /// <param name="contextualParameter">The parameter.</param>
        /// <returns>The parameter.</returns>
        public OpenApiParameter CreatePrimitiveParameter(string name, ContextualParameterInfo contextualParameter)
        {
            var documentation = contextualParameter.GetDescription();

            return(CreatePrimitiveParameter(name, documentation, contextualParameter));
        }
        private OpenApiParameter AddPrimitiveParametersFromUri(
            OperationProcessorContext context, string httpPath, string name, ContextualParameterInfo contextualParameter, JsonTypeDescription typeDescription)
        {
            var operation = context.OperationDescription.Operation;

            if (typeDescription.Type.HasFlag(JsonObjectType.Array))
            {
                var parameterDocumentation = contextualParameter.GetDescription(_settings);
                var operationParameter     = context.DocumentGenerator.CreatePrimitiveParameter(
                    name, parameterDocumentation, contextualParameter);

                operationParameter.Kind = OpenApiParameterKind.Query;
                operation.Parameters.Add(operationParameter);
                return(operationParameter);
            }
            else
            {
                foreach (var contextualProperty in contextualParameter.Type.GetContextualProperties())
                {
                    if (contextualProperty.ContextAttributes.Select(a => a.GetType()).All(a =>
                                                                                          !a.IsAssignableToTypeName("SwaggerIgnoreAttribute", TypeNameStyle.Name) &&
                                                                                          !a.IsAssignableToTypeName("JsonIgnoreAttribute", TypeNameStyle.Name)))
                    {
                        var fromQueryAttribute = contextualProperty.ContextAttributes.SingleOrDefault(a => a.GetType().Name == "FromQueryAttribute");
                        var propertyName       = fromQueryAttribute.TryGetPropertyValue <string>("Name") ??
                                                 context.SchemaGenerator.GetPropertyName(null, contextualProperty);

                        dynamic fromRouteAttribute = contextualProperty.ContextAttributes.SingleOrDefault(a => a.GetType().FullName == "Microsoft.AspNetCore.Mvc.FromRouteAttribute");
                        if (fromRouteAttribute != null && !string.IsNullOrEmpty(fromRouteAttribute?.Name))
                        {
                            propertyName = fromRouteAttribute?.Name;
                        }

                        dynamic fromHeaderAttribute = contextualProperty.ContextAttributes.SingleOrDefault(a => a.GetType().FullName == "Microsoft.AspNetCore.Mvc.FromHeaderAttribute");
                        if (fromHeaderAttribute != null && !string.IsNullOrEmpty(fromHeaderAttribute?.Name))
                        {
                            propertyName = fromHeaderAttribute?.Name;
                        }

                        var propertySummary    = contextualProperty.PropertyInfo.GetXmlDocsSummary(_settings.ResolveExternalXmlDocumentation);
                        var operationParameter = context.DocumentGenerator.CreatePrimitiveParameter(propertyName, propertySummary, contextualProperty.AccessorType);

                        // TODO: Check if required can be controlled with mechanisms other than RequiredAttribute

                        var parameterInfo = _settings.ReflectionService.GetDescription(contextualProperty.AccessorType, _settings);
                        var isFileArray   = IsFileArray(contextualProperty.AccessorType.Type, parameterInfo);

                        if (parameterInfo.Type == JsonObjectType.File || isFileArray)
                        {
                            InitializeFileParameter(operationParameter, isFileArray);
                        }
                        else if (fromRouteAttribute != null ||
                                 httpPath.ToLowerInvariant().Contains("{" + propertyName.ToLower() + "}") ||
                                 httpPath.ToLowerInvariant().Contains("{" + propertyName.ToLower() + ":"))
                        {
                            operationParameter.Kind          = OpenApiParameterKind.Path;
                            operationParameter.IsNullableRaw = false;
                            operationParameter.IsRequired    = true; // Path is always required => property not needed
                        }
                        else if (fromHeaderAttribute != null)
                        {
                            operationParameter.Kind = OpenApiParameterKind.Header;
                        }
                        else
                        {
                            operationParameter.Kind = OpenApiParameterKind.Query;
                        }

                        operation.Parameters.Add(operationParameter);
                    }
                }

                return(null);
            }
        }
        private OpenApiParameter AddBodyParameter(OperationProcessorContext context, string name, ContextualParameterInfo contextualParameter)
        {
            OpenApiParameter operationParameter;

            var typeDescription = _settings.ReflectionService.GetDescription(contextualParameter, _settings);
            var isRequired      = _settings.AllowNullableBodyParameters == false || contextualParameter.ContextAttributes.FirstAssignableToTypeNameOrDefault("RequiredAttribute", TypeNameStyle.Name) != null;
            var isNullable      = _settings.AllowNullableBodyParameters && (typeDescription.IsNullable && !isRequired);

            var operation = context.OperationDescription.Operation;

            if (contextualParameter.TypeName == "XmlDocument" || contextualParameter.Type.InheritsFromTypeName("XmlDocument", TypeNameStyle.Name))
            {
                operation.TryAddConsumes("application/xml");
                operationParameter = new OpenApiParameter
                {
                    Name   = name,
                    Kind   = OpenApiParameterKind.Body,
                    Schema = new JsonSchema
                    {
                        Type          = JsonObjectType.String,
                        IsNullableRaw = isNullable
                    },
                    IsNullableRaw = isNullable,
                    IsRequired    = isRequired,
                    Description   = contextualParameter.GetDescription(_settings)
                };
                operation.Parameters.Add(operationParameter);
            }
            else if (contextualParameter.Type.IsAssignableToTypeName("System.IO.Stream", TypeNameStyle.FullName))
            {
                operation.TryAddConsumes("application/octet-stream");
                operationParameter = new OpenApiParameter
                {
                    Name   = name,
                    Kind   = OpenApiParameterKind.Body,
                    Schema = new JsonSchema
                    {
                        Type          = JsonObjectType.String,
                        Format        = JsonFormatStrings.Binary,
                        IsNullableRaw = isNullable
                    },
                    IsNullableRaw = isNullable,
                    IsRequired    = isRequired,
                    Description   = contextualParameter.GetDescription(_settings)
                };
                operation.Parameters.Add(operationParameter);
            }
            else
            {
                operationParameter = new OpenApiParameter
                {
                    Name          = name,
                    Kind          = OpenApiParameterKind.Body,
                    IsRequired    = isRequired,
                    IsNullableRaw = isNullable,
                    Description   = contextualParameter.GetDescription(_settings),
                    Schema        = context.SchemaGenerator.GenerateWithReferenceAndNullability <JsonSchema>(
                        contextualParameter, isNullable, schemaResolver: context.SchemaResolver)
                };
                operation.Parameters.Add(operationParameter);
            }

            return(operationParameter);
        }