Esempio n. 1
0
        private static Info CreateInfoForApiVersion(ApiVersionDescription description, EnvironmentConfig env)
        {
            var info = new Info
            {
                Title       = $"Plugin Sisense {description.ApiVersion}",
                Version     = description.ApiVersion.ToString(),
                Description = $"API surface for Sisense Plugin. Application version is {env.Version}.",
                Contact     = new Contact {
                    Name = "Naveego", Email = "*****@*****.**"
                },
            };

            if (description.IsDeprecated)
            {
                info.Description += " This API version has been deprecated.";
            }

            return(info);
        }
Esempio n. 2
0
        public static void ConfigureSwaggerServices(this IServiceCollection services, IConfiguration configuration)
        {
            services.AddSwaggerGen(options =>
            {
                var env = new EnvironmentConfig();

                options.SchemaFilter <AutoRestSchemaFilter>();

                options.OperationFilter <OperationNamingFilter>();
                options.OperationFilter <VersioningFilter>();
                options.OperationFilter <RequiredBodyFilter>();

                options.MapType <JObject>(() => new Schema()
                {
                    Type = "object",
                    AdditionalProperties = new Schema()
                    {
                    }
                });

                options.CustomSchemaIds(type =>
                {
                    if (type.IsGenericType)
                    {
                        var wrapperName    = type.Name.Split("`").First();
                        var parameterNames = type.GetGenericArguments().Select(x => x.Name).ToList();
                        return($"{wrapperName}Of{string.Join("And", parameterNames)}");
                    }

                    // TODO: Uncomment when ready to support polymorphism and integrate with dataflow-contracts
                    // options.SchemaFilter<PolymorphismSchemaFilter>();
//                    var resourceName = type
//                        .GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy)
//                        .Where(fi => fi.Name == "ResourceName")
//                        .Select(fi => (string) fi.GetRawConstantValue())
//                        .FirstOrDefault();
//                    if (resourceName != null)
//                    {
//                        return resourceName;
//                    }

                    return(Regex.Replace(type.Name, "Model$", ""));
                });

                options.AddSecurityDefinition("api", new ApiKeyScheme()
                {
                    In   = "header",
                    Name = "Authorization"
                });

                options.AddSecurityRequirement(new Dictionary <string, IEnumerable <string> >()
                {
                    { "api", new string[0] }
                });

                // resolve the IApiVersionDescriptionProvider service
                // note: that we have to build a temporary service provider here because one has not been created yet
                var provider = services.BuildServiceProvider().GetRequiredService <IApiVersionDescriptionProvider>();

                // add a swagger document for each discovered API version
                // note: you might choose to skip or document deprecated API versions differently
                foreach (var description in provider.ApiVersionDescriptions)
                {
                    options.SwaggerDoc(description.GroupName, CreateInfoForApiVersion(description, env));
                }


                /* Disabled because it breaks dotnet watch */
//                var xmlCommentsBasePath = ApplicationEnvironment.ApplicationBasePath;
//                var xmlCommentsFilePath = typeof( Startup ).GetTypeInfo().Assembly.GetName().Name + ".xml";
//                options.IncludeXmlComments(Path.Combine( xmlCommentsBasePath, xmlCommentsFilePath));
            });
        }