Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug / Lazy Error #149

Merged
merged 4 commits into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
**/.dockerignore
**/.env
**/.git
**/.gitignore
**/.github
**/.vs
**/.vscode
**/.project
**/*.*proj.user
**/.settings
**/.toolstarget
**/Dockerfile*
**/bin
**/obj
**/*.swp
**/*.md
docs/**/*
research/**/*
samples/**/*
8 changes: 8 additions & 0 deletions docs/features/authorization.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,11 @@ and this handler directly throws;
- `UnauthorizedAccessExcetpin` when forbidden

instead of using `IAuthenticationHandler` fallback methods.

## Disabled

You can disable this feature by calling `Disabled()` method;

```csharp
c => c.Disabled()
```
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
using Baked.Architecture;
namespace Baked.Authentication;

namespace Baked.Authentication;

public class AuthenticationConfigurator
{
public IFeature<AuthenticationConfigurator> Disabled() =>
Feature.Empty<AuthenticationConfigurator>();
}
public class AuthenticationConfigurator { }
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Baked.Architecture;
using Baked.RestApi;
using Baked.RestApi.Conventions;
using FluentNHibernate.Conventions.Helpers;
using FluentNHibernate.Mapping;
Expand All @@ -8,6 +9,7 @@
using Microsoft.Extensions.Logging;
using NHibernate;
using NHibernate.Exceptions;
using NHibernate.Proxy;

namespace Baked.Orm.AutoMap;

Expand Down Expand Up @@ -79,6 +81,13 @@ public void Configure(LayerConfigurator configurator)
});
});

configurator.ConfigureMvcNewtonsoftJsonOptions(options =>
{
if (options.SerializerSettings.ContractResolver is null) { return; }

options.SerializerSettings.ContractResolver = new ProxyAwareContractResolver<INHibernateProxy>(options.SerializerSettings.ContractResolver);
});

configurator.ConfigureApiModelConventions(conventions =>
{
var domainModel = configurator.Context.GetDomainModel();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using Newtonsoft.Json.Serialization;

namespace Baked.RestApi;

public class ProxyAwareContractResolver<TProxyInterface>(IContractResolver _real)
: IContractResolver
{
public JsonContract ResolveContract(Type type)
{
if (type.IsAssignableTo(typeof(TProxyInterface)))
{
type = type.BaseType ?? throw new($"Proxy type {type.FullName} should have a base type!!");
}

return _real.ResolveContract(type);
}
}
5 changes: 5 additions & 0 deletions test/recipe/Baked.Test.Recipe.Service.Application/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# syntax=docker/dockerfile:1.7-labs

FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base

ARG ENVIRONMENT=Production
Expand All @@ -8,6 +10,9 @@ ENV ASPNETCORE_URLS=http://+:80

FROM mcr.microsoft.com/dotnet/sdk:8.0 AS publish

COPY --exclude=**/*.cs --exclude=**/*.json . .
RUN dotnet restore

COPY . .
RUN dotnet publish ./test/recipe/Baked.Test.Recipe.Service.Application -c Release -o /app/publish

Expand Down
21 changes: 21 additions & 0 deletions test/recipe/Baked.Test.Recipe.Service.Test/Orm/LazyLoading.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using Newtonsoft.Json;
using System.Net;
using System.Net.Http.Json;

namespace Baked.Test.Orm;

public class LazyLoading : TestServiceNfr
{
[Test]
public async Task Proxy_classes_serialized_correctly()
{
var parentResponse = await Client.PostAsync("/parents", JsonContent.Create(new { name = "test" }));
dynamic? parent = JsonConvert.DeserializeObject(await parentResponse.Content.ReadAsStringAsync());

await Client.PostAsync($"/parents/{parent?.id}/children", JsonContent.Create(new { }));

var response = await Client.GetAsync($"/children");

response.StatusCode.ShouldBe(HttpStatusCode.OK);
}
}
3 changes: 3 additions & 0 deletions test/recipe/Baked.Test.Recipe.Service/Orm/Child.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,7 @@ public class Children(IQueryContext<Child> _context)
{
internal List<Child> ByParent(Parent parent) =>
_context.By(e => e.Parent == parent);

public List<Child> By() =>
_context.By(c => true);
}
5 changes: 5 additions & 0 deletions unreleased.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,8 @@
- Root namespace is `Baked`
- `Forge` is now `Bake`
- `Bluprints` are now `Recipes`
- `Authentication.Disabled` was removed

## Bugfixes

- NHibernate proxies were causing serialization error, fixed