Exemplo n.º 1
0
        public object Get(SwaggerResource request)
        {
            var path  = "/" + request.Name;
            var map   = HostContext.ServiceController.RestPathMap;
            var paths = new List <RestPath>();

            var basePath = base.Request.GetBaseUrl();

            var meta = HostContext.Metadata;

            foreach (var key in map.Keys)
            {
                var restPaths     = map[key];
                var selectedPaths = restPaths.Where(x => x.Path == path || x.Path.StartsWith(path + "/"));
                var visiblePaths  = selectedPaths.Where(x => meta.IsVisible(Request, Format.Json, x.RequestType.Name));
                paths.AddRange(visiblePaths);
            }

            var models = new Dictionary <string, SwaggerModel>();

            foreach (var restPath in paths.SelectMany(x => x.Verbs.Select(y => new { Value = x, Verb = y })))
            {
                ParseModel(models, restPath.Value.RequestType, restPath.Value.Path, restPath.Verb);
            }

            var apis = paths.Select(p => FormatMethodDescription(p, models))
                       .ToArray().OrderBy(md => md.Path).ToList();

            var result = new SwaggerApiDeclaration
            {
                ApiVersion   = HostContext.Config.ApiVersion,
                ResourcePath = path,
                BasePath     = basePath,
                Apis         = apis,
                Models       = models
            };

            if (OperationFilter != null)
            {
                apis.Each(x => x.Operations.Each(OperationFilter));
            }

            ApiDeclarationFilter?.Invoke(result);

            return(new HttpResult(result)
            {
                ResultScope = () => JsConfig.With(new Config {
                    IncludeNullValues = false
                })
            });
        }
Exemplo n.º 2
0
        public object Get(OpenApiSpecification request)
        {
            var map   = HostContext.ServiceController.RestPathMap;
            var paths = new List <RestPath>();

            var basePath = new Uri(base.Request.GetBaseUrl());

            var meta = HostContext.Metadata;

            foreach (var key in map.Keys)
            {
                var restPaths    = map[key];
                var visiblePaths = restPaths.Where(x => meta.IsVisible(Request, Format.Json, x.RequestType.Name));
                paths.AddRange(visiblePaths);
            }

            var definitions = new Dictionary <string, OpenApiSchema>()
            {
                { "Object", new OpenApiSchema()
                  {
                      Description = "Object", Type = OpenApiType.Object, Properties = new OrderedDictionary <string, OpenApiProperty>()
                  } }
            };

            foreach (var restPath in paths.SelectMany(x => x.Verbs.Select(y => new { Value = x, Verb = y })))
            {
                ParseDefinitions(definitions, restPath.Value.RequestType, restPath.Value.Path, restPath.Verb);
            }

            var tags     = new Dictionary <string, OpenApiTag>();
            var apiPaths = ParseOperations(paths, definitions, tags);

            var result = new OpenApiDeclaration
            {
                Info = new OpenApiInfo
                {
                    Title   = HostContext.ServiceName,
                    Version = HostContext.Config.ApiVersion,
                },
                Paths    = apiPaths,
                BasePath = basePath.AbsolutePath,
                Schemes  = new List <string> {
                    basePath.Scheme
                },                                              //TODO: get https from config
                Host     = basePath.Authority,
                Consumes = new List <string> {
                    "application/json"
                },
                Produces = new List <string> {
                    "application/json"
                },
                Definitions = definitions,
                Tags        = tags.Values.OrderBy(x => x.Name).ToList(),
                Parameters  = new Dictionary <string, OpenApiParameter> {
                    { "Accept", GetAcceptHeaderParameter() }
                },
                SecurityDefinitions = new Dictionary <string, OpenApiSecuritySchema> {
                    { "basic", new OpenApiSecuritySchema {
                          Type = "basic"
                      } }
                }
            };


            if (OperationFilter != null)
            {
                apiPaths.Each(x => GetOperations(x.Value).Each(o => OperationFilter(o.Item1, o.Item2)));
            }

            ApiDeclarationFilter?.Invoke(result);

            return(new HttpResult(result)
            {
                ResultScope = () => JsConfig.With(includeNullValues: false)
            });
        }