Skip to content

Commit 3e8a709

Browse files
Extension value nullability (Azure#19287)
* Make extension attribute value not nullable * Export API * fix comment
1 parent a51887e commit 3e8a709

File tree

6 files changed

+30
-7
lines changed

6 files changed

+30
-7
lines changed

sdk/core/Azure.Core/api/Azure.Core.net461.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -581,7 +581,7 @@ public CloudEvent(string source, string type, object? jsonSerializableData, Syst
581581
public System.BinaryData? Data { get { throw null; } set { } }
582582
public string? DataContentType { get { throw null; } set { } }
583583
public string? DataSchema { get { throw null; } set { } }
584-
public System.Collections.Generic.IDictionary<string, object?> ExtensionAttributes { get { throw null; } }
584+
public System.Collections.Generic.IDictionary<string, object> ExtensionAttributes { get { throw null; } }
585585
public string Id { get { throw null; } set { } }
586586
public string Source { get { throw null; } set { } }
587587
public string? Subject { get { throw null; } set { } }

sdk/core/Azure.Core/api/Azure.Core.net5.0.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -581,7 +581,7 @@ public CloudEvent(string source, string type, object? jsonSerializableData, Syst
581581
public System.BinaryData? Data { get { throw null; } set { } }
582582
public string? DataContentType { get { throw null; } set { } }
583583
public string? DataSchema { get { throw null; } set { } }
584-
public System.Collections.Generic.IDictionary<string, object?> ExtensionAttributes { get { throw null; } }
584+
public System.Collections.Generic.IDictionary<string, object> ExtensionAttributes { get { throw null; } }
585585
public string Id { get { throw null; } set { } }
586586
public string Source { get { throw null; } set { } }
587587
public string? Subject { get { throw null; } set { } }

sdk/core/Azure.Core/api/Azure.Core.netstandard2.0.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -581,7 +581,7 @@ public CloudEvent(string source, string type, object? jsonSerializableData, Syst
581581
public System.BinaryData? Data { get { throw null; } set { } }
582582
public string? DataContentType { get { throw null; } set { } }
583583
public string? DataSchema { get { throw null; } set { } }
584-
public System.Collections.Generic.IDictionary<string, object?> ExtensionAttributes { get { throw null; } }
584+
public System.Collections.Generic.IDictionary<string, object> ExtensionAttributes { get { throw null; } }
585585
public string Id { get { throw null; } set { } }
586586
public string Source { get { throw null; } set { } }
587587
public string? Subject { get { throw null; } set { } }

sdk/core/Azure.Core/src/Messaging/CloudEvent.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ public string Type
141141
/// <summary>
142142
/// Gets extension attributes that can be additionally added to the CloudEvent envelope.
143143
/// </summary>
144-
public IDictionary<string, object?> ExtensionAttributes { get; } = new CloudEventExtensionAttributes<string, object?>();
144+
public IDictionary<string, object> ExtensionAttributes { get; } = new CloudEventExtensionAttributes<string, object>();
145145

146146
/// <summary>
147147
/// Given JSON-encoded events, parses the event envelope and returns an array of CloudEvents.

sdk/core/Azure.Core/src/Messaging/CloudEventConverter.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,13 @@ internal static CloudEvent DeserializeCloudEvent(JsonElement element, bool skipV
9797
{
9898
if (!skipValidation)
9999
{
100-
cloudEvent.ExtensionAttributes.Add(property.Name, GetObject(property.Value));
100+
cloudEvent.ExtensionAttributes.Add(property.Name, GetObject(property.Value)!);
101101
}
102102
else
103103
{
104104
// This aspect of skipValidation would not be supported for converters that live in a different
105105
// package since CloudEventExtensionAttributes is internal.
106-
((CloudEventExtensionAttributes<string, object?>)cloudEvent.ExtensionAttributes).AddWithoutValidation(property.Name, GetObject(property.Value));
106+
((CloudEventExtensionAttributes<string, object>)cloudEvent.ExtensionAttributes).AddWithoutValidation(property.Name, GetObject(property.Value)!);
107107
}
108108
}
109109
}
@@ -213,7 +213,7 @@ public override void Write(Utf8JsonWriter writer, CloudEvent value, JsonSerializ
213213
writer.WritePropertyName(CloudEventConstants.Subject);
214214
writer.WriteStringValue(value.Subject);
215215
}
216-
foreach (KeyValuePair<string, object?> item in value.ExtensionAttributes)
216+
foreach (KeyValuePair<string, object> item in value.ExtensionAttributes)
217217
{
218218
writer.WritePropertyName(item.Key);
219219
WriteObjectValue(writer, item.Value);

sdk/core/Azure.Core/tests/CloudEventTests.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -926,6 +926,29 @@ public void CanParseInvalidAttributes(bool skipValidation)
926926
Assert.AreEqual(5, ((IDictionary<string, object>)evt.ExtensionAttributes["dict"])["key2"]);
927927
}
928928
}
929+
930+
[Test]
931+
[TestCase(true)]
932+
[TestCase(false)]
933+
public void CanParseNullAttributeValue(bool skipValidation)
934+
{
935+
// null extension attribute values can still be deserialized.
936+
var json = new BinaryData("{\"subject\": \"Subject-0\", \"source\":\"source\", \"specversion\":\"1.0\", \"id\": \"id\", \"type\": \"type\", \"key\":null }");
937+
938+
if (!skipValidation)
939+
{
940+
Assert.That(
941+
() => CloudEvent.ParseMany(json),
942+
Throws.InstanceOf<ArgumentException>());
943+
}
944+
else
945+
{
946+
var evt = CloudEvent.ParseMany(json, true)[0];
947+
Assert.AreEqual("Subject-0", evt.Subject);
948+
Assert.AreEqual("type", evt.Type);
949+
Assert.IsNull(evt.ExtensionAttributes["key"]);
950+
}
951+
}
929952
}
930953

931954
#pragma warning disable SA1402 // File may only contain a single type

0 commit comments

Comments
 (0)