Skip to content

Commit

Permalink
Use JSON PolymorphismOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
usercode committed Dec 22, 2022
1 parent 1049c04 commit d749436
Show file tree
Hide file tree
Showing 36 changed files with 172 additions and 171 deletions.
4 changes: 4 additions & 0 deletions src/DragonFly.API.Client/DragonFly.API.Client.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
<MinVerTagPrefix>v</MinVerTagPrefix>
</PropertyGroup>

<ItemGroup>
<SupportedPlatform Include="browser" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All" />
</ItemGroup>
Expand Down
28 changes: 28 additions & 0 deletions src/DragonFly.API.Client/DragonFlyBuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright (c) usercode
// https://github.com/usercode/DragonFly
// MIT License

using DragonFly.Builders;
using DragonFly.API.Exports.Json;
using DragonFly.Core.ContentStructures;
using DragonFly.Storage;
using Microsoft.Extensions.DependencyInjection;

namespace DragonFly.Client;

public static class StartupExtensions
{
public static IDragonFlyBuilder AddRestApi(this IDragonFlyBuilder builder)
{
builder.Services.AddTransient<ClientContentService>();
builder.Services.AddTransient<IContentStorage, ClientContentService>();
builder.Services.AddTransient<IStructureStorage, ClientContentService>();
builder.Services.AddTransient<IWebHookStorage, ClientContentService>();
builder.Services.AddTransient<IAssetStorage, ClientContentService>();
builder.Services.AddTransient<IAssetFolderStorage, ClientContentService>();

builder.PostInit<JsonDerivedTypesAction>();

return builder;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace DragonFly.Content;
/// <summary>
/// ContentField
/// </summary>
public class RestContentFields : Dictionary<string, JsonNode>
public class RestContentFields : Dictionary<string, JsonNode?>
{
public RestContentFields()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@ public static ContentItem ToModel(this RestContentItem restContentItem, ContentS
{
RestContentSchema? restSchema = restContentItem.Schema.Deserialize<RestContentSchema>();

if (restSchema != null)
if (restSchema == null)
{
schema = restSchema.ToModel();
throw new Exception("Unknown schema");
}

schema = restSchema.ToModel();
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/DragonFly.API.Exports/DragonFly.API.Exports.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
<MinVerTagPrefix>v</MinVerTagPrefix>
</PropertyGroup>

<ItemGroup>
<SupportedPlatform Include="browser" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All" />
</ItemGroup>
Expand Down

This file was deleted.

This file was deleted.

47 changes: 47 additions & 0 deletions src/DragonFly.API.Exports/Json/JsonDerivedTypesAction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright (c) usercode
// https://github.com/usercode/DragonFly
// MIT License

using System.Text.Json.Serialization.Metadata;
using DragonFly.AspNetCore.API.Exports.Json;
using DragonFly.Query;

namespace DragonFly.API.Exports.Json;

public class JsonDerivedTypesAction : IPostInitialize
{
public async Task ExecuteAsync(IDragonFlyApi api)
{
JsonPolymorphismOptions optionsDerivedTypes = new JsonPolymorphismOptions();

foreach (Type type in api.ContentField().GetAllOptionsTypes())
{
optionsDerivedTypes.DerivedTypes.Add(new JsonDerivedType(type, type.Name));
}

JsonPolymorphismOptions queryDerivedTypes = new JsonPolymorphismOptions();

foreach (Type type in api.ContentField().GetAllQueryTypes())
{
queryDerivedTypes.DerivedTypes.Add(new JsonDerivedType(type, type.Name));
}

JsonSerializerDefault.Options.TypeInfoResolver = new DefaultJsonTypeInfoResolver()
{
Modifiers =
{
(JsonTypeInfo typeInfo) =>
{
if (typeInfo.Type == typeof(ContentFieldOptions))
{
typeInfo.PolymorphismOptions = optionsDerivedTypes;
}
else if (typeInfo.Type == typeof(FieldQuery))
{
typeInfo.PolymorphismOptions = queryDerivedTypes;
}
}
}
};
}
}
7 changes: 0 additions & 7 deletions src/DragonFly.API.Exports/Json/JsonSerializerDefault.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,13 @@
// https://github.com/usercode/DragonFly
// MIT License

using DragonFly.Core.Json;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace DragonFly.AspNetCore.API.Exports.Json;

public class JsonSerializerDefault
{
public JsonSerializerDefault()
{
}

private static JsonSerializerOptions? _options;

public static JsonSerializerOptions Options
Expand All @@ -24,8 +19,6 @@ public static JsonSerializerOptions Options
{
_options = new JsonSerializerOptions();
_options.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull;
_options.Converters.Add(new ArrayOptionJsonConverter());
_options.Converters.Add(new QueryFieldJsonConverter());
}

return _options;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,29 @@
using DragonFly.AspNetCore.API.Middlewares.Assets;
using DragonFly.AspNetCore.API.Middlewares.ContentSchemas;
using DragonFly.Builders;
using Microsoft.Extensions.DependencyInjection;
using DragonFly.AspNetCore.API.Middlewares.ContentStructures;
using DragonFly.AspNetCore.Middleware;
using Microsoft.AspNetCore.Http.Json;
using DragonFly.AspNetCore.API.Exports.Json;
using System.Text.Json.Serialization;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Routing;
using DragonFly.API.Exports.Json;
using Microsoft.AspNetCore.Http.Json;
using Microsoft.Extensions.DependencyInjection;
using System.Text.Json.Serialization;
using DragonFly.AspNetCore.API.Exports.Json;

namespace DragonFly.AspNetCore;

public static class StartupExtensions
public static class DragonFlyBuilderExtensions
{
public static IDragonFlyBuilder AddRestApi(this IDragonFlyBuilder builder)
{
builder.Services.Configure<JsonOptions>(opt =>
{
foreach (JsonConverter converter in JsonSerializerDefault.Options.Converters)
{
opt.SerializerOptions.Converters.Add(converter);
}
opt.SerializerOptions.TypeInfoResolver = JsonSerializerDefault.Options.TypeInfoResolver;
});

builder.PostInit<JsonDerivedTypesAction>();

return builder;
}

Expand Down
4 changes: 4 additions & 0 deletions src/DragonFly.ApiKeys.Razor/DragonFly.ApiKeys.Razor.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
<MinVerTagPrefix>v</MinVerTagPrefix>
</PropertyGroup>

<ItemGroup>
<SupportedPlatform Include="browser" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All" />
</ItemGroup>
Expand Down
3 changes: 2 additions & 1 deletion src/DragonFly.ApiKeys.Razor/DragonFlyBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
using Microsoft.Extensions.DependencyInjection;
using DragonFLy.ApiKeys;
using DragonFly.ApiKeys.Razor.Services;
using DragonFly.ApiKeys.Razor;

namespace DragonFly.ApiKeys.Razor;
namespace DragonFly.Client;

public static class DragonFlyBuilderExtensions
{
Expand Down
6 changes: 5 additions & 1 deletion src/DragonFly.ApiKeys/DragonFly.ApiKeys.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<MinVerTagPrefix>v</MinVerTagPrefix>
</PropertyGroup>


<ItemGroup>
<SupportedPlatform Include="browser" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All" />
</ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/DragonFly.App.Client/DragonFly.App.Client.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<TargetFramework>net7.0</TargetFramework>
<RunAOTCompilation>false</RunAOTCompilation>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="7.0.1" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Authentication" Version="7.0.1" />
Expand Down
11 changes: 4 additions & 7 deletions src/DragonFly.App.Client/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,22 @@
// https://github.com/usercode/DragonFly
// MIT License

using DragonFly.ApiKeys.Razor;
using DragonFly.AspNetCore.Identity.Razor;
using DragonFly.Client.Core;
using DragonFly.BlockField.Razor;
using Microsoft.AspNetCore.Components.Web;
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
using DragonFly.Client;

var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.RootComponents.Add<DragonFly.App.Client.App>("app");
builder.RootComponents.Add<HeadOutlet>("head::after");

builder.AddDragonFlyClient()
.AddRestApi()
.AddBlockField()
.AddIdentity()
.AddApiKeys()
.AddApiKeys()
;

WebAssemblyHost build = builder.Build();

build.UseDragonFlyClient();

await build.UseDragonFlyClient();
await build.RunAsync();
4 changes: 2 additions & 2 deletions src/DragonFly.App.Server/DragonFly.App.Server.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\DragonFly.ApiKeys.AspNetCore\DragonFly.ApiKeys.AspNetCore.csproj" />
<ProjectReference Include="..\DragonFly.API\DragonFly.API.csproj" />
<ProjectReference Include="..\DragonFly.App.Client\DragonFly.App.Client.csproj" />
<ProjectReference Include="..\DragonFly.ApiKeys.AspNetCore\DragonFly.ApiKeys.AspNetCore.csproj" />
<ProjectReference Include="..\DragonFly.API\DragonFly.API.csproj" />
<ProjectReference Include="..\DragonFly.AspNetCore\DragonFly.AspNetCore.csproj" />
<ProjectReference Include="..\DragonFly.BlockField.AspNetCore\DragonFly.BlockField.AspNetCore.csproj" />
<ProjectReference Include="..\DragonFly.Core\DragonFly.Core.csproj" />
Expand Down
1 change: 1 addition & 0 deletions src/DragonFly.App.Server/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,5 @@
app.UseDragonFlyManager();
app.UseSwagger();
app.UseSwaggerUI();
app.UseRouting();
app.Run();
1 change: 0 additions & 1 deletion src/DragonFly.AspNetCore/DragonFly.AspNetCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\DragonFly.API.Client\DragonFly.API.Client.csproj" />
<ProjectReference Include="..\DragonFly.Core\DragonFly.Core.csproj" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
<MinVerTagPrefix>v</MinVerTagPrefix>
</PropertyGroup>

<ItemGroup>
<SupportedPlatform Include="browser" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All" />
</ItemGroup>
Expand Down
Loading

0 comments on commit d749436

Please sign in to comment.