Skip to content

Commit

Permalink
# 8.0.0-beta.2
Browse files Browse the repository at this point in the history
- switched all projects to dotnet 8, netstandard removed
- removed reflection based dDI configuration
- removed obsolete MicroElements.Functional dependency
  • Loading branch information
petriashev committed Dec 13, 2024
1 parent 73772e0 commit 430b2c1
Show file tree
Hide file tree
Showing 26 changed files with 59 additions and 196 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/github_build_and_publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
- name: Setup .NET Core
uses: actions/setup-dotnet@v1
with:
dotnet-version: 6.0.300
dotnet-version: 8.0.404
#-------------------------------
- name: Build and Publish
env:
Expand Down
10 changes: 0 additions & 10 deletions .travis.yml

This file was deleted.

5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# 8.0.0-beta.2
- switched all projects to dotnet 8, netstandard removed
- removed reflection based dDI configuration
- removed obsolete MicroElements.Functional dependency

# 8.0.0-beta.1
- MicroElements.Functional dependency removed
- MicroElements.Diagnostics extracted
Expand Down
16 changes: 0 additions & 16 deletions MicroElements.Metadata.sln
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{3324E7E9-251C-4996-AF33-AAF79DC681A9}"
ProjectSection(SolutionItems) = preProject
.editorconfig = .editorconfig
.travis.yml = .travis.yml
build.sh = build.sh
CHANGELOG.md = CHANGELOG.md
common.props = common.props
Expand Down Expand Up @@ -50,8 +49,6 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MicroElements.Metadata.Expe
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MicroElements.Diagnostics", "src\MicroElements.Diagnostics\MicroElements.Diagnostics.csproj", "{63B52079-9574-4145-B6DC-2ED0B3F1BDF4}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MicroElements.Metadata.Functional", "src\MicroElements.Metadata.Functional\MicroElements.Metadata.Functional.csproj", "{C41680FB-C814-4533-AD9E-011973376B1C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -218,18 +215,6 @@ Global
{63B52079-9574-4145-B6DC-2ED0B3F1BDF4}.Release|x64.Build.0 = Release|Any CPU
{63B52079-9574-4145-B6DC-2ED0B3F1BDF4}.Release|x86.ActiveCfg = Release|Any CPU
{63B52079-9574-4145-B6DC-2ED0B3F1BDF4}.Release|x86.Build.0 = Release|Any CPU
{C41680FB-C814-4533-AD9E-011973376B1C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C41680FB-C814-4533-AD9E-011973376B1C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C41680FB-C814-4533-AD9E-011973376B1C}.Debug|x64.ActiveCfg = Debug|Any CPU
{C41680FB-C814-4533-AD9E-011973376B1C}.Debug|x64.Build.0 = Debug|Any CPU
{C41680FB-C814-4533-AD9E-011973376B1C}.Debug|x86.ActiveCfg = Debug|Any CPU
{C41680FB-C814-4533-AD9E-011973376B1C}.Debug|x86.Build.0 = Debug|Any CPU
{C41680FB-C814-4533-AD9E-011973376B1C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C41680FB-C814-4533-AD9E-011973376B1C}.Release|Any CPU.Build.0 = Release|Any CPU
{C41680FB-C814-4533-AD9E-011973376B1C}.Release|x64.ActiveCfg = Release|Any CPU
{C41680FB-C814-4533-AD9E-011973376B1C}.Release|x64.Build.0 = Release|Any CPU
{C41680FB-C814-4533-AD9E-011973376B1C}.Release|x86.ActiveCfg = Release|Any CPU
{C41680FB-C814-4533-AD9E-011973376B1C}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -248,7 +233,6 @@ Global
{81C38035-F801-4CCB-A5A2-BAD92CBF9730} = {BC47333A-FBE7-4A16-849A-709C6EBF7D40}
{D45D2A84-7725-4DD1-9218-4DD0C0DF56EA} = {39CBC28C-A189-4A8B-A5A0-EB4F89A684CF}
{63B52079-9574-4145-B6DC-2ED0B3F1BDF4} = {39CBC28C-A189-4A8B-A5A0-EB4F89A684CF}
{C41680FB-C814-4533-AD9E-011973376B1C} = {39CBC28C-A189-4A8B-A5A0-EB4F89A684CF}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {58329F1B-0ADB-47A5-8677-7D0C87788051}
Expand Down
2 changes: 1 addition & 1 deletion common.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<Import Project="version.props" />
<PropertyGroup>
<Product>MicroElements.Metadata</Product>
<Copyright>2023</Copyright>
<Copyright>2025</Copyright>
<Authors>alexey.petriashev, MicroElements</Authors>
<Description>Metadata model, parsing, validation and reporting.</Description>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<LangVersion>9.0</LangVersion>
<Nullable>enable</Nullable>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.1.3" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerGen" Version="6.1.3" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="7.2.0" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerGen" Version="7.2.0" />
</ItemGroup>

<ItemGroup>
Expand Down
3 changes: 2 additions & 1 deletion src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Deterministic>true</Deterministic>
<LangVersion>9.0</LangVersion>
<LangVersion>latest</LangVersion>
<Nullable>enable</Nullable>
</PropertyGroup>

<Import Project="stylecop.props"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>MicroElements</RootNamespace>
<LangVersion>9.0</LangVersion>
<Nullable>enable</Nullable>
</PropertyGroup>

<PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using MicroElements.Metadata.Serialization;
using MicroElements.Metadata.Swashbuckle;
using MicroElements.Metadata.SystemTextJson;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Swashbuckle.AspNetCore.SwaggerGen;
Expand Down Expand Up @@ -55,7 +56,7 @@ public static IServiceCollection AddMetadata(
// Register ISerializerDataContractResolver (if not registered already)
services.TryAddTransient<ISerializerDataContractResolver>(provider =>
{
var serializerOptions = provider.GetJsonSerializerOptions() ?? new JsonSerializerOptions();
var serializerOptions = provider.GetJsonSerializerOptionsOrDefault();
return new JsonSerializerDataContractResolver(serializerOptions);
});

Expand Down Expand Up @@ -109,7 +110,7 @@ ServiceDescriptor CreateServiceDescriptor(Func<IServiceProvider, TService> func)
}

/// <summary>
/// AspNetCore MVC JsonOptions.Value wrapper that can be used in netstandard.
/// AspNetCore MVC JsonOptions. Value wrapper that can be used in netstandard.
/// </summary>
public class AspNetJsonSerializerOptions
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,105 +2,33 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.Linq;
using System.Reflection;
using System.Text.Json;
using MicroElements.Reflection;
using MicroElements.Reflection.TypeCaching;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;

namespace MicroElements.Metadata.AspNetCore
{
/// <summary>
/// DependencyInjection through Reflection.
/// DependencyInjection.
/// </summary>
public static class ReflectionDependencyInjectionExtensions
{
private static readonly Lazy<ITypeCache> AppDomainTypeCache = new(() => new TypeCache(AssemblySource.AppDomain.LoadTypes(TypeFilters.AllPublicTypes)));
private static readonly Func<string, Type?> GetByFullName = typeName => AppDomainTypeCache.Value.GetType(typeName);

/// <summary>
/// Calls through reflection: <c>services.Configure&lt;JsonOptions&gt;(options =&gt; configureJson(options));</c>.
/// Can be used from netstandard.
/// </summary>
/// <param name="services">Services.</param>
/// <param name="configureJson">Action to configure <see cref="JsonSerializerOptions"/> in JsonOptions.</param>
public static void ConfigureJsonOptionsForAspNetCore(this IServiceCollection services, Action<JsonSerializerOptions> configureJson)
{
Action<object> configureJsonOptionsUntyped = options =>
{
PropertyInfo? propertyInfo = options.GetType().GetProperty("JsonSerializerOptions");

if (propertyInfo?.GetValue(options) is JsonSerializerOptions jsonSerializerOptions)
{
configureJson(jsonSerializerOptions);
}
};

Type? jsonOptionsType = GetByFullName("Microsoft.AspNetCore.Mvc.JsonOptions");
if (jsonOptionsType != null)
{
Type? extensionsType = GetByFullName("Microsoft.Extensions.DependencyInjection.OptionsServiceCollectionExtensions");

MethodInfo? configureMethodGeneric = extensionsType
?.GetTypeInfo()
.DeclaredMethods
.FirstOrDefault(info => info.Name == "Configure" && info.GetParameters().Length == 2);

MethodInfo? configureMethod = configureMethodGeneric?.MakeGenericMethod(jsonOptionsType);

if (configureMethod != null)
{
// services.Configure<JsonOptions>(options => configureJson(options));
configureMethod.Invoke(services, new object?[] { services, configureJsonOptionsUntyped });
}
}
services.Configure<JsonOptions>(options => configureJson(options.JsonSerializerOptions));
}

/// <summary>
/// Gets <see cref="JsonSerializerOptions"/> from JsonOptions registered in AspNetCore.
/// Uses reflection to call code:
/// <code>serviceProvider.GetService&lt;IOptions&lt;JsonOptions&gt;&gt;()?.Value?.JsonSerializerOptions;</code>
/// </summary>
/// <param name="serviceProvider">Source service provider.</param>
/// <returns>Optional <see cref="JsonSerializerOptions"/>.</returns>
public static JsonSerializerOptions? GetJsonSerializerOptions(this IServiceProvider serviceProvider)
public static void ConfigureJsonOptionsForMinimalApi(this IServiceCollection services, Action<JsonSerializerOptions> configureJson)
{
JsonSerializerOptions? jsonSerializerOptions = null;

Type? jsonOptionsType = GetByFullName("Microsoft.AspNetCore.Mvc.JsonOptions");
if (jsonOptionsType != null)
{
// IOptions<JsonOptions>
Type jsonOptionsInterfaceType = typeof(IOptions<>).MakeGenericType(jsonOptionsType);
object? jsonOptionsOption = serviceProvider.GetService(jsonOptionsInterfaceType);

if (jsonOptionsOption != null)
{
PropertyInfo? valueProperty = jsonOptionsInterfaceType.GetProperty("Value", BindingFlags.Instance | BindingFlags.Public);
PropertyInfo? jsonSerializerOptionsProperty = jsonOptionsType.GetProperty("JsonSerializerOptions", BindingFlags.Instance | BindingFlags.Public);

if (valueProperty != null && jsonSerializerOptionsProperty != null)
{
// JsonOptions
var jsonOptions = valueProperty.GetValue(jsonOptionsOption);

// JsonSerializerOptions
if (jsonOptions != null)
{
jsonSerializerOptions = jsonSerializerOptionsProperty.GetValue(jsonOptions) as JsonSerializerOptions;
}
}
}
}

return jsonSerializerOptions;
services.ConfigureHttpJsonOptions(options => configureJson(options.SerializerOptions));
}

public static JsonSerializerOptions GetJsonSerializerOptionsOrDefault(this IServiceProvider serviceProvider)
{
return serviceProvider.GetJsonSerializerOptions() ?? new JsonSerializerOptions();
return serviceProvider.GetService<IOptions<JsonOptions>>()?.Value.JsonSerializerOptions
?? serviceProvider.GetService<IOptions<Microsoft.AspNetCore.Http.Json.JsonOptions>>()?.Value.SerializerOptions
?? new JsonSerializerOptions();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>MicroElements.Metadata</RootNamespace>
<LangVersion>9.0</LangVersion>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerGen" Version="6.1.3" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerGen" Version="7.2.0" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
using System.Reflection;
using System.Text.Json;
using MicroElements.Metadata.AspNetCore;
using MicroElements.Metadata.ComponentModel;
using MicroElements.Metadata.JsonSchema;
using MicroElements.Metadata.Schema;
using MicroElements.Reflection.TypeExtensions;
using Microsoft.Extensions.Options;
using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen;
Expand All @@ -23,7 +23,7 @@ namespace MicroElements.Metadata.Swashbuckle
/// </summary>
public class PropertyContainerSchemaFilter : ISchemaFilter
{
private readonly PropertyContainerSchemaFilterOptions _options;
private readonly IOptions<PropertyContainerSchemaFilterOptions> _options;
private readonly JsonSerializerOptions _serializerOptions;
private readonly SchemaGeneratorOptions _schemaGeneratorOptions;

Expand All @@ -34,19 +34,13 @@ public class PropertyContainerSchemaFilter : ISchemaFilter
/// <param name="serializerOptions">JsonSerializerOptions.</param>
/// <param name="generatorOptions">Swagger generator options.</param>
public PropertyContainerSchemaFilter(
PropertyContainerSchemaFilterOptions? options,
IOptions<PropertyContainerSchemaFilterOptions>? options,
AspNetJsonSerializerOptions serializerOptions,
SwaggerGenOptions generatorOptions)
{
_options = options?.Clone() ?? new PropertyContainerSchemaFilterOptions();
_options = options ?? new OptionsWrapper<PropertyContainerSchemaFilterOptions>(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;
}

/// <inheritdoc />
Expand All @@ -72,7 +66,7 @@ public void Apply(OpenApiSchema schema, SchemaFilterContext context)
}

// "$ref": "#/components/schemas/KnownSchema"
if (_options.GenerateKnownSchemasAsRefs && propertySet != null)
if (_options.Value.GenerateKnownSchemasAsRefs && propertySet != null)
{
knownSchemaType ??= propertySet.GetType();
string knownSchemaId = _schemaGeneratorOptions.SchemaIdSelector(knownSchemaType);
Expand Down Expand Up @@ -126,7 +120,7 @@ private void FillObjectSchema(OpenApiSchema schema, SchemaFilterContext context,
if (property.GetOrEvaluateNullability() is { } allowNull)
propertySchema.Nullable = allowNull.IsNullAllowed;

if (_options.GenerateKnownSchemasAsRefs && property.GetSchema() is { } separateSchema)
if (_options.Value.GenerateKnownSchemasAsRefs && property.GetSchema() is { } separateSchema)
{
string knownSchemaId = separateSchema.Name;

Expand Down Expand Up @@ -163,7 +157,7 @@ private void FillObjectSchema(OpenApiSchema schema, SchemaFilterContext context,
propertySchema = FillDataSchema(propertySchema, property);
}

string? propertyName = _options.ResolvePropertyName!(property.Name);
string propertyName = _options.Value.ResolvePropertyName!(property.Name);
schema.Properties.Add(propertyName, propertySchema);
}

Expand Down
Loading

0 comments on commit 430b2c1

Please sign in to comment.