public void Apply(
            ApiDescription apiDescription,
            OperationSpec operationSpec,
            ModelSpecRegistrar modelSpecRegistrar,
            ModelSpecGenerator modelSpecGenerator)
        {
            var responseTypeAttr = apiDescription.ActionDescriptor.GetCustomAttributes<ResponseTypeAttribute>().FirstOrDefault();
            if (responseTypeAttr == null) return;

            IEnumerable<ModelSpec> complexSpecs;
            var modelSpec = modelSpecGenerator.TypeToModelSpec(responseTypeAttr.Type, out complexSpecs);

            if (modelSpec.Type == "object")
            {
                operationSpec.Type = modelSpec.Id;
            }
            else
            {
                operationSpec.Type = modelSpec.Type;
                operationSpec.Format = modelSpec.Format;
                operationSpec.Items = modelSpec.Items;
                operationSpec.Enum = modelSpec.Enum;
            }
            modelSpecRegistrar.RegisterMany(complexSpecs);
        }
        public OperationSpec ApiDescriptionToOperationSpec(ApiDescription apiDescription, ModelSpecRegistrar modelSpecRegistrar)
        {
            var apiPath    = apiDescription.RelativePathSansQueryString();
            var paramSpecs = apiDescription.ParameterDescriptions
                             .Select(paramDesc => CreateParameterSpec(paramDesc, apiPath, modelSpecRegistrar))
                             .ToList();

            var operationSpec = new OperationSpec
            {
                Method           = apiDescription.HttpMethod.Method,
                Nickname         = apiDescription.Nickname(),
                Summary          = apiDescription.Documentation,
                Parameters       = paramSpecs,
                ResponseMessages = new List <ResponseMessageSpec>()
            };

            var returnType = apiDescription.ActionDescriptor.ReturnType;

            if (returnType == null)
            {
                operationSpec.Type = "void";
            }
            else
            {
                IEnumerable <ModelSpec> complexSpecs;
                var modelSpec = _modelSpecGenerator.TypeToModelSpec(returnType, out complexSpecs);

                modelSpecRegistrar.RegisterMany(complexSpecs);

                if (modelSpec.Type == "object")
                {
                    operationSpec.Type = modelSpec.Id;
                }
                else
                {
                    operationSpec.Type   = modelSpec.Type;
                    operationSpec.Format = modelSpec.Format;
                    operationSpec.Items  = modelSpec.Items;
                    operationSpec.Enum   = modelSpec.Enum;
                }
            }

            foreach (var filter in _operationFilters)
            {
                filter.Apply(apiDescription, operationSpec, modelSpecRegistrar, _modelSpecGenerator);
            }

            // IOperationSpecFilter is obsolete - below is for back-compat
            var modelSpecMap = new ModelSpecMap(modelSpecRegistrar, _modelSpecGenerator);

            foreach (var filter in _operationSpecFilters)
            {
                filter.Apply(apiDescription, operationSpec, modelSpecMap);
            }

            return(operationSpec);
        }
        public OperationSpec ApiDescriptionToOperationSpec(ApiDescription apiDescription, ModelSpecRegistrar modelSpecRegistrar)
        {
            var apiPath = apiDescription.RelativePathSansQueryString();
            var paramSpecs = apiDescription.ParameterDescriptions
                .Select(paramDesc => CreateParameterSpec(paramDesc, apiPath, modelSpecRegistrar))
                .ToList();

            var operationSpec = new OperationSpec
            {
                Method = apiDescription.HttpMethod.Method,
                Nickname = apiDescription.Nickname(),
                Summary = apiDescription.Documentation,
                Parameters = paramSpecs,
                ResponseMessages = new List<ResponseMessageSpec>()
            };

            var returnType = apiDescription.ActionDescriptor.ReturnType;
            if (returnType == null)
            {
                operationSpec.Type = "void";
            }
            else
            {
                IEnumerable<ModelSpec> complexSpecs;
                var modelSpec = _modelSpecGenerator.TypeToModelSpec(returnType, out complexSpecs);

                modelSpecRegistrar.RegisterMany(complexSpecs);

                if (modelSpec.Type == "object")
                {
                    operationSpec.Type = modelSpec.Id;
                }
                else
                {
                    operationSpec.Type = modelSpec.Type;
                    operationSpec.Format = modelSpec.Format;
                    operationSpec.Items = modelSpec.Items;
                    operationSpec.Enum = modelSpec.Enum;
                }
            }

            foreach (var filter in _operationFilters)
            {
                filter.Apply(apiDescription, operationSpec, modelSpecRegistrar, _modelSpecGenerator);
            }

            // IOperationSpecFilter is obsolete - below is for back-compat
            var modelSpecMap = new ModelSpecMap(modelSpecRegistrar, _modelSpecGenerator);
            foreach (var filter in _operationSpecFilters)
            {
                filter.Apply(apiDescription, operationSpec, modelSpecMap);
            }

            return operationSpec;
        }
 public void Apply(ApiDescription apiDescription, OperationSpec operationSpec, ModelSpecMap modelSpecMap)
 {
     if (apiDescription.ActionDescriptor.GetFilters().OfType<AuthorizeAttribute>().Any())
     {
         operationSpec.ResponseMessages.Add(new ResponseMessageSpec
             {
                 Code = (int) HttpStatusCode.Unauthorized,
                 Message = "Authentication required"
             });
     }
 }
        public OperationSpec From(ApiDescription apiDescription, ModelSpecRegistrar modelSpecRegistrar)
        {
            var apiPath = apiDescription.RelativePath.Split('?').First();
            var paramSpecs = apiDescription.ParameterDescriptions
                .Select(paramDesc => CreateParameterSpec(paramDesc, apiPath, modelSpecRegistrar))
                .ToList();

            var operationSpec = new OperationSpec
            {
                Method = apiDescription.HttpMethod.Method,
                Nickname = String.Format("{0}_{1}",
                    apiDescription.ActionDescriptor.ControllerDescriptor.ControllerName,
                    apiDescription.ActionDescriptor.ActionName),
                Summary = apiDescription.Documentation,
                Parameters = paramSpecs,
                ResponseMessages = new List<ResponseMessageSpec>()
            };

            var returnType = apiDescription.ActionDescriptor.ReturnType;
            if (returnType == null)
            {
                operationSpec.Type = "void";
            }
            else
            {
                var modelSpec = _modelSpecGenerator.From(returnType, modelSpecRegistrar);

                if (modelSpec.Type == "object")
                {
                    operationSpec.Type = modelSpec.Id;
                }
                else
                {
                    operationSpec.Type = modelSpec.Type;
                    operationSpec.Format = modelSpec.Format;
                    operationSpec.Items = modelSpec.Items;
                    operationSpec.Enum = modelSpec.Enum;
                }
            }

            foreach (var filter in _operationFilters)
            {
                filter.Apply(apiDescription, operationSpec, modelSpecRegistrar, _modelSpecGenerator);
            }

            // IOperationSpecFilter is obsolete - below is for back-compat
            var modelSpecMap = new ModelSpecMap(modelSpecRegistrar, _modelSpecGenerator);
            foreach (var filter in _operationSpecFilters)
            {
                filter.Apply(apiDescription, operationSpec, modelSpecMap);
            }

            return operationSpec;
        }
        public void Apply(ApiDescription apiDescription, OperationSpec operationSpec, ModelSpecMap modelSpecMap)
        {
            operationSpec.ResponseMessages.Add(new ResponseMessageSpec
                {
                    Code = (int) HttpStatusCode.OK,
                    Message = "It's all good!"
                });

            operationSpec.ResponseMessages.Add(new ResponseMessageSpec
            {
                Code = (int)HttpStatusCode.InternalServerError,
                Message = "Somethings up!"
            });
        }
        public void Apply(ApiDescription apiDescription, OperationSpec operationSpec, ModelSpecMap modelSpecMap)
        {
            var descriptionXml = XElement.Parse(apiDescription.Documentation);

            var summary = descriptionXml.Element("summary");
            operationSpec.Summary = summary != null ? summary.Value : descriptionXml.Value;

            var notes = descriptionXml.Element("remarks");
            if (notes != null)
                operationSpec.Notes = notes.Value;

            foreach (var error in descriptionXml.Elements("response"))
            {
                operationSpec.ResponseMessages.Add(new ResponseMessageSpec() { Code = Convert.ToInt32(error.Attribute("code").Value), Message = error.Value });
            }
        }
 private void ParameterSpec(OperationSpec operation, string name, Action<ParameterSpec> applyAssertions)
 {
     var parameterSpec = operation.Parameters.Single(param => param.Name == name);
     applyAssertions(parameterSpec);
 }
        private OperationSpec GenerateOperationSpec(ApiDescription apiDescription, ModelSpecMap modelSpecMap)
        {
            var apiPath = apiDescription.RelativePath.Split('?').First();
            var paramSpecs = apiDescription.ParameterDescriptions
                .Select(paramDesc => GenerateParameterSpec(paramDesc, apiPath, modelSpecMap))
                .ToList();

            var operationSpec = new OperationSpec
            {
                Method = apiDescription.HttpMethod.Method,
                Nickname = String.Format("{0}_{1}",
                    apiDescription.ActionDescriptor.ControllerDescriptor.ControllerName,
                    apiDescription.ActionDescriptor.ActionName),
                Summary = apiDescription.Documentation,
                Parameters = paramSpecs,
                ResponseMessages = new List<ResponseMessageSpec>()
            };

            var returnType = apiDescription.ActionDescriptor.ReturnType;
            if (returnType == null)
            {
                operationSpec.Type = "void";
            }
            else if (returnType != typeof(HttpResponseMessage))
            {
                var modelSpec = modelSpecMap.FindOrCreateFor(returnType);
                if (modelSpec.Type == "object")
                {
                    operationSpec.Type = modelSpec.Id;
                }
                else
                {
                    operationSpec.Type = modelSpec.Type;
                    operationSpec.Format = modelSpec.Format;
                    operationSpec.Items = modelSpec.Items;
                    operationSpec.Enum = modelSpec.Enum;
                }
            }

            foreach (var filter in _operationSpecFilters)
            {
                filter.Apply(apiDescription, operationSpec, modelSpecMap);
            }

            return operationSpec;
        }