public IEnumerable<ApiDescription> Map(ODataActionDescriptor actionDescriptor)
        {
            Contract.Requires(actionDescriptor != null);
            Contract.Ensures(Contract.Result<IEnumerable<ApiDescription>>() != null);

            throw new NotImplementedException();
        }
Beispiel #2
0
        public IEnumerable <ApiDescription> Map(ODataActionDescriptor actionDescriptor)
        {
            Contract.Requires(actionDescriptor != null);
            Contract.Ensures(Contract.Result <IEnumerable <ApiDescription> >() != null);

            throw new NotImplementedException();
        }
        public IEnumerable <ApiDescription> Map(ODataActionDescriptor oDataActionDescriptor)
        {
            var apiDocumentation = GetApiDocumentation(oDataActionDescriptor.ActionDescriptor);

            // parameters
            var parameterDescriptions = CreateParameterDescriptions(oDataActionDescriptor.ActionDescriptor);

            // request formatters
            var bodyParameter = parameterDescriptions.FirstOrDefault(description => description.Source == ApiParameterSource.FromBody);

            var supportedRequestBodyFormatters = bodyParameter != null
                ? oDataActionDescriptor.ActionDescriptor.Configuration.Formatters.Where(f => f is ODataMediaTypeFormatter && f.CanReadType(bodyParameter.ParameterDescriptor.ParameterType))
                : Enumerable.Empty <MediaTypeFormatter>();

            // response formatters
            var responseDescription         = CreateResponseDescription(oDataActionDescriptor.ActionDescriptor);
            var returnType                  = responseDescription.ResponseType ?? responseDescription.DeclaredType;
            var supportedResponseFormatters = returnType != null && returnType != typeof(void)
                ? oDataActionDescriptor.ActionDescriptor.Configuration.Formatters.Where(f => f is ODataMediaTypeFormatter && f.CanWriteType(returnType))
                : Enumerable.Empty <MediaTypeFormatter>();

            // Replacing the formatter tracers with formatters if tracers are present.
            supportedRequestBodyFormatters = GetInnerFormatters(supportedRequestBodyFormatters);
            supportedResponseFormatters    = GetInnerFormatters(supportedResponseFormatters);

            // get HttpMethods supported by an action. Usually there is one HttpMethod per action but we allow multiple of them per action as well.
            var supportedMethods = GetHttpMethodsSupportedByAction(oDataActionDescriptor.Route, oDataActionDescriptor.ActionDescriptor);

            var apiDescriptions = new List <ApiDescription>();

            foreach (var method in supportedMethods)
            {
                var apiDescription = new ApiDescription
                {
                    Documentation    = apiDocumentation,
                    HttpMethod       = method,
                    RelativePath     = oDataActionDescriptor.RelativePathTemplate.TrimStart('/'),
                    ActionDescriptor = oDataActionDescriptor.ActionDescriptor,
                    Route            = oDataActionDescriptor.Route
                };
                apiDescription.SupportedResponseFormatters.AddRange(supportedResponseFormatters);
                apiDescription.SupportedRequestBodyFormatters.AddRange(supportedRequestBodyFormatters.ToList());
                if (parameterDescriptions != null)
                {
                    apiDescription.ParameterDescriptions.AddRange(parameterDescriptions);
                }

                // Have to set ResponseDescription because it's internal!??
                apiDescription.GetType().GetProperty("ResponseDescription").SetValue(apiDescription, responseDescription);

                apiDescription.RelativePath = apiDescription.GetRelativePathForSwagger();

                apiDescriptions.Add(apiDescription);
            }
            return(apiDescriptions);
        }
        public IEnumerable <ApiDescription> Map(ODataActionDescriptor oDataActionDescriptor)
        {
            var apiDescriptions = new List <ApiDescription>();

            var apiDocumentation = GetApiDocumentation(oDataActionDescriptor.ActionDescriptor);

            var parameterDescriptions = CreateParameterDescriptions(oDataActionDescriptor.ActionDescriptor);

            PopulateApiDescriptions(oDataActionDescriptor, parameterDescriptions, apiDocumentation, apiDescriptions);

            return(apiDescriptions);
        }
        public IEnumerable<ApiDescription> Map(ODataActionDescriptor oDataActionDescriptor)
        {
            var apiDescriptions = new List<ApiDescription>();

            var apiDocumentation = GetApiDocumentation(oDataActionDescriptor.ActionDescriptor);

            var parameterDescriptions = CreateParameterDescriptions(oDataActionDescriptor.ActionDescriptor);

            PopulateApiDescriptions(oDataActionDescriptor, parameterDescriptions, apiDocumentation, apiDescriptions);

            return apiDescriptions;
        }
Beispiel #6
0
        private static Func <MediaTypeFormatter, bool> CanWriteODataType(ODataActionDescriptor oDataActionDescriptor, Type returnType)
        {
            return(mediaTypeFormatter =>
            {
                var oDataMediaTypeFormatter = mediaTypeFormatter as ODataMediaTypeFormatter;

                if (oDataMediaTypeFormatter != null)
                {
                    oDataMediaTypeFormatter.SetInstanceProperty("Request", oDataActionDescriptor.Request);
                    return mediaTypeFormatter.CanWriteType(returnType);
                }
                return false;
            });
        }
Beispiel #7
0
        private static Func <MediaTypeFormatter, bool> CanReadODataType(ODataActionDescriptor oDataActionDescriptor, ApiParameterDescription bodyParameter)
        {
            return(mediaTypeFormatter =>
            {
                var oDataMediaTypeFormatter = mediaTypeFormatter as ODataMediaTypeFormatter;

                if (oDataMediaTypeFormatter != null)
                {
                    oDataMediaTypeFormatter.SetInstanceProperty("Request", oDataActionDescriptor.Request);
                    return mediaTypeFormatter.CanReadType(bodyParameter.ParameterDescriptor.ParameterType);
                }
                return false;
            });
        }
        private static Func <MediaTypeFormatter, bool> CanWriteODataType(ODataActionDescriptor oDataActionDescriptor, Type returnType)
        {
            return(mediaTypeFormatter =>
            {
                var oDataMediaTypeFormatter = mediaTypeFormatter as ODataMediaTypeFormatter;

                if (oDataMediaTypeFormatter != null)
                {
                    var mediaType = oDataMediaTypeFormatter.SupportedMediaTypes.FirstOrDefault();
                    var instanceFormatter = oDataMediaTypeFormatter.GetPerRequestFormatterInstance(returnType,
                                                                                                   oDataActionDescriptor.Request, mediaType);
                    return instanceFormatter.CanWriteType(returnType);
                }
                return false;
            });
        }
        private static Func <MediaTypeFormatter, bool> CanReadODataType(ODataActionDescriptor oDataActionDescriptor, ApiParameterDescription bodyParameter)
        {
            return(mediaTypeFormatter =>
            {
                var oDataMediaTypeFormatter = mediaTypeFormatter as ODataMediaTypeFormatter;

                if (oDataMediaTypeFormatter != null)
                {
                    var mediaType = oDataMediaTypeFormatter.SupportedMediaTypes.FirstOrDefault();
                    var instanceFormatter = oDataMediaTypeFormatter.GetPerRequestFormatterInstance(bodyParameter.ParameterDescriptor.ParameterType,
                                                                                                   oDataActionDescriptor.Request, mediaType);
                    return instanceFormatter.CanReadType(bodyParameter.ParameterDescriptor.ParameterType);
                }
                return false;
            });
        }
        protected void PopulateApiDescriptions(ODataActionDescriptor oDataActionDescriptor, List <ApiParameterDescription> parameterDescriptions, string apiDocumentation, List <ApiDescription> apiDescriptions)
        {
            Contract.Requires(oDataActionDescriptor != null);
            Contract.Requires(apiDescriptions != null);

            // request formatters
            var bodyParameter = default(ApiParameterDescription);

            if (parameterDescriptions != null)
            {
                bodyParameter = parameterDescriptions.FirstOrDefault(description => description.Source == ApiParameterSource.FromBody);
            }

            var httpConfiguration = oDataActionDescriptor.ActionDescriptor.Configuration;

            Contract.Assume(httpConfiguration != null);
            var mediaTypeFormatterCollection = httpConfiguration.Formatters;
            var responseDescription          = oDataActionDescriptor.ActionDescriptor.CreateResponseDescription();
            IEnumerable <MediaTypeFormatter> supportedRequestBodyFormatters = new List <MediaTypeFormatter>();
            IEnumerable <MediaTypeFormatter> supportedResponseFormatters    = new List <MediaTypeFormatter>();

            if (mediaTypeFormatterCollection != null)
            {
                supportedRequestBodyFormatters = bodyParameter != null?mediaTypeFormatterCollection.Where(CanReadODataType(oDataActionDescriptor, bodyParameter)) : Enumerable.Empty <MediaTypeFormatter>();

                // response formatters
                var returnType = responseDescription.ResponseType ?? responseDescription.DeclaredType;
                supportedResponseFormatters = returnType != null && returnType != typeof(void) ? mediaTypeFormatterCollection.Where(CanWriteODataType(oDataActionDescriptor, returnType)) : Enumerable.Empty <MediaTypeFormatter>();


                // Replacing the formatter tracers with formatters if tracers are present.
                supportedRequestBodyFormatters = GetInnerFormatters(supportedRequestBodyFormatters);
                supportedResponseFormatters    = GetInnerFormatters(supportedResponseFormatters);
            }

            var supportedHttpMethods = GetHttpMethodsSupportedByAction(oDataActionDescriptor.Route, oDataActionDescriptor.ActionDescriptor);

            foreach (var supportedHttpMethod in supportedHttpMethods)
            {
                var apiDescription = new ApiDescription
                {
                    Documentation    = apiDocumentation,
                    HttpMethod       = supportedHttpMethod,
                    RelativePath     = oDataActionDescriptor.RelativePathTemplate.TrimStart('/'),
                    ActionDescriptor = oDataActionDescriptor.ActionDescriptor,
                    Route            = oDataActionDescriptor.Route
                };

                var apiSupportedResponseFormatters = apiDescription.SupportedResponseFormatters;
                Contract.Assume(apiSupportedResponseFormatters != null);
                apiSupportedResponseFormatters.AddRange(supportedResponseFormatters);

                var apiSupportedRequestBodyFormatters = apiDescription.SupportedRequestBodyFormatters;
                Contract.Assume(apiSupportedRequestBodyFormatters != null);
                apiSupportedRequestBodyFormatters.AddRange(supportedRequestBodyFormatters);

                if (parameterDescriptions != null)
                {
                    var apiParameterDescriptions = apiDescription.ParameterDescriptions;
                    Contract.Assume(apiParameterDescriptions != null);
                    apiParameterDescriptions.AddRange(parameterDescriptions);
                }

                // Have to set ResponseDescription because it's internal!??
                apiDescription.SetInstanceProperty("ResponseDescription", responseDescription);

                if (apiDescription.ParameterDescriptions != null)
                {
                    apiDescription.RelativePath = apiDescription.GetRelativePathForSwagger();
                }

                apiDescriptions.Add(apiDescription);
            }
        }
        private static Func<MediaTypeFormatter, bool> CanWriteODataType(ODataActionDescriptor oDataActionDescriptor, Type returnType)
        {
            return mediaTypeFormatter =>
            {
                var oDataMediaTypeFormatter = mediaTypeFormatter as ODataMediaTypeFormatter;

                if (oDataMediaTypeFormatter != null)
                {
                    var mediaType = oDataMediaTypeFormatter.SupportedMediaTypes.FirstOrDefault();
                    var instanceFormatter = oDataMediaTypeFormatter.GetPerRequestFormatterInstance(returnType,
                        oDataActionDescriptor.Request, mediaType);
                    return instanceFormatter.CanWriteType(returnType);
                }
                return false;
            };
        }
        protected void PopulateApiDescriptions(ODataActionDescriptor oDataActionDescriptor, List<ApiParameterDescription> parameterDescriptions, string apiDocumentation, List<ApiDescription> apiDescriptions)
        {
            Contract.Requires(oDataActionDescriptor != null);
            Contract.Requires(apiDescriptions != null);

            // request formatters
            var bodyParameter = default(ApiParameterDescription);
            if (parameterDescriptions != null)
            {
                bodyParameter = parameterDescriptions.FirstOrDefault(description => description.Source == ApiParameterSource.FromBody);
            }

            var httpConfiguration = oDataActionDescriptor.ActionDescriptor.Configuration;
            Contract.Assume(httpConfiguration != null);
            var mediaTypeFormatterCollection = httpConfiguration.Formatters;
            var responseDescription = oDataActionDescriptor.ActionDescriptor.CreateResponseDescription();
            IEnumerable<MediaTypeFormatter> supportedRequestBodyFormatters = new List<MediaTypeFormatter>();
            IEnumerable<MediaTypeFormatter> supportedResponseFormatters = new List<MediaTypeFormatter>();
            if (mediaTypeFormatterCollection != null)
            {
                supportedRequestBodyFormatters = bodyParameter != null ? mediaTypeFormatterCollection.Where(CanReadODataType(oDataActionDescriptor, bodyParameter)) : Enumerable.Empty<MediaTypeFormatter>();

                // response formatters
                var returnType = responseDescription.ResponseType ?? responseDescription.DeclaredType;
                supportedResponseFormatters = returnType != null && returnType != typeof (void) ? mediaTypeFormatterCollection.Where(CanWriteODataType(oDataActionDescriptor, returnType)) : Enumerable.Empty<MediaTypeFormatter>();


                // Replacing the formatter tracers with formatters if tracers are present.
                supportedRequestBodyFormatters = GetInnerFormatters(supportedRequestBodyFormatters);
                supportedResponseFormatters = GetInnerFormatters(supportedResponseFormatters);
            }

            var supportedHttpMethods = GetHttpMethodsSupportedByAction(oDataActionDescriptor.Route, oDataActionDescriptor.ActionDescriptor);
            foreach (var supportedHttpMethod in supportedHttpMethods)
            {
                var apiDescription = new ApiDescription
                {
                    Documentation = apiDocumentation,
                    HttpMethod = supportedHttpMethod,
                    RelativePath = oDataActionDescriptor.RelativePathTemplate.TrimStart('/'),
                    ActionDescriptor = oDataActionDescriptor.ActionDescriptor,
                    Route = oDataActionDescriptor.Route
                };

                var apiSupportedResponseFormatters = apiDescription.SupportedResponseFormatters;
                Contract.Assume(apiSupportedResponseFormatters != null);
                apiSupportedResponseFormatters.AddRange(supportedResponseFormatters);

                var apiSupportedRequestBodyFormatters = apiDescription.SupportedRequestBodyFormatters;
                Contract.Assume(apiSupportedRequestBodyFormatters != null);
                apiSupportedRequestBodyFormatters.AddRange(supportedRequestBodyFormatters);

                if (parameterDescriptions != null)
                {
                    var apiParameterDescriptions = apiDescription.ParameterDescriptions;
                    Contract.Assume(apiParameterDescriptions != null);
                    apiParameterDescriptions.AddRange(parameterDescriptions);
                }

                // Have to set ResponseDescription because it's internal!??
                apiDescription.SetInstanceProperty("ResponseDescription", responseDescription);

                if (apiDescription.ParameterDescriptions != null)
                {
                    apiDescription.RelativePath = apiDescription.GetRelativePathForSwagger();
                }

                apiDescriptions.Add(apiDescription);
            }
        }
        private static Func<MediaTypeFormatter, bool> CanReadODataType(ODataActionDescriptor oDataActionDescriptor, ApiParameterDescription bodyParameter)
        {
            return mediaTypeFormatter =>
            {
                var oDataMediaTypeFormatter = mediaTypeFormatter as ODataMediaTypeFormatter;

                if (oDataMediaTypeFormatter != null)
                {
                    var mediaType = oDataMediaTypeFormatter.SupportedMediaTypes.FirstOrDefault();
                    var instanceFormatter = oDataMediaTypeFormatter.GetPerRequestFormatterInstance(bodyParameter.ParameterDescriptor.ParameterType,
                        oDataActionDescriptor.Request, mediaType);
                    return instanceFormatter.CanReadType(bodyParameter.ParameterDescriptor.ParameterType);
                }
                return false;
            };
        }
        private static Func<MediaTypeFormatter, bool> CanWriteODataType(ODataActionDescriptor oDataActionDescriptor, Type returnType)
        {
            return mediaTypeFormatter =>
            {
                var oDataMediaTypeFormatter = mediaTypeFormatter as ODataMediaTypeFormatter;

                if (oDataMediaTypeFormatter != null)
                {
                    oDataMediaTypeFormatter.SetInstanceProperty("Request", oDataActionDescriptor.Request);
                    return mediaTypeFormatter.CanWriteType(returnType);
                }
                return false;
            };
        }
        private static Func<MediaTypeFormatter, bool> CanReadODataType(ODataActionDescriptor oDataActionDescriptor, ApiParameterDescription bodyParameter)
        {
            return mediaTypeFormatter =>
            {
                var oDataMediaTypeFormatter = mediaTypeFormatter as ODataMediaTypeFormatter;

                if (oDataMediaTypeFormatter != null)
                {
                    oDataMediaTypeFormatter.SetInstanceProperty("Request", oDataActionDescriptor.Request);
                    return mediaTypeFormatter.CanReadType(bodyParameter.ParameterDescriptor.ParameterType);
                }
                return false;
            };
        }