Exemple #1
0
        /// <summary>
        /// Creates a new swagger parameter
        /// </summary>
        public SwaggerParameter(MethodInfo method, ParameterInfo parameter, RestInvokeAttribute operation)
        {
            this.Name        = parameter.Name;
            this.Location    = operation.UriTemplate.Contains($"{{{parameter.Name}}}") ? SwaggerParameterLocation.path : SwaggerParameterLocation.body;
            this.Description = MetadataComposerUtil.GetElementDocumentation(method, parameter);

            SwaggerSchemaElementType type = SwaggerSchemaElementType.@string;

            if (parameter.ParameterType.StripNullable().IsEnum)
            {
                this.Enum = parameter.ParameterType.StripNullable().GetFields().Select(f => f.GetCustomAttributes <XmlEnumAttribute>().FirstOrDefault()?.Name).Where(o => !string.IsNullOrEmpty(o)).ToList();
                this.Type = SwaggerSchemaElementType.@string;
            }
            else if (!m_typeMap.TryGetValue(parameter.ParameterType, out type))
            {
                this.Schema = new SwaggerSchemaDefinition()
                {
                    Reference = $"#/definitions/{MetadataComposerUtil.CreateSchemaReference(parameter.ParameterType)}",
                    NetType   = parameter.ParameterType
                };
            }
            else
            {
                this.Type = type;
            }
        }
Exemple #2
0
        /// <summary>
        /// Create a swagger query parameter
        /// </summary>
        public SwaggerParameter(PropertyInfo queryFilter)
        {
            this.Name        = queryFilter.GetSerializationName() ?? queryFilter.GetCustomAttribute <Core.Model.Attributes.QueryParameterAttribute>()?.ParameterName;
            this.Description = MetadataComposerUtil.GetElementDocumentation(queryFilter);
            this.Location    = SwaggerParameterLocation.query;

            SwaggerSchemaElementType type = SwaggerSchemaElementType.@string;

            if (queryFilter.PropertyType.StripNullable().IsEnum)
            {
                this.Enum = queryFilter.PropertyType.StripNullable().GetFields().Select(f => f.GetCustomAttributes <XmlEnumAttribute>().FirstOrDefault()?.Name).Where(o => !string.IsNullOrEmpty(o)).ToList();
                this.Type = SwaggerSchemaElementType.@string;
            }
            else if (!m_typeMap.TryGetValue(queryFilter.PropertyType, out type))
            {
                this.Type = SwaggerSchemaElementType.@string;
            }
            else
            {
                this.Type = type;
            }
        }
Exemple #3
0
        /// <summary>
        /// Creates a new path definition
        /// </summary>
        public SwaggerPathDefinition(MethodInfo behaviorMethod, MethodInfo contractMethod) : this()
        {
            var operationAtt = contractMethod.GetCustomAttribute <RestInvokeAttribute>();

            this.Summary     = MetadataComposerUtil.GetElementDocumentation(contractMethod, MetaDataElementType.Summary) ?? MetadataComposerUtil.GetElementDocumentation(behaviorMethod, MetaDataElementType.Summary) ?? behaviorMethod.Name;
            this.Description = MetadataComposerUtil.GetElementDocumentation(contractMethod, MetaDataElementType.Remarks) ?? MetadataComposerUtil.GetElementDocumentation(behaviorMethod, MetaDataElementType.Remarks);
            this.Produces.AddRange(contractMethod.GetCustomAttributes <ServiceProducesAttribute>().Select(o => o.MimeType));
            this.Consumes.AddRange(contractMethod.GetCustomAttributes <ServiceConsumesAttribute>().Select(o => o.MimeType));

            // Obsolete?
            this.Obsolete = (behaviorMethod?.GetCustomAttribute <ObsoleteAttribute>() != null || contractMethod?.GetCustomAttribute <ObsoleteAttribute>() != null ||
                             behaviorMethod.DeclaringType.GetCustomAttribute <ObsoleteAttribute>() != null || contractMethod.DeclaringType.GetCustomAttribute <ObsoleteAttribute>() != null);

            var parms = contractMethod.GetParameters();

            if (parms.Length > 0)
            {
                this.Parameters = parms.Select(o => new SwaggerParameter(contractMethod, o, operationAtt)).ToList();
            }

            var demands = behaviorMethod.GetCustomAttributes <DemandAttribute>();

            if (demands.Count() > 0)
            {
                this.Security = new List <SwaggerPathSecurity>()
                {
                    new SwaggerPathSecurity()
                    {
                        { "oauth_user", demands.Select(o => o.PolicyId).ToList() }
                    }
                }
            }
            ;

            // Return type is not void
            if (behaviorMethod.ReturnType != typeof(void))
            {
                SwaggerSchemaElementType type = SwaggerSchemaElementType.@object;

                if (SwaggerSchemaElement.m_typeMap.TryGetValue(behaviorMethod.ReturnType, out type))
                {
                    this.Responses.Add(200, new SwaggerSchemaElement()
                    {
                        Type        = SwaggerSchemaElementType.@object,
                        Description = "Operation was completed successfully"
                    });
                }
                else
                {
                    // Get the response type name
                    this.Responses.Add(200, new SwaggerSchemaElement()
                    {
                        Type        = SwaggerSchemaElementType.@object,
                        Description = "Operation was completed successfully",
                        Schema      = new SwaggerSchemaDefinition()
                        {
                            NetType   = behaviorMethod.ReturnType,
                            Reference = $"#/definitions/{MetadataComposerUtil.CreateSchemaReference(behaviorMethod.ReturnType)}"
                        }
                    });
                }
            }
            else
            {
                this.Responses.Add(204, new SwaggerSchemaElement()
                {
                    Description = "There is not response for this method"
                });
            }

            // Any faults?
            foreach (var flt in contractMethod.GetCustomAttributes <ServiceFaultAttribute>())
            {
                this.Responses.Add(flt.StatusCode, new SwaggerSchemaElement()
                {
                    Description = flt.Condition,
                    Schema      = new SwaggerSchemaDefinition()
                    {
                        NetType   = flt.FaultType,
                        Reference = $"#/definitions/{MetadataComposerUtil.CreateSchemaReference(flt.FaultType)}"
                    }
                });
            }

            // Service Query PArameters
            foreach (var prm in contractMethod.GetCustomAttributes <UrlParameterAttribute>().Union(behaviorMethod.GetCustomAttributes <UrlParameterAttribute>()))
            {
                var sp = new SwaggerParameter()
                {
                    Description = prm.Description,
                    Name        = prm.Name,
                    Location    = SwaggerParameterLocation.query,
                    Type        = SwaggerSchemaElement.m_typeMap[prm.Type.StripNullable()],
                    Format      = SwaggerSchemaElement.m_formatMap[prm.Type.StripNullable()],
                    Required    = prm.Required
                };

                this.Parameters.Add(sp);
            }
        }
        /// <summary>
        /// Create a schema element from a property
        /// </summary>
        public SwaggerSchemaElement(PropertyInfo property)
        {
            this.Description = MetadataComposerUtil.GetElementDocumentation(property);

            SwaggerSchemaElementType type = SwaggerSchemaElementType.@string;

            if (property.PropertyType.StripNullable().IsEnum)
            {
                this.Enum = property.PropertyType.StripNullable().GetFields().Select(f => f.GetCustomAttributes <XmlEnumAttribute>().FirstOrDefault()?.Name).Where(o => !string.IsNullOrEmpty(o)).ToList();
                if (this.Enum.Count == 0)
                {
                    this.Enum = property.PropertyType.StripNullable().GetFields().Select(f => f.Name).Where(o => o != "value__").ToList();
                }
                this.Type = SwaggerSchemaElementType.@string;
            }
            else if (typeof(IList).IsAssignableFrom(property.PropertyType) || property.PropertyType.IsArray) // List or array {
            {
                this.Type = SwaggerSchemaElementType.array;

                Type elementType = null;
                if (property.PropertyType.IsArray)
                {
                    elementType = property.PropertyType.GetElementType();
                }
                else if (property.PropertyType.IsConstructedGenericType)
                {
                    elementType = property.PropertyType.GetGenericArguments()[0];
                }

                if (elementType == null || !m_typeMap.TryGetValue(elementType.StripNullable(), out type))
                {
                    this.Items = new SwaggerSchemaDefinition()
                    {
                        Type      = SwaggerSchemaElementType.@object,
                        Reference = elementType != null ? $"#/definitions/{MetadataComposerUtil.CreateSchemaReference(elementType)}" : null,
                        NetType   = elementType
                    }
                }
                ;
                else
                {
                    this.Items = new SwaggerSchemaDefinition()
                    {
                        Type   = type,
                        Format = m_formatMap[elementType.StripNullable()]
                    }
                };
            }
            else if (!m_typeMap.TryGetValue(property.PropertyType.StripNullable(), out type))
            {
                this.Schema = new SwaggerSchemaDefinition()
                {
                    Reference = $"#/definitions/{MetadataComposerUtil.CreateSchemaReference(property.PropertyType)}",
                    NetType   = property.PropertyType
                };
            }
            else
            {
                this.Type   = type;
                this.Format = m_formatMap[property.PropertyType.StripNullable()];
            }
            // XML info
            var xmlElement   = property.GetCustomAttributes <XmlElementAttribute>().FirstOrDefault();
            var xmlAttribute = property.GetCustomAttribute <XmlAttributeAttribute>();

            if (xmlElement != null)
            {
                this.Xml = new SwaggerXmlInfo(xmlElement);
            }
            else if (xmlAttribute != null)
            {
                this.Xml = new SwaggerXmlInfo(xmlAttribute);
            }
        }