/// <summary>
        /// Register Request types 
        /// </summary>
        /// <param name="httpConfiguration">HttpConfiguration value</param>
        /// <param name="setSampleRequest">SampleRequest MethodInfo value</param>
        /// <param name="controllerName">ControllerName value</param>
        /// <param name="requestActions">RequestActions value</param>
        internal static void Register(HttpConfiguration httpConfiguration, MethodInfo setSampleRequest, string controllerName, IEnumerable<MethodInfo> requestActions, MethodInfo generateObject)
        {
            try
            {
                JsonMediaTypeFormatter jsonFormatter = null;

                if (httpConfiguration.Formatters != null && httpConfiguration.Formatters.Count > 0)
                {
                    jsonFormatter = httpConfiguration.Formatters.JsonFormatter;
                }

                foreach (var action in requestActions)
                {

                    if (action.GetParameters().Count() > 0) // Make sure request action is a parameterized action
                    {
                        // Documentation output builders
                        IFluentRequestBuilder jsonSampleBuilder = new JSONSampleBuilder(generateObject, jsonFormatter);
                        IFluentRequestBuilder xmlSampleBuilder = new XMLSampleBuilder(generateObject);

                        var actionName = action.Name;
                        var requestCustomAttribs = Attribute.GetCustomAttributes(action);

                        foreach (var parameter in action.GetParameters())
                        {
                            Type type = parameter.ParameterType;
                            string parameterName = parameter.Name;

                            if (requestCustomAttribs != null && requestCustomAttribs.Count() > 0)
                            {
                                // Check if the action is decorated with [RequestTypeAttribute] attribute class and if so grab the type from the attribute
                                var typeQuery = requestCustomAttribs.
                                    Where(rt => rt is RequestTypeAttribute).
                                    Where(p => p.TypeId.ToString().Equals(parameterName));
                                type = (typeQuery != null && typeQuery.Count() > 0) ?
                                    type = ((RequestTypeAttribute)typeQuery.FirstOrDefault()).Type : type;
                            }

                            jsonSampleBuilder = jsonSampleBuilder.BuildSample(type, parameterName);
                            xmlSampleBuilder = xmlSampleBuilder.BuildSample(type, parameterName);
                        }

                        var parameters = action.GetParameters().Select(a => a.Name).ToArray();

                        setSampleRequest.Invoke(null, new object[] { httpConfiguration, jsonSampleBuilder.Sample, new MediaTypeHeaderValue("text/json"), controllerName, actionName, parameters });

                        setSampleRequest.Invoke(null, new object[] { httpConfiguration, jsonSampleBuilder.Sample, new MediaTypeHeaderValue("application/json"), controllerName, actionName, parameters });

                        setSampleRequest.Invoke(null, new object[] { httpConfiguration, xmlSampleBuilder.Sample, new MediaTypeHeaderValue("text/xml"), controllerName, actionName, parameters });

                        setSampleRequest.Invoke(null, new object[] { httpConfiguration, xmlSampleBuilder.Sample, new MediaTypeHeaderValue("application/xml"), controllerName, actionName, parameters });
                    }
                }
            }
            catch (Exception exception)
            {
                throw exception;
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Register Request types
        /// </summary>
        /// <param name="httpConfiguration">HttpConfiguration value</param>
        /// <param name="setSampleRequest">SampleRequest MethodInfo value</param>
        /// <param name="controllerName">ControllerName value</param>
        /// <param name="requestActions">RequestActions value</param>
        internal static void Register(HttpConfiguration httpConfiguration, MethodInfo setSampleRequest, string controllerName, IEnumerable <MethodInfo> requestActions, MethodInfo generateObject)
        {
            try
            {
                JsonMediaTypeFormatter jsonFormatter = null;

                if (httpConfiguration.Formatters != null && httpConfiguration.Formatters.Count > 0)
                {
                    jsonFormatter = httpConfiguration.Formatters.JsonFormatter;
                }

                foreach (var action in requestActions)
                {
                    if (action.GetParameters().Count() > 0) // Make sure request action is a parameterized action
                    {
                        // Documentation output builders
                        IFluentRequestBuilder jsonSampleBuilder = new JSONSampleBuilder(generateObject, jsonFormatter);
                        IFluentRequestBuilder xmlSampleBuilder  = new XMLSampleBuilder(generateObject);

                        var actionName           = action.Name;
                        var requestCustomAttribs = Attribute.GetCustomAttributes(action);

                        foreach (var parameter in action.GetParameters())
                        {
                            Type   type          = parameter.ParameterType;
                            string parameterName = parameter.Name;

                            if (requestCustomAttribs != null && requestCustomAttribs.Count() > 0)
                            {
                                // Check if the action is decorated with [RequestTypeAttribute] attribute class and if so grab the type from the attribute
                                var typeQuery = requestCustomAttribs.
                                                Where(rt => rt is RequestTypeAttribute).
                                                Where(p => p.TypeId.ToString().Equals(parameterName));
                                type               = (typeQuery != null && typeQuery.Count() > 0) ?
                                              type = ((RequestTypeAttribute)typeQuery.FirstOrDefault()).Type : type;
                            }

                            jsonSampleBuilder = jsonSampleBuilder.BuildSample(type, parameterName);
                            xmlSampleBuilder  = xmlSampleBuilder.BuildSample(type, parameterName);
                        }

                        var parameters = action.GetParameters().Select(a => a.Name).ToArray();

                        setSampleRequest.Invoke(null, new object[] { httpConfiguration, jsonSampleBuilder.Sample, new MediaTypeHeaderValue("text/json"), controllerName, actionName, parameters });

                        setSampleRequest.Invoke(null, new object[] { httpConfiguration, jsonSampleBuilder.Sample, new MediaTypeHeaderValue("application/json"), controllerName, actionName, parameters });

                        setSampleRequest.Invoke(null, new object[] { httpConfiguration, xmlSampleBuilder.Sample, new MediaTypeHeaderValue("text/xml"), controllerName, actionName, parameters });

                        setSampleRequest.Invoke(null, new object[] { httpConfiguration, xmlSampleBuilder.Sample, new MediaTypeHeaderValue("application/xml"), controllerName, actionName, parameters });
                    }
                }
            }
            catch (Exception exception)
            {
                throw exception;
            }
        }
        /// <summary>
        /// Register Response types
        /// </summary>
        /// <param name="httpConfiguration">HttpConfiguration value</param>
        /// <param name="setSampleResponse">SampleRequest MethodInfo value</param>
        /// <param name="controllerName">ControllerName value</param>
        /// <param name="responseActions">ResponseActions value</param>
        internal static void Register(HttpConfiguration httpConfiguration, MethodInfo setSampleResponse, string controllerName, IEnumerable <MethodInfo> responseActions, MethodInfo generateObject)
        {
            try
            {
                JsonMediaTypeFormatter jsonFormatter = new JsonMediaTypeFormatter();

                if (httpConfiguration.Formatters != null && httpConfiguration.Formatters.Count > 0)
                {
                    jsonFormatter = httpConfiguration.Formatters.JsonFormatter;
                }

                foreach (var action in responseActions)
                {
                    var actionName = action.Name;

                    string jsonSample = string.Empty;
                    string xmlSample  = string.Empty;

                    IFluentResponseBuilder jsonSampleBuilder = new JSONSampleBuilder(generateObject, jsonFormatter);
                    IFluentResponseBuilder xmlSampleBuilder  = new XMLSampleBuilder(generateObject);

                    var responseCustomAttribs = Attribute.GetCustomAttributes(action);
                    if (responseCustomAttribs != null && responseCustomAttribs.Count() > 0)
                    {
                        // Check if the action is decorated with [ResponseTypeAttribute] attribute class and if so grab the type from the attribute
                        var typeQuery = responseCustomAttribs.
                                        Where(rt => rt is ResponseTypeAttribute);

                        if (typeQuery != null && typeQuery.Count() > 0)
                        {
                            jsonSample = BuildSample(jsonSampleBuilder, typeQuery.FirstOrDefault(), action.ReturnType);
                            xmlSample  = BuildSample(xmlSampleBuilder, typeQuery.FirstOrDefault(), action.ReturnType);
                        }
                        else
                        {
                            Type type = action.ReturnType;
                            jsonSample = jsonSampleBuilder.BuildSample(type).Sample;
                            xmlSample  = xmlSampleBuilder.BuildSample(type).Sample;
                        }
                    }
                    else
                    {
                        Type type = action.ReturnType;
                        jsonSample = jsonSampleBuilder.BuildSample(type).Sample;
                        xmlSample  = xmlSampleBuilder.BuildSample(type).Sample;
                    }

                    var parameters = action.GetParameters().Select(a => a.Name).ToArray();

                    setSampleResponse.Invoke(null, new object[] { httpConfiguration, jsonSample, new MediaTypeHeaderValue("text/json"), controllerName, actionName, parameters });

                    setSampleResponse.Invoke(null, new object[] { httpConfiguration, jsonSample, new MediaTypeHeaderValue("application/json"), controllerName, actionName, parameters });

                    setSampleResponse.Invoke(null, new object[] { httpConfiguration, xmlSample, new MediaTypeHeaderValue("text/xml"), controllerName, actionName, parameters });

                    setSampleResponse.Invoke(null, new object[] { httpConfiguration, xmlSample, new MediaTypeHeaderValue("application/xml"), controllerName, actionName, parameters });
                }
            }
            catch (Exception exception)
            {
                throw exception;
            }
        }
        /// <summary>
        /// Register Response types 
        /// </summary>
        /// <param name="httpConfiguration">HttpConfiguration value</param>
        /// <param name="setSampleResponse">SampleRequest MethodInfo value</param>
        /// <param name="controllerName">ControllerName value</param>
        /// <param name="responseActions">ResponseActions value</param>
        internal static void Register(HttpConfiguration httpConfiguration, MethodInfo setSampleResponse, string controllerName, IEnumerable<MethodInfo> responseActions, MethodInfo generateObject)
        {
            try
            {
                JsonMediaTypeFormatter jsonFormatter = new JsonMediaTypeFormatter();

                if (httpConfiguration.Formatters != null && httpConfiguration.Formatters.Count > 0)
                {
                    jsonFormatter = httpConfiguration.Formatters.JsonFormatter;
                }

                foreach (var action in responseActions)
                {
                    var actionName = action.Name;

                    string jsonSample = string.Empty;
                    string xmlSample = string.Empty;

                    IFluentResponseBuilder jsonSampleBuilder = new JSONSampleBuilder(generateObject, jsonFormatter);
                    IFluentResponseBuilder xmlSampleBuilder = new XMLSampleBuilder(generateObject);

                    var responseCustomAttribs = Attribute.GetCustomAttributes(action);
                    if (responseCustomAttribs != null && responseCustomAttribs.Count() > 0)
                    {
                        // Check if the action is decorated with [ResponseTypeAttribute] attribute class and if so grab the type from the attribute
                        var typeQuery = responseCustomAttribs.
                            Where(rt => rt is ResponseTypeAttribute);

                        if (typeQuery != null && typeQuery.Count() > 0)
                        {
                            jsonSample = BuildSample(jsonSampleBuilder, typeQuery.FirstOrDefault(), action.ReturnType);
                            xmlSample = BuildSample(xmlSampleBuilder, typeQuery.FirstOrDefault(), action.ReturnType);
                        }
                        else
                        {
                            Type type = action.ReturnType;
                            jsonSample = jsonSampleBuilder.BuildSample(type).Sample;
                            xmlSample = xmlSampleBuilder.BuildSample(type).Sample;
                        }
                    }
                    else
                    {
                        Type type = action.ReturnType;
                        jsonSample = jsonSampleBuilder.BuildSample(type).Sample;
                        xmlSample = xmlSampleBuilder.BuildSample(type).Sample;
                    }

                    var parameters = action.GetParameters().Select(a => a.Name).ToArray();

                    setSampleResponse.Invoke(null, new object[] { httpConfiguration, jsonSample, new MediaTypeHeaderValue("text/json"), controllerName, actionName, parameters });

                    setSampleResponse.Invoke(null, new object[] { httpConfiguration, jsonSample, new MediaTypeHeaderValue("application/json"), controllerName, actionName, parameters });

                    setSampleResponse.Invoke(null, new object[] { httpConfiguration, xmlSample, new MediaTypeHeaderValue("text/xml"), controllerName, actionName, parameters });

                    setSampleResponse.Invoke(null, new object[] { httpConfiguration, xmlSample, new MediaTypeHeaderValue("application/xml"), controllerName, actionName, parameters });
                }
            }
            catch (Exception exception)
            {
                throw exception;
            }
        }