Esempio n. 1
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);
            }
        }
Esempio n. 2
0
 /// <summary>
 /// Copy constructor
 /// </summary>
 public SwaggerParameter(SwaggerParameter copy) : base(copy)
 {
     this.Name     = copy.Name;
     this.Location = copy.Location;
 }