Exemplo n.º 1
0
        private void ProcessHttpRule <TRequest, TResponse>(Method <TRequest, TResponse> method, MethodDescriptor methodDescriptor, HttpRule httpRule)
            where TRequest : class
            where TResponse : class
        {
            if (ServiceDescriptorHelpers.TryResolvePattern(httpRule, out var pattern, out var httpVerb))
            {
                AddMethodCore(method, httpRule, pattern, httpVerb, httpRule.Body, httpRule.ResponseBody, methodDescriptor);
            }

            foreach (var additionalRule in httpRule.AdditionalBindings)
            {
                ProcessHttpRule(method, methodDescriptor, additionalRule);
            }
        }
Exemplo n.º 2
0
        private ApiDescriptionGroupCollection GetCollection()
        {
            var descriptions = new List <ApiDescription>();

            var endpoints = _endpointDataSource.Endpoints;

            foreach (var endpoint in endpoints)
            {
                if (endpoint is RouteEndpoint routeEndpoint)
                {
                    var grpcMetadata = endpoint.Metadata.GetMetadata <GrpcHttpMetadata>();

                    if (grpcMetadata != null)
                    {
                        var httpRule         = grpcMetadata.HttpRule;
                        var methodDescriptor = grpcMetadata.MethodDescriptor;

                        if (ServiceDescriptorHelpers.TryResolvePattern(grpcMetadata.HttpRule, out var pattern, out var verb))
                        {
                            var apiDescription = new ApiDescription();
                            apiDescription.HttpMethod       = verb;
                            apiDescription.ActionDescriptor = new ActionDescriptor
                            {
                                RouteValues = new Dictionary <string, string>
                                {
                                    // Swagger uses this to group endpoints together.
                                    // Group methods together using the service name.
                                    ["controller"] = methodDescriptor.Service.FullName
                                }
                            };
                            apiDescription.RelativePath = pattern.TrimStart('/');
                            apiDescription.SupportedRequestFormats.Add(new ApiRequestFormat {
                                MediaType = "application/json"
                            });
                            apiDescription.SupportedResponseTypes.Add(new ApiResponseType
                            {
                                ApiResponseFormats = { new ApiResponseFormat {
                                                           MediaType = "application/json"
                                                       } },
                                ModelMetadata = new GrpcModelMetadata(ModelMetadataIdentity.ForType(methodDescriptor.OutputType.ClrType)),
                                StatusCode    = 200
                            });

                            var routeParameters = ServiceDescriptorHelpers.ResolveRouteParameterDescriptors(routeEndpoint.RoutePattern, methodDescriptor.InputType);

                            foreach (var routeParameter in routeParameters)
                            {
                                var field = routeParameter.Value.Last();

                                apiDescription.ParameterDescriptions.Add(new ApiParameterDescription
                                {
                                    Name          = routeParameter.Key,
                                    ModelMetadata = new GrpcModelMetadata(ModelMetadataIdentity.ForType(MessageDescriptorHelpers.ResolveFieldType(field))),
                                    Source        = BindingSource.Path,
                                    DefaultValue  = string.Empty
                                });
                            }

                            ServiceDescriptorHelpers.ResolveBodyDescriptor(httpRule.Body, methodDescriptor, out var bodyDescriptor, out _, out _);
                            if (bodyDescriptor != null)
                            {
                                apiDescription.ParameterDescriptions.Add(new ApiParameterDescription
                                {
                                    Name          = "Input",
                                    ModelMetadata = new GrpcModelMetadata(ModelMetadataIdentity.ForType(bodyDescriptor.ClrType)),
                                    Source        = BindingSource.Body
                                });
                            }

                            descriptions.Add(apiDescription);
                        }
                    }
                }
            }

            var groups = new List <ApiDescriptionGroup>();

            groups.Add(new ApiDescriptionGroup("Grpc", descriptions));

            return(new ApiDescriptionGroupCollection(groups, 1));
        }