Beispiel #1
0
        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 oDataActionDescriptor)
        {
            var apiDescriptions = new List <ApiDescription>();

            var apiDocumentation = GetApiDocumentation(oDataActionDescriptor.ActionDescriptor);

            var parameterDescriptions = CreateParameterDescriptions(oDataActionDescriptor.ActionDescriptor);

            PopulateApiDescriptions(oDataActionDescriptor, parameterDescriptions, apiDocumentation, apiDescriptions);

            return(apiDescriptions);
        }
Beispiel #3
0
        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;
            });
        }
Beispiel #4
0
        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;
            });
        }
Beispiel #5
0
        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);
            }
        }