Skip to content

Commit 4891c67

Browse files
committed
Correctly locate syntax for method
1 parent a3ccdd5 commit 4891c67

File tree

3 files changed

+36
-17
lines changed

3 files changed

+36
-17
lines changed

src/Mappit.Generator/Mappit.Generator.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
<PackageReadmeFile>README.md</PackageReadmeFile>
1717
<PackageLicenseFile>LICENSE</PackageLicenseFile>
1818
<IsPackable>true</IsPackable>
19-
<Version>0.0.6</Version>
19+
<Version>0.0.7</Version>
2020
<WarningsNotAsErrors>NU5128</WarningsNotAsErrors>
2121
</PropertyGroup>
2222

src/Mappit.Generator/MappitGenerator.TypeInfoBuilding.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,10 @@ public partial class MappitGenerator : IIncrementalGenerator
3939
methodSymbol.ReturnType is ITypeSymbol returnType)
4040
{
4141
// Find the method declaration in the syntax tree
42-
var methodDeclaration = classDeclarationSyntax.DescendantNodes()
42+
var methodDeclaration = methodSymbol.DeclaringSyntaxReferences
43+
.Select(r => r.GetSyntax())
4344
.OfType<MethodDeclarationSyntax>()
44-
.FirstOrDefault(m => m.Identifier.Text == methodSymbol.Name);
45+
.FirstOrDefault();
4546

4647
if (methodDeclaration == null)
4748
{

test/Mappit.Tests/MappingGenerationVerification/CustomMappingTests.cs

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,9 @@ public void Map_WithCustomEnumMapping_ShouldMapAllEnumValues()
7575
public void Map_WithUserDefinedMappingMethod_ShouldMapCorrectly()
7676
{
7777
// Arrange
78-
var source = new WeirdModel { Name = "Weird" };
78+
var source = new CustomMapping { Name = "Weird" };
7979
// Act
80-
var target = _mapper.MapWeirdModel(source);
80+
var target = _mapper.MapCustom(source);
8181
// Assert
8282
Assert.Equal("drieW", target.Name);
8383
}
@@ -86,7 +86,7 @@ public void Map_WithUserDefinedMappingMethod_ShouldMapCorrectly()
8686
public void Map_WithNestedModelRequiringUserDefinedMapping_ShouldMapCorrectly()
8787
{
8888
// Arrange
89-
var source = new WeirdModelContainer(1, new WeirdModel { Name = "Weird" });
89+
var source = new CustomMappingContainer(1, new CustomMapping { Name = "Weird" });
9090
// Act
9191
var target = _mapper.Map(source);
9292
// Assert
@@ -100,7 +100,7 @@ public void Map_WithStaticCustomValueConversion_ShouldMapCorrectly()
100100
var mapper = new CustomPropertyConversionMapper();
101101

102102
// Arrange
103-
var source = new WeirdModel { Name = "Weird" };
103+
var source = new CustomMapping { Name = "Weird" };
104104
// Act
105105
var target = mapper.Map(source);
106106
// Assert
@@ -113,12 +113,25 @@ public void Map_WithInstanceCustomValueConversion_ShouldMapCorrectly()
113113
var mapper = new CustomPropertyConversionMapper();
114114

115115
// Arrange
116-
var source = new WeirdModel { Name = "Weird" };
116+
var source = new CustomMapping { Name = "Weird" };
117117
// Act
118118
var target = mapper.MapWithUpperCase(source);
119119
// Assert
120120
Assert.Equal("WEIRD", target.Name);
121121
}
122+
123+
[Fact]
124+
public void Map_WithInstanceCustomValueConversionToPropertyInitialization_ShouldMapCorrectly()
125+
{
126+
var mapper = new CustomPropertyConversionMapper();
127+
128+
// Arrange
129+
var source = new CustomMappingMapped("Weird");
130+
// Act
131+
var target = mapper.Map(source);
132+
// Assert
133+
Assert.Equal("WEIRD", target.Name);
134+
}
122135
}
123136

124137
// Test enum types with different values but same meaning
@@ -154,16 +167,16 @@ public class TargetModel
154167
public DateTime DateCreated { get; set; }
155168
}
156169

157-
public class WeirdModel
170+
public class CustomMapping
158171
{
159172
public required string Name { get; set; }
160173
}
161174

162-
public record WeirdModelMapped(string Name);
175+
public record CustomMappingMapped(string Name);
163176

164-
public record WeirdModelContainer(int Id, WeirdModel WeirdModel);
177+
public record CustomMappingContainer(int Id, CustomMapping WeirdModel);
165178

166-
public record WeirdModelContainerMapped(int Id, WeirdModelMapped WeirdModel);
179+
public record CustomMappingContainerMapped(int Id, CustomMappingMapped WeirdModel);
167180

168181
[Mappit]
169182
public partial class CustomMappingTestMapper
@@ -178,30 +191,35 @@ public partial class CustomMappingTestMapper
178191
[MapEnumValue(nameof(SourceStatus.Pending), nameof(TargetStatus.AwaitingConfirmation))]
179192
public partial TargetStatus MapSourceStatus(SourceStatus source);
180193

181-
public partial WeirdModelContainerMapped Map(WeirdModelContainer source);
194+
public partial CustomMappingContainerMapped Map(CustomMappingContainer source);
182195

183196
// Custom implementation for some bespoke weird mapping - in this case we're
184197
// reversing the string on one of the properties.
185-
public WeirdModelMapped? MapWeirdModel(WeirdModel? source)
198+
public CustomMappingMapped? MapCustom(CustomMapping? source)
186199
{
187200
if (source is null)
188201
{
189202
return default;
190203
}
191204

192-
return new WeirdModelMapped(new string(source.Name.Reverse().ToArray()));
205+
return new CustomMappingMapped(new string(source.Name.Reverse().ToArray()));
193206
}
194207
}
195208

196209
[Mappit]
197210
public partial class CustomPropertyConversionMapper
198211
{
199212
[MapProperty("Name", "Name", ValueConversionMethod = nameof(ReverseText))]
200-
public partial WeirdModelMapped Map(WeirdModel source);
213+
public partial CustomMappingMapped Map(CustomMapping source);
201214

202215
// Verify we don't *have* to pass the target property name if it's the same as the source
203216
[MapProperty("Name", ValueConversionMethod = nameof(UpperCase))]
204-
public partial WeirdModelMapped MapWithUpperCase(WeirdModel source);
217+
public partial CustomMappingMapped MapWithUpperCase(CustomMapping source);
218+
219+
// Use the same name as a previous map method to ensure that we are locating the correct
220+
// method in the class for picking up attributes
221+
[MapProperty("Name", ValueConversionMethod = nameof(UpperCase))]
222+
public partial CustomMapping Map(CustomMappingMapped source);
205223

206224
private static string ReverseText(string value) =>
207225
string.IsNullOrEmpty(value) ? value : new string(value.Reverse().ToArray());

0 commit comments

Comments
 (0)