/// <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());
        }
コード例 #2
0
        public static JsonSerializerOptions ConfigureJsonOptions(this JsonSerializerOptions options, Action <MetadataJsonSerializationOptions>?configureSerialization = null)
        {
            options.ConfigureJsonForPropertyContainers(configureSerialization);
            options.ConfigureForNodaTime(DateTimeZoneProviders.Tzdb);

            options.Encoder       = JavaScriptEncoder.UnsafeRelaxedJsonEscaping;
            options.WriteIndented = true;

            return(options);
        }
コード例 #3
0
 /// <summary>
 /// Alias for <see cref="ConfigureJsonForPropertyContainers"/>.
 /// Configures <see cref="JsonSerializerOptions"/> to serialize <see cref="IPropertyContainer"/>.
 /// </summary>
 /// <param name="options"><see cref="JsonSerializerOptions"/> instance.</param>
 /// <param name="configureMetadataJson">Configure Metadata Json serialization.</param>
 /// <returns>The same options.</returns>
 public static JsonSerializerOptions ConfigureForMetadata(
     this JsonSerializerOptions options,
     Action <MetadataJsonSerializationOptions>?configureMetadataJson = null)
 {
     return(options.ConfigureJsonForPropertyContainers(configureMetadataJson));
 }