Skip to content

When obtaining RuleOptions in Middleware, the ServiceNamespace in the configuration items will be lost after using the ExpandConfig and GroupByPaths methods #328

@zhangmeng1993

Description

@zhangmeng1993

Describe the bug
When obtaining RuleOptions in Middleware, the ServiceNamespace in the configuration items will be lost after using the ExpandConfig and GroupByPaths methods.

This results in the inability to obtain the Service correctly.

            IEnumerable<RouteOptions> routeOptions = _routes.CurrentValue
                .ExpandConfig(endPoint)
                .GroupByPaths();

            RouteOptions route = routeOptions.FirstOrDefault(r => r.SwaggerKey == endPoint.Key);
        /// <summary>
        /// Groups the re routes by paths.
        /// </summary>
        /// <param name="routeOptions">The re route options.</param>
        public static IEnumerable<RouteOptions> GroupByPaths(this IEnumerable<RouteOptions> routeOptions)
            => routeOptions
            .GroupBy(p => new { p.SwaggerKey, p.UpstreamPathTemplate, p.DownstreamPathTemplate, p.VirtualDirectory })
            .Select(p =>
            {
                RouteOptions route = p.First();
                return new RouteOptions(
                    p.Key.SwaggerKey,
                    route.UpstreamPathTemplate,
                    route.DownstreamPathTemplate,
                    p.Key.VirtualDirectory,
                    route.DangerousAcceptAnyServerCertificateValidator,
                    p.Where(r => r.UpstreamHttpMethod is not null).SelectMany(r => r.UpstreamHttpMethod))
                {
                    DownstreamHttpVersion = route.DownstreamHttpVersion,
                    DownstreamScheme = route.DownstreamScheme,
                    AuthenticationOptions = route.AuthenticationOptions,
                };
            });

        /// <summary>
        /// Expands versions from one endpoint to more Route options.
        /// </summary>
        /// <param name="routes">The re routes.</param>
        /// <param name="endPoint">The end point.</param>
        public static IEnumerable<RouteOptions> ExpandConfig(
            this IEnumerable<RouteOptions> routes,
            SwaggerEndPointOptions endPoint)
        {
            var routeOptions = routes.Where(p => p.SwaggerKey == endPoint.Key).ToList();

            if (string.IsNullOrWhiteSpace(endPoint.VersionPlaceholder))
            {
                return routeOptions;
            }

            var versionRouteOptions = routeOptions.Where(x =>
                x.DownstreamPathTemplate.Contains(endPoint.VersionPlaceholder)
                || x.UpstreamPathTemplate.Contains(endPoint.VersionPlaceholder)).ToList();
            versionRouteOptions.ForEach(o => routeOptions.Remove(o));
            foreach (RouteOptions routeOption in versionRouteOptions)
            {
                IEnumerable<RouteOptions> versionMappedRouteOptions = endPoint.Config.Select(c => new RouteOptions()
                {
                    SwaggerKey = routeOption.SwaggerKey,
                    DownstreamPathTemplate =
                        routeOption.DownstreamPathTemplate.Replace(endPoint.VersionPlaceholder,
                            c.Version),
                    UpstreamHttpMethod = routeOption.UpstreamHttpMethod,
                    UpstreamPathTemplate =
                        routeOption.UpstreamPathTemplate.Replace(endPoint.VersionPlaceholder,
                            c.Version),
                    VirtualDirectory = routeOption.VirtualDirectory,
                    DownstreamHttpVersion = routeOption.DownstreamHttpVersion,
                    DownstreamScheme = routeOption.DownstreamScheme,
                    AuthenticationOptions = routeOption.AuthenticationOptions,
                    DangerousAcceptAnyServerCertificateValidator = routeOption.DangerousAcceptAnyServerCertificateValidator
                });
                routeOptions.AddRange(versionMappedRouteOptions);
            }

            return routeOptions;
        }

demo.json

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions