Skip to content

Commit e126d92

Browse files
dkattanCopilot
andcommitted
feat: add RenameProperties to GenerateDtosAttribute
Allows renaming entity properties in generated DTOs via 'EntityProp:DtoProp' pairs. The generated property uses the DTO name but maps to the entity name in constructors and projections. Not yet working in SGF hoist pipeline — needs further investigation into why the renameMap isn't applied during member creation. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 6988631 commit e126d92

2 files changed

Lines changed: 44 additions & 8 deletions

File tree

‎src/Facet.Attributes/GenerateDtosAttribute.cs‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,19 @@ public class GenerateDtosAttribute : Attribute
186186
/// to avoid collisions. Default is false (shorter file names).
187187
/// </summary>
188188
public bool UseFullName { get; set; } = false;
189+
190+
/// <summary>
191+
/// Pairs of "EntityPropertyName:DtoPropertyName" that rename entity properties in the
192+
/// generated DTO. The generated property uses <c>DtoPropertyName</c> as its name but
193+
/// maps to <c>EntityPropertyName</c> in constructors and projections. This avoids
194+
/// needing a hand-written partial class just to rename a few properties.
195+
/// <para>
196+
/// Example: <c>RenameProperties = new[] { "ActionReason:Reason", "ActionResult:Result" }</c>
197+
/// generates <c>public MaintenanceActionReason Reason { get; set; }</c> mapped from
198+
/// <c>source.ActionReason</c>.
199+
/// </para>
200+
/// </summary>
201+
public string[] RenameProperties { get; set; } = Array.Empty<string>();
189202
}
190203

191204
/// <summary>

‎src/Facet/Generators/FacetGenerators/GenerateDtosGenerator.cs‎

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,24 @@ bool NeedsPatchWireSupport(GenerateDtosTargetModel m) =>
479479

480480
var excludeProperties = new HashSet<string>(userExcludeProperties, System.StringComparer.OrdinalIgnoreCase);
481481

482+
// Parse RenameProperties: "EntityProp:DtoProp" pairs that rename entity properties
483+
// in the generated DTO while keeping the source mapping intact.
484+
var renameMap = new Dictionary<string, string>(StringComparer.Ordinal);
485+
var renameArg = attribute.NamedArguments.FirstOrDefault(kvp => kvp.Key == "RenameProperties");
486+
if (renameArg.Value.Kind == TypedConstantKind.Array && !renameArg.Value.IsNull)
487+
{
488+
foreach (var v in renameArg.Value.Values)
489+
{
490+
var pair = v.Value?.ToString();
491+
if (pair is not null && pair.Contains(':'))
492+
{
493+
var parts = pair.Split(new[] { ':' }, 2);
494+
if (parts.Length == 2 && !string.IsNullOrWhiteSpace(parts[0]) && !string.IsNullOrWhiteSpace(parts[1]))
495+
renameMap[parts[0].Trim()] = parts[1].Trim();
496+
}
497+
}
498+
}
499+
482500
if (excludeAuditFields)
483501
{
484502
foreach (var field in DefaultAuditFields)
@@ -517,28 +535,32 @@ bool NeedsPatchWireSupport(GenerateDtosTargetModel m) =>
517535
settableProperties.Add(p.Name);
518536
}
519537

538+
var dtoName = renameMap.TryGetValue(p.Name, out var renamed) ? renamed : p.Name;
520539
members.Add(CreateGenerateDtoMember(
521-
p.Name,
540+
dtoName,
522541
p.Type,
523542
FacetMemberKind.Property,
524543
isInitOnly,
525544
isRequired,
526545
false,
527-
convertEnumsTo));
528-
addedMembers.Add(p.Name);
546+
convertEnumsTo,
547+
sourcePropertyName: p.Name));
548+
addedMembers.Add(dtoName);
529549
}
530550
else if (includeFields && member is IFieldSymbol { DeclaredAccessibility: Accessibility.Public } f)
531551
{
532552
bool isReadOnly = f.IsReadOnly;
553+
var dtoName = renameMap.TryGetValue(f.Name, out var renamed) ? renamed : f.Name;
533554
members.Add(CreateGenerateDtoMember(
534-
f.Name,
555+
dtoName,
535556
f.Type,
536557
FacetMemberKind.Field,
537558
false,
538559
isRequired,
539560
isReadOnly,
540-
convertEnumsTo));
541-
addedMembers.Add(f.Name);
561+
convertEnumsTo,
562+
sourcePropertyName: f.Name));
563+
addedMembers.Add(dtoName);
542564
}
543565
}
544566

@@ -1188,7 +1210,8 @@ private static FacetMember CreateGenerateDtoMember(
11881210
bool isInitOnly,
11891211
bool isRequired,
11901212
bool isReadOnly,
1191-
string? convertEnumsTo)
1213+
string? convertEnumsTo,
1214+
string? sourcePropertyName = null)
11921215
{
11931216
var isCollection = GeneratorUtilities.TryGetCollectionElementType(typeSymbol, out var elementType, out var collectionWrapper);
11941217
var originalTypeName = GeneratorUtilities.GetTypeNameWithNullability(typeSymbol);
@@ -1260,7 +1283,7 @@ private static FacetMember CreateGenerateDtoMember(
12601283
null,
12611284
false,
12621285
true,
1263-
name,
1286+
sourcePropertyName ?? name,
12641287
false,
12651288
null,
12661289
null,

0 commit comments

Comments
 (0)