public virtual Type ResolveType(
            ApiDescription api,
            string controllerName,
            string actionName,
            IEnumerable <string> parameterNames,
            SampleDirection sampleDirection,
            out Collection <MediaTypeFormatter> formatters
            )
        {
            if (!Enum.IsDefined(typeof(SampleDirection), sampleDirection))
            {
                throw new InvalidEnumArgumentException(
                          "sampleDirection",
                          (int)sampleDirection,
                          typeof(SampleDirection)
                          );
            }
            if (api == null)
            {
                throw new ArgumentNullException("api");
            }
            Type type;

            if (
                ActualHttpMessageTypes.TryGetValue(
                    new HelpPageSampleKey(
                        sampleDirection,
                        controllerName,
                        actionName,
                        parameterNames
                        ),
                    out type
                    ) ||
                ActualHttpMessageTypes.TryGetValue(
                    new HelpPageSampleKey(
                        sampleDirection,
                        controllerName,
                        actionName,
                        new[] { "*" }
                        ),
                    out type
                    )
                )
            {
                // Re-compute the supported formatters based on type
                Collection <MediaTypeFormatter> newFormatters = new Collection <MediaTypeFormatter>();
                foreach (var formatter in api.ActionDescriptor.Configuration.Formatters)
                {
                    if (IsFormatSupported(sampleDirection, formatter, type))
                    {
                        newFormatters.Add(formatter);
                    }
                }
                formatters = newFormatters;
            }
            else
            {
                switch (sampleDirection)
                {
                case SampleDirection.Request:
                    ApiParameterDescription requestBodyParameter =
                        api.ParameterDescriptions.FirstOrDefault(
                            p => p.Source == ApiParameterSource.FromBody
                            );
                    type =
                        requestBodyParameter == null
                                ? null
                                : requestBodyParameter.ParameterDescriptor.ParameterType;
                    formatters = api.SupportedRequestBodyFormatters;
                    break;

                case SampleDirection.Response:
                default:
                    type =
                        api.ResponseDescription.ResponseType
                        ?? api.ResponseDescription.DeclaredType;
                    formatters = api.SupportedResponseFormatters;
                    break;
                }
            }

            return(type);
        }
예제 #2
0
        private static bool TryGetResourceParameter(ApiDescription apiDescription, HttpConfiguration config, out ApiParameterDescription parameterDescription, out Type resourceType)
        {
            parameterDescription = apiDescription.ParameterDescriptions.FirstOrDefault(
                p => p.Source == ApiParameterSource.FromBody ||
                (p.ParameterDescriptor != null && p.ParameterDescriptor.ParameterType == typeof(HttpRequestMessage)));

            if (parameterDescription == null)
            {
                resourceType = null;
                return(false);
            }

            resourceType = parameterDescription.ParameterDescriptor.ParameterType;

            if (resourceType == typeof(HttpRequestMessage))
            {
                HelpPageSampleGenerator sampleGenerator = config.GetHelpPageSampleGenerator();
                resourceType = sampleGenerator.ResolveHttpRequestMessageType(apiDescription);
            }

            if (resourceType == null)
            {
                parameterDescription = null;
                return(false);
            }

            return(true);
        }
예제 #3
0
 /// <summary>
 /// Instantiates a new instance of <see cref="ApiParameterModel"/> with the provided <paramref name="apiParameterDescription"/>.
 /// </summary>
 public ApiParameterModel(ApiParameterDescription apiParameterDescription)
 {
     Name            = apiParameterDescription.Name;
     IsUriParameter  = apiParameterDescription.Source == ApiParameterSource.FromUri;
     IsBodyParameter = apiParameterDescription.Source == ApiParameterSource.FromBody;
 }