Skip to content

Commit fc1bae7

Browse files
authored
Merge pull request #147 from Tynamix/develop
Release 1.5.9
2 parents b28d04b + dbaebaa commit fc1bae7

File tree

7 files changed

+414
-189
lines changed

7 files changed

+414
-189
lines changed

.github/workflows/build-and-test.yml

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# This workflow will build a .NET project
2+
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-net
3+
4+
name: build-and-test
5+
6+
on:
7+
push:
8+
branches: [ "develop" ]
9+
pull_request:
10+
branches: [ "develop", "master" ]
11+
12+
jobs:
13+
build_and_test:
14+
name: Build and Test .NET Libraries
15+
runs-on: windows-latest
16+
17+
strategy:
18+
matrix:
19+
dotnet-version: [8.0.x, 4.8]
20+
21+
steps:
22+
- name: Checkout code
23+
uses: actions/checkout@v3
24+
25+
- name: Setup .NET 8
26+
if: matrix.dotnet-version == '8.0.x'
27+
uses: actions/setup-dotnet@v3
28+
with:
29+
dotnet-version: ${{ matrix.dotnet-version }}
30+
31+
- name: Setup .NET Framework 4.8
32+
if: matrix.dotnet-version == '4.8'
33+
run: echo "Using pre-installed .NET Framework 4.8"
34+
35+
- name: Restore dependencies .NET 8
36+
if: matrix.dotnet-version == '8.0.x'
37+
run: dotnet restore
38+
39+
- name: Restore dependencies .NET 4.8
40+
if: matrix.dotnet-version == '4.8'
41+
run: nuget restore ObjectFillerNET.sln
42+
43+
- name: Build .NET 8
44+
if: matrix.dotnet-version == '8.0.x'
45+
run: dotnet build --configuration Release --no-restore
46+
47+
- name: Build .NET 4.8
48+
if: matrix.dotnet-version == '4.8'
49+
run: |
50+
&"C:\Program Files\Microsoft Visual Studio\2022\Enterprise\MSBuild\Current\bin\MSBuild.exe" ObjectFillerNET.sln /p:Configuration=Release
51+
52+
- name: Test .NET 8
53+
if: matrix.dotnet-version == '8.0.x'
54+
run: dotnet test --configuration Release --no-build --verbosity normal
55+
56+
- name: Test .NET 4.8
57+
if: matrix.dotnet-version == '4.8'
58+
run: |
59+
&"C:\Program Files\Microsoft Visual Studio\2022\Enterprise\Common7\IDE\CommonExtensions\Microsoft\TestWindow\vstest.console.exe" Tynamix.ObjectFiller.Test\bin\Release\net48\Tynamix.ObjectFiller.Test.dll
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# yaml-language-server: $schema=https://json.schemastore.org/github-workflow.json
2+
3+
name: publish-to-nuget
4+
on:
5+
workflow_dispatch: # Allow running the workflow manually from the GitHub UI
6+
push:
7+
branches:
8+
- 'master' # Run the workflow when pushing to the main branch
9+
10+
env:
11+
DOTNET_SKIP_FIRST_TIME_EXPERIENCE: 1
12+
DOTNET_NOLOGO: true
13+
NuGetDirectory: ${{ github.workspace}}/nuget
14+
15+
defaults:
16+
run:
17+
shell: pwsh
18+
19+
jobs:
20+
create_nuget:
21+
runs-on: windows-latest
22+
steps:
23+
- name: checkout
24+
uses: actions/checkout@v3
25+
26+
# Install the .NET SDK indicated in the global.json file
27+
- name: Setup .NET
28+
uses: actions/setup-dotnet@v4
29+
30+
# Create the NuGet package in the folder from the environment variable NuGetDirectory
31+
- run: dotnet pack --configuration Release --output ${{ env.NuGetDirectory }}
32+
33+
# Publish the NuGet package as an artifact, so they can be used in the following jobs
34+
- uses: actions/upload-artifact@v3
35+
with:
36+
name: nuget
37+
if-no-files-found: error
38+
retention-days: 7
39+
path: ${{ env.NuGetDirectory }}/*.nupkg
40+
41+
validate_nuget:
42+
runs-on: windows-latest
43+
needs: [ create_nuget ]
44+
steps:
45+
# Install the .NET SDK indicated in the global.json file
46+
- name: Setup .NET
47+
uses: actions/setup-dotnet@v4
48+
49+
# Download the NuGet package created in the previous job
50+
- uses: actions/download-artifact@v3
51+
with:
52+
name: nuget
53+
path: ${{ env.NuGetDirectory }}
54+
55+
- name: Install nuget validator
56+
run: dotnet tool update Meziantou.Framework.NuGetPackageValidation.Tool --global
57+
58+
# Validate metadata and content of the NuGet package
59+
# https://www.nuget.org/packages/Meziantou.Framework.NuGetPackageValidation.Tool#readme-body-tab
60+
# If some rules are not applicable, you can disable them
61+
# using the --excluded-rules or --excluded-rule-ids option
62+
- name: Validate package
63+
run: meziantou.validate-nuget-package (Get-ChildItem "${{ env.NuGetDirectory }}/*.nupkg")
64+
65+
run_test:
66+
runs-on: windows-latest
67+
steps:
68+
- uses: actions/checkout@v3
69+
- name: Setup .NET
70+
uses: actions/setup-dotnet@v4
71+
- name: Run tests
72+
run: dotnet test --configuration Release
73+
74+
deploy:
75+
runs-on: windows-latest
76+
needs: [ validate_nuget, run_test ]
77+
steps:
78+
# Download the NuGet package created in the previous job
79+
- uses: actions/download-artifact@v3
80+
with:
81+
name: nuget
82+
path: ${{ env.NuGetDirectory }}
83+
84+
# Install the .NET SDK indicated in the global.json file
85+
- name: Setup .NET Core
86+
uses: actions/setup-dotnet@v4
87+
88+
# Publish all NuGet packages to NuGet.org
89+
# Use --skip-duplicate to prevent errors if a package with the same version already exists.
90+
# If you retry a failed workflow, already published packages will be skipped without error.
91+
- name: Publish NuGet package
92+
run: |
93+
foreach($file in (Get-ChildItem "${{ env.NuGetDirectory }}" -Recurse -Include *.nupkg)) {
94+
dotnet nuget push $file --api-key "${{ secrets.NUGET_API_KEY }}" --source https://api.nuget.org/v3/index.json --skip-duplicate
95+
}

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,3 +180,4 @@ ObjectFiller.Test/ObjectFiller.Test.v2.ncrunchproject
180180
.vs/config/applicationhost.config
181181
Output-Build.txt
182182
.vs/
183+
.idea/
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using Microsoft.VisualStudio.TestTools.UnitTesting;
2+
3+
namespace Tynamix.ObjectFiller.Test.BugfixTests
4+
{
5+
internal abstract class EntityBase
6+
{
7+
public string Name { get; set; }
8+
public abstract string AbstractName { get; set; }
9+
}
10+
11+
internal class CustomEntity : EntityBase
12+
{
13+
public override string AbstractName { get; set; }
14+
}
15+
16+
[TestClass]
17+
public class Bug143InheritedAbstractPropertiesAreNotFilled
18+
{
19+
[TestMethod]
20+
public void InheritedAbstractPropertiesShouldBeFilled()
21+
{
22+
Filler<CustomEntity> filler = new Filler<CustomEntity>();
23+
filler
24+
.Setup(true)
25+
.OnProperty(x => x.Name).Use("Alice")
26+
.OnProperty(x => x.AbstractName).Use("John");
27+
var entity = filler.Create();
28+
29+
Assert.IsNotNull(entity);
30+
Assert.AreEqual("Alice", entity.Name);
31+
Assert.AreEqual("John", entity.AbstractName);
32+
}
33+
}
34+
}

Tynamix.ObjectFiller/NetTypeApiExtension.cs

Lines changed: 45 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,25 @@ internal static bool IsAbstract(this Type source)
8888
return source.IsAbstract;
8989
#endif
9090
}
91+
92+
internal static bool IsAbstract(this PropertyInfo source)
93+
{
94+
#if NETSTANDARD
95+
return source.GetMethod.IsAbstract;
96+
#else
97+
var methodInfo = source.GetGetMethod() ?? source.GetSetMethod();
98+
return methodInfo != null && methodInfo.IsAbstract;
99+
#endif
100+
}
101+
102+
internal static Type GetBaseType(this Type source)
103+
{
104+
#if NETSTANDARD
105+
return source.GetTypeInfo().BaseType;
106+
#else
107+
return source.BaseType;
108+
#endif
109+
}
91110

92111
internal static IEnumerable<Type> GetImplementedInterfaces(this Type source)
93112
{
@@ -100,50 +119,52 @@ internal static IEnumerable<Type> GetImplementedInterfaces(this Type source)
100119

101120
internal static IEnumerable<PropertyInfo> GetProperties(this Type source, bool ignoreInheritance)
102121
{
103-
#if NETSTANDARD
104-
105122
if (ignoreInheritance)
106123
{
107-
return source.GetTypeInfo().DeclaredProperties.ToList();
124+
return GetOwnProperties(source);
108125
}
109126

110-
return GetDeclaredPropertyInfosRecursive(new List<PropertyInfo>(), source.GetTypeInfo());
111-
#else
112-
113-
if (ignoreInheritance)
114-
{
115-
return source.GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public);
116-
}
117-
118-
return source.GetProperties();
119-
#endif
120-
127+
return GetPropertiesRecursively(source, new List<PropertyInfo>());
121128
}
122129

130+
private static PropertyInfo[] GetOwnProperties(Type source)
131+
{
123132
#if NETSTANDARD
124-
125-
internal static List<PropertyInfo> GetDeclaredPropertyInfosRecursive(List<PropertyInfo> propertyInfos, TypeInfo typeInfo)
133+
return source.GetTypeInfo().DeclaredProperties.ToArray();
134+
#else
135+
return source.GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public);
136+
#endif
137+
}
138+
139+
private static IEnumerable<PropertyInfo> GetPropertiesRecursively(Type source, List<PropertyInfo> propertyInfos)
126140
{
127-
foreach (var property in typeInfo.DeclaredProperties)
141+
foreach (var property in GetOwnProperties(source))
128142
{
129-
if (!propertyInfos.Any(x => x.Name == property.Name))
143+
var existingProperty = propertyInfos.FirstOrDefault(p => p.Name == property.Name);
144+
if (existingProperty != null)
145+
{
146+
if (IsAbstract(property))
147+
{
148+
// abstract properties take precedence over their concrete declaration counterpart
149+
propertyInfos.Remove(existingProperty);
150+
propertyInfos.Add(property);
151+
}
152+
}
153+
else
130154
{
131155
propertyInfos.Add(property);
132156
}
133157
}
134158

135-
if(typeInfo.BaseType != null)
159+
Type baseType = GetBaseType(source);
160+
if(baseType != null)
136161
{
137-
return GetDeclaredPropertyInfosRecursive(propertyInfos, typeInfo.BaseType.GetTypeInfo());
162+
return GetPropertiesRecursively(baseType, propertyInfos);
138163
}
139164

140165
return propertyInfos;
141166
}
142167

143-
#endif
144-
145-
146-
147168
internal static Type[] GetGenericTypeArguments(this Type source)
148169
{
149170
#if NETSTANDARD

0 commit comments

Comments
 (0)