diff --git a/CHANGELOG.md b/CHANGELOG.md index 9f0dc15c82..995aedcedc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,7 @@ * None ### Fixed -* None +* Improved the warning message when adding Realm attributes on a non-persisted property. (Issue [#3352](https://github.com/realm/realm-dotnet/issues/3352)) ### Compatibility * Realm Studio: 13.0.0 or later. diff --git a/Realm/Realm.SourceGenerator/Diagnostics.cs b/Realm/Realm.SourceGenerator/Diagnostics.cs index 697bcd292d..986ced9c9e 100644 --- a/Realm/Realm.SourceGenerator/Diagnostics.cs +++ b/Realm/Realm.SourceGenerator/Diagnostics.cs @@ -49,7 +49,6 @@ private enum Id DateTimeNotSupported = 23, TypeNotSupported = 24, RealmObjectWithoutAutomaticProperty = 25, - NotPersistedPropertyWithRealmAttributes = 26, ParentOfNestedClassIsNotPartial = 27, IndexedPrimaryKey = 28, } @@ -322,15 +321,6 @@ public static Diagnostic RealmObjectWithoutAutomaticProperty(string className, s location); } - public static Diagnostic NotPersistedPropertyWithRealmAttributes(string className, string propertyName, Location location) - { - return CreateDiagnosticWarning( - Id.NotPersistedPropertyWithRealmAttributes, - "Not persisted property with Realm attributes", - $"{className}.{propertyName} has one or more Realm attributes applied, but it's not persisted, so those attributes will be ignored.", - location); - } - #endregion private static Diagnostic CreateDiagnostic(Id id, string title, string messageFormat, DiagnosticSeverity severity, diff --git a/Realm/Realm.Weaver/RealmWeaver.cs b/Realm/Realm.Weaver/RealmWeaver.cs index 1daf14fd5f..0c4b85e6ad 100644 --- a/Realm/Realm.Weaver/RealmWeaver.cs +++ b/Realm/Realm.Weaver/RealmWeaver.cs @@ -445,7 +445,7 @@ private WeaveTypeResult WeaveType(TypeDefinition type) if (realmAttributeNames.Any()) { - _logger.Warning($"{type.Name}.{prop.Name} has {string.Join(", ", realmAttributeNames)} applied, but it's not persisted, so those attributes will be ignored.", sequencePoint); + _logger.Warning($"{type.Name}.{prop.Name} has {string.Join(", ", realmAttributeNames)} applied, but it's not persisted, so these attributes will be ignored. Skip reason: {weaveResult.SkipReason}", sequencePoint); } } } @@ -517,7 +517,7 @@ private WeavePropertyResult WeaveProperty(PropertyDefinition prop, TypeDefinitio if (prop.GetMethod == null) { - return WeavePropertyResult.Skipped(); + return WeavePropertyResult.Skipped("Property has no getter"); } var indexedAttribute = prop.CustomAttributes.FirstOrDefault(a => a.AttributeType.Name == "IndexedAttribute"); @@ -570,7 +570,7 @@ private WeavePropertyResult WeaveProperty(PropertyDefinition prop, TypeDefinitio return WeavePropertyResult.Warning($"{type.Name}.{prop.Name} is not an automatic property but its type is a AsymmetricObject. This usually indicates a relationship but AsymmetricObjects are not allowed to be the receiving end of any relationships."); } - return WeavePropertyResult.Skipped(); + return WeavePropertyResult.Skipped("Property is not autoimplemented"); } var backlinkAttribute = prop.CustomAttributes.FirstOrDefault(a => a.AttributeType.Name == "BacklinkAttribute"); @@ -589,7 +589,7 @@ private WeavePropertyResult WeaveProperty(PropertyDefinition prop, TypeDefinitio { if (prop.SetMethod == null) { - return WeavePropertyResult.Skipped(); + return WeavePropertyResult.Skipped("Property has no setter"); } var setter = isPrimaryKey ? _references.RealmObject_SetValueUnique : _references.RealmObject_SetValue; @@ -707,7 +707,7 @@ private WeavePropertyResult WeaveProperty(PropertyDefinition prop, TypeDefinitio } else if (prop.SetMethod == null) { - return WeavePropertyResult.Skipped(); + return WeavePropertyResult.Skipped("Property has no setter"); } else if (prop.PropertyType.FullName == "System.DateTime") { diff --git a/Realm/Realm.Weaver/WeaveResults.cs b/Realm/Realm.Weaver/WeaveResults.cs index 87a363bb77..baea030a41 100644 --- a/Realm/Realm.Weaver/WeaveResults.cs +++ b/Realm/Realm.Weaver/WeaveResults.cs @@ -150,15 +150,17 @@ public static WeavePropertyResult Error(string error) return new WeavePropertyResult(error: error); } - public static WeavePropertyResult Skipped() + public static WeavePropertyResult Skipped(string reason) { - return new WeavePropertyResult(); + return new WeavePropertyResult(skipReason: reason); } public string? ErrorMessage { get; } public string? WarningMessage { get; } + public string? SkipReason { get; } + [MemberNotNullWhen(true, nameof(Property))] public bool Woven { get; } @@ -181,10 +183,11 @@ private WeavePropertyResult(PropertyDefinition property, FieldReference? field, Woven = true; } - private WeavePropertyResult(string? error = null, string? warning = null) + private WeavePropertyResult(string? error = null, string? warning = null, string? skipReason = null) { ErrorMessage = error; WarningMessage = warning; + SkipReason = skipReason; } public override string ToString() diff --git a/Realm/Realm/Realm.csproj b/Realm/Realm/Realm.csproj index dd4a118895..275a6b0c2b 100644 --- a/Realm/Realm/Realm.csproj +++ b/Realm/Realm/Realm.csproj @@ -44,7 +44,7 @@ - + diff --git a/Tests/Weaver/AnalyticsAssembly/Program.cs b/Tests/Weaver/AnalyticsAssembly/Program.cs index ea925c19c4..778b4f6388 100644 --- a/Tests/Weaver/AnalyticsAssembly/Program.cs +++ b/Tests/Weaver/AnalyticsAssembly/Program.cs @@ -256,13 +256,6 @@ public static void ApiKeyAuthenticationMethod() } #endif -#if SERVER_API_KEY - public static void ServerApiKeyAuthenticationMethod() - { - _ = Credentials.ServerApiKey("serverApiKey"); - } -#endif - #if FUNCTION public static void FunctionAuthenticationMethod() { diff --git a/Tests/Weaver/Realm.Fody.Tests/WeaverTests.cs b/Tests/Weaver/Realm.Fody.Tests/WeaverTests.cs index 4e9041c90a..e32e09078e 100644 --- a/Tests/Weaver/Realm.Fody.Tests/WeaverTests.cs +++ b/Tests/Weaver/Realm.Fody.Tests/WeaverTests.cs @@ -476,10 +476,10 @@ public void MatchErrorsAndWarnings() { "LambdaPropertyObject.FirstPropertyObject is not an automatic property but its type is a RealmObject/EmbeddedObject which normally indicates a relationship.", "Sensor.FirstMeasurement is not an automatic property but its type is a AsymmetricObject. This usually indicates a relationship but AsymmetricObjects are not allowed to be the receiving end of any relationships.", - "IncorrectAttributes.AutomaticId has [PrimaryKey] applied, but it's not persisted, so those attributes will be ignored.", - "IncorrectAttributes.AutomaticDate has [Indexed] applied, but it's not persisted, so those attributes will be ignored.", - "IncorrectAttributes.Email_ has [MapTo] applied, but it's not persisted, so those attributes will be ignored.", - "IncorrectAttributes.Date_ has [Indexed], [MapTo] applied, but it's not persisted, so those attributes will be ignored.", + "IncorrectAttributes.AutomaticId has [PrimaryKey] applied, but it's not persisted, so these attributes will be ignored. Skip reason: Property has no setter", + "IncorrectAttributes.AutomaticDate has [Indexed] applied, but it's not persisted, so these attributes will be ignored. Skip reason: Property has no setter", + "IncorrectAttributes.Email_ has [MapTo] applied, but it's not persisted, so these attributes will be ignored. Skip reason: Property has no setter", + "IncorrectAttributes.Date_ has [Indexed], [MapTo] applied, but it's not persisted, so these attributes will be ignored. Skip reason: Property has no setter", "AccessorTestObject.SetterLessObject does not have a setter but its type is a RealmObject/EmbeddedObject which normally indicates a relationship.", };