/// <summary>
        /// Configures swagger to properly generate info for <see cref="IPropertyContainer"/>.
        /// By default uses Microsoft.AspNetCore.Mvc.JsonOptions registered in AspNetCore but allows to configure manually.
        /// </summary>
        /// <param name="services">Source services.</param>
        /// <param name="configure">Allows to configure options.</param>
        public static void ConfigureSwaggerForPropertyContainers(
            this IServiceCollection services,
            Action <PropertyContainerSchemaFilterOptions>?configure = null)
        {
            if (configure != null)
            {
                services.Configure(configure);

                services.TryAddTransient(provider =>
                {
                    var options = provider.GetService <IOptions <PropertyContainerSchemaFilterOptions> >();
                    return(options?.Value ?? new PropertyContainerSchemaFilterOptions());
                });
            }
            else
            {
                services.TryAddTransient(provider =>
                {
                    JsonSerializerOptions?jsonSerializerOptions = provider.GetJsonSerializerOptions();

                    // Configure json serializer
                    jsonSerializerOptions?.ConfigureJsonForPropertyContainers();

                    var schemaFilterOptions = new PropertyContainerSchemaFilterOptions();
                    schemaFilterOptions.ConfigureFromJsonOptions(jsonSerializerOptions);

                    return(schemaFilterOptions);
                });
            }

            // Configure swagger
            services.ConfigureSwaggerGen(swagger => swagger.ConfigureForPropertyContainers());
        }
 /// <summary>
 /// Configures <see cref="PropertyContainerSchemaFilterOptions"/> from <see cref="JsonSerializerOptions"/>.
 /// </summary>
 /// <param name="options">Options to configure.</param>
 /// <param name="jsonOptions">JsonSerializerOptions.</param>
 /// <returns>The same options.</returns>
 public static PropertyContainerSchemaFilterOptions ConfigureFromJsonOptions(
     this PropertyContainerSchemaFilterOptions options,
     JsonSerializerOptions?jsonOptions)
 {
     options.ResolvePropertyName = jsonOptions.ResolvePropertyNameByJsonSerializerOptions;
     return(options);
 }
예제 #3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="PropertyContainerSchemaFilter"/> class.
        /// </summary>
        /// <param name="options">Options.</param>
        /// <param name="serializerOptions">JsonSerializerOptions.</param>
        /// <param name="generatorOptions">Swagger generator options.</param>
        public PropertyContainerSchemaFilter(
            PropertyContainerSchemaFilterOptions?options,
            IOptions <JsonSerializerOptions> serializerOptions,
            SwaggerGenOptions generatorOptions)
        {
            _options                = options?.Clone() ?? new PropertyContainerSchemaFilterOptions();
            _serializerOptions      = serializerOptions.Value;
            _schemaGeneratorOptions = generatorOptions.SchemaGeneratorOptions;

            Func <string, string>?resolvePropertyName = options?.ResolvePropertyName;

            resolvePropertyName ??= propertyName => _serializerOptions.PropertyNamingPolicy.ConvertName(propertyName);
            resolvePropertyName ??= propertyName => propertyName;

            _options.ResolvePropertyName = resolvePropertyName;
        }