Beispiel #1
0
 private void AssignDefaultValues(EndPointMethodConfiguration configuration, IPathAttribute pathAttribute)
 {
     if (pathAttribute != null)
     {
         SetStatusAndResponseBodyConfigValues(configuration, pathAttribute.HasResponseBody, pathAttribute.SuccessCodeValue);
     }
     else
     {
         SetStatusAndResponseBodyConfigValues(configuration, null, null);
     }
 }
Beispiel #2
0
        private (string, string, bool) GenerateMethodPath(ICurrentApiInformation currentApi, Type type, string name,
                                                          MethodInfo methodInfo, List <Attribute> classAttributes, List <Attribute> methodAttributes,
                                                          IPathAttribute pathAttribute)
        {
            if (string.IsNullOrEmpty(name))
            {
                var basePath = (IBasePathAttribute)classAttributes.FirstOrDefault(a => a is IBasePathAttribute);

                name = basePath != null ?
                       basePath.BasePath :
                       _exposeConfigurations.RouteNameGenerator(type);
            }

            string fullPathString = null;

            var parameters = methodInfo.GetParameters();

            if (pathAttribute != null)
            {
                fullPathString = pathAttribute.Path;

                if (string.IsNullOrEmpty(fullPathString))
                {
                    fullPathString = GeneratePath(name, methodInfo, parameters, GetDefaultRequestBody(pathAttribute.Method, pathAttribute.HasRequestBody));
                }

                return(fullPathString, pathAttribute.Method, GetDefaultRequestBody(pathAttribute.Method, pathAttribute.HasRequestBody));
            }

            if (currentApi.DefaultMethod == ExposeDefaultMethod.PostOnly ||
                (currentApi.DefaultMethod == ExposeDefaultMethod.PostAndGet && parameters.Length > 0) ||
                (currentApi.DefaultMethod == ExposeDefaultMethod.PostAndGetInt && parameters.Any(p => p.ParameterType != typeof(int))))
            {
                fullPathString = GeneratePath(name, methodInfo, parameters, true);

                return(fullPathString, HttpMethods.Post, true);
            }

            fullPathString = GeneratePath(name, methodInfo, parameters, false);

            return(fullPathString, HttpMethods.Get, false);
        }
Beispiel #3
0
        private IEnumerable <EndPointMethodConfiguration> CreateEndPointMethodConfiguration(
            ICurrentApiInformation currentApi,
            Type type,
            ServiceActivationMethod serviceActivationMethod,
            Func <RequestExecutionContext, object> activationFunc,
            List <Attribute> classAttributes,
            string name,
            List <IEndPointMethodAuthorization> authorizations,
            string obsoleteMessage,
            MethodInfo methodInfo, List <Attribute> methodAttributes, IPathAttribute pathAttribute)
        {
            string methodPath;
            string methodVerb;
            bool   methodHasBody;

            (methodPath, methodVerb, methodHasBody) = GenerateMethodPath(currentApi, type, name, methodInfo, classAttributes, methodAttributes, pathAttribute);

            if (activationFunc == null)
            {
                activationFunc = GenerateActivation(currentApi, type, classAttributes, name, methodInfo, methodAttributes);
            }

            foreach (var routeInformation in GenerateRouteInformationList(methodPath, methodVerb, methodHasBody, currentApi, type, name, methodInfo, methodAttributes))
            {
                var configuration = new EndPointMethodConfiguration(routeInformation, activationFunc, new MethodInvokeInformation {
                    MethodToInvoke = methodInfo
                }, methodInfo.ReturnType);

                AssignDefaultValues(configuration, pathAttribute);

                var methodParameters = GenerateMethodParameters(type, methodInfo, routeInformation);

                configuration.Parameters.AddRange(methodParameters);

                configuration.Parameters.AddRange(AddInstancePropertyBindingParameters(configuration, type));

                var rawAttribute = (IRawContentAttribute)methodAttributes.FirstOrDefault(a => a is IRawContentAttribute);

                if (rawAttribute != null)
                {
                    configuration.RawContentType     = rawAttribute.ContentType;
                    configuration.RawContentEncoding = rawAttribute.ContentEncoding;
                }
                else if (string.IsNullOrEmpty(configuration.RawContentType))
                {
                    var returnType = methodInfo.ReturnType;

                    if (returnType.IsConstructedGenericType &&
                        (returnType.GetGenericTypeDefinition() == typeof(Task <>) ||
                         returnType.GetGenericTypeDefinition() == typeof(ValueTask <>)))
                    {
                        returnType = returnType.GenericTypeArguments[0];
                    }

                    if (_exposeConfigurations.TypeWrapSelector(returnType))
                    {
                        configuration.WrappedType = _wrappedResultTypeCreator.GetTypeWrapper(returnType);
                    }
                }

                var headerAttributes = classAttributes.Where(a => a is ResponseHeaderAttribute).ToList();
                headerAttributes.AddRange(methodAttributes.Where(a => a is ResponseHeaderAttribute));

                if (headerAttributes.Count > 0 ||
                    currentApi.Headers != ImmutableLinkedList <IResponseHeader> .Empty)
                {
                    var headers = new List <IResponseHeader>();

                    headers.AddRange(currentApi.Headers);

                    foreach (ResponseHeaderAttribute headerAttribute in headerAttributes)
                    {
                        headers.Add(new ResponseHeader.ResponseHeader(headerAttribute.Name, headerAttribute.Value));
                    }

                    configuration.ResponseHeaders = headers;
                }

                Func <IEndPointMethodConfigurationReadOnly, IEnumerable <IEndPointMethodAuthorization> >[]
                authorizationFunc = null;

                if (authorizations != null)
                {
                    authorizationFunc =
                        new Func <IEndPointMethodConfigurationReadOnly, IEnumerable <IEndPointMethodAuthorization> >[]
                    {
                        config => authorizations
                    };
                }

                ApplyAuthorizations(currentApi, authorizationFunc, configuration, classAttributes, methodAttributes);
                ApplyFilters(currentApi, GetFilterList(currentApi, configuration, classAttributes, methodAttributes), configuration);

                if (_supportCompression)
                {
                    configuration.SupportsCompression = _compressionSelectorService.ShouldCompressResult(configuration);
                }

                var returnTypeAttribute = (ReturnsTypeAttribute)methodAttributes.FirstOrDefault(a => a is ReturnsTypeAttribute);

                configuration.DocumentationReturnType = returnTypeAttribute?.ReturnType;

                yield return(configuration);
            }
        }