Skip to content

Commit aeb7bd9

Browse files
API updates (Azure#19250)
* API updates * Export API * Use parse * PR FB * Fix exception message * fix
1 parent f28cbe4 commit aeb7bd9

File tree

12 files changed

+303
-278
lines changed

12 files changed

+303
-278
lines changed

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -582,13 +582,13 @@ public CloudEvent(string source, string type, object? jsonSerializableData, Syst
582582
public string? DataContentType { get { throw null; } set { } }
583583
public string? DataSchema { get { throw null; } set { } }
584584
public System.Collections.Generic.IDictionary<string, object?> ExtensionAttributes { get { throw null; } }
585-
public string? Id { get { throw null; } set { } }
586-
public string? Source { get { throw null; } set { } }
585+
public string Id { get { throw null; } set { } }
586+
public string Source { get { throw null; } set { } }
587587
public string? Subject { get { throw null; } set { } }
588588
public System.DateTimeOffset? Time { get { throw null; } set { } }
589-
public string? Type { get { throw null; } set { } }
590-
public static Azure.Messaging.CloudEvent? Parse(System.BinaryData jsonEvent, bool skipValidation = false) { throw null; }
591-
public static Azure.Messaging.CloudEvent[] ParseEvents(string jsonContent, bool skipValidation = false) { throw null; }
589+
public string Type { get { throw null; } set { } }
590+
public static Azure.Messaging.CloudEvent? Parse(System.BinaryData json, bool skipValidation = false) { throw null; }
591+
public static Azure.Messaging.CloudEvent[] ParseMany(System.BinaryData json, bool skipValidation = false) { throw null; }
592592
}
593593
public enum CloudEventDataFormat
594594
{

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -582,13 +582,13 @@ public CloudEvent(string source, string type, object? jsonSerializableData, Syst
582582
public string? DataContentType { get { throw null; } set { } }
583583
public string? DataSchema { get { throw null; } set { } }
584584
public System.Collections.Generic.IDictionary<string, object?> ExtensionAttributes { get { throw null; } }
585-
public string? Id { get { throw null; } set { } }
586-
public string? Source { get { throw null; } set { } }
585+
public string Id { get { throw null; } set { } }
586+
public string Source { get { throw null; } set { } }
587587
public string? Subject { get { throw null; } set { } }
588588
public System.DateTimeOffset? Time { get { throw null; } set { } }
589-
public string? Type { get { throw null; } set { } }
590-
public static Azure.Messaging.CloudEvent? Parse(System.BinaryData jsonEvent, bool skipValidation = false) { throw null; }
591-
public static Azure.Messaging.CloudEvent[] ParseEvents(string jsonContent, bool skipValidation = false) { throw null; }
589+
public string Type { get { throw null; } set { } }
590+
public static Azure.Messaging.CloudEvent? Parse(System.BinaryData json, bool skipValidation = false) { throw null; }
591+
public static Azure.Messaging.CloudEvent[] ParseMany(System.BinaryData json, bool skipValidation = false) { throw null; }
592592
}
593593
public enum CloudEventDataFormat
594594
{

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -582,13 +582,13 @@ public CloudEvent(string source, string type, object? jsonSerializableData, Syst
582582
public string? DataContentType { get { throw null; } set { } }
583583
public string? DataSchema { get { throw null; } set { } }
584584
public System.Collections.Generic.IDictionary<string, object?> ExtensionAttributes { get { throw null; } }
585-
public string? Id { get { throw null; } set { } }
586-
public string? Source { get { throw null; } set { } }
585+
public string Id { get { throw null; } set { } }
586+
public string Source { get { throw null; } set { } }
587587
public string? Subject { get { throw null; } set { } }
588588
public System.DateTimeOffset? Time { get { throw null; } set { } }
589-
public string? Type { get { throw null; } set { } }
590-
public static Azure.Messaging.CloudEvent? Parse(System.BinaryData jsonEvent, bool skipValidation = false) { throw null; }
591-
public static Azure.Messaging.CloudEvent[] ParseEvents(string jsonContent, bool skipValidation = false) { throw null; }
589+
public string Type { get { throw null; } set { } }
590+
public static Azure.Messaging.CloudEvent? Parse(System.BinaryData json, bool skipValidation = false) { throw null; }
591+
public static Azure.Messaging.CloudEvent[] ParseMany(System.BinaryData json, bool skipValidation = false) { throw null; }
592592
}
593593
public enum CloudEventDataFormat
594594
{

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

Lines changed: 45 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ public class CloudEvent
1919
/// <param name="jsonSerializableData"> Event data specific to the event type. </param>
2020
/// <param name="dataSerializationType">The type to use when serializing the data.
2121
/// If not specified, <see cref="object.GetType()"/> will be used on <paramref name="jsonSerializableData"/>.</param>
22+
/// <exception cref="ArgumentNullException">
23+
/// <paramref name="source"/> or <paramref name="type"/> was null.
24+
/// </exception>
2225
public CloudEvent(string source, string type, object? jsonSerializableData, Type? dataSerializationType = default)
2326
{
2427
if (jsonSerializableData is BinaryData)
@@ -38,7 +41,11 @@ public CloudEvent(string source, string type, object? jsonSerializableData, Type
3841
/// <param name="type"> Type of event related to the originating occurrence. For example, "Contoso.Items.ItemReceived". </param>
3942
/// <param name="data"> Binary event data specific to the event type. </param>
4043
/// <param name="dataContentType"> Content type of the payload. A content type different from "application/json" should be specified if payload is not JSON. </param>
41-
/// <param name="dataFormat"></param>
44+
/// <param name="dataFormat">The format that the data of a <see cref="CloudEvent"/> should be sent in
45+
/// when using the JSON envelope format.</param>
46+
/// <exception cref="ArgumentNullException">
47+
/// <paramref name="source"/> or <paramref name="type"/> was null.
48+
/// </exception>
4249
public CloudEvent(string source, string type, BinaryData? data, string? dataContentType, CloudEventDataFormat dataFormat = CloudEventDataFormat.Binary)
4350
{
4451
Source = source;
@@ -62,11 +69,11 @@ internal CloudEvent() { }
6269
/// Gets or sets an identifier for the event. The combination of <see cref="Id"/> and <see cref="Source"/> must be unique for each distinct event.
6370
/// If not explicitly set, this will default to a <see cref="Guid"/>.
6471
/// </summary>
65-
public string? Id
72+
public string Id
6673
{
6774
get
6875
{
69-
return _id;
76+
return _id!;
7077
}
7178
set
7279
{
@@ -80,11 +87,11 @@ public string? Id
8087

8188
/// <summary>Gets or sets the context in which an event happened. The combination of <see cref="Id"/>
8289
/// and <see cref="Source"/> must be unique for each distinct event.</summary>
83-
public string? Source
90+
public string Source
8491
{
8592
get
8693
{
87-
return _source;
94+
return _source!;
8895
}
8996
set
9097
{
@@ -95,11 +102,11 @@ public string? Source
95102
private string? _source;
96103

97104
/// <summary>Gets or sets the type of event related to the originating occurrence.</summary>
98-
public string? Type
105+
public string Type
99106
{
100107
get
101108
{
102-
return _type;
109+
return _type!;
103110
}
104111
set
105112
{
@@ -142,19 +149,18 @@ public string? Type
142149
/// By default, if the event is missing required properties, an exception is thrown though this can be relaxed
143150
/// by setting the <paramref name="skipValidation"/> parameter.
144151
/// </summary>
145-
/// <param name="jsonContent"> The JSON-encoded representation of either a single event or an array or events,
146-
/// in the CloudEvent schema.</param>
152+
/// <param name="json">An instance of <see cref="BinaryData"/> containing the JSON for one or more CloudEvents.</param>
147153
/// <param name="skipValidation">Set to <see langword="true"/> to allow missing or invalid properties to still parse into a CloudEvent.
148154
/// In particular, by setting strict to <see langword="true"/>, the source, id, specversion and type properties are no longer required
149155
/// to be present in the JSON. Additionally, the casing requirements of the extension attribute names are relaxed.
150156
/// </param>
151-
/// <returns> A list of <see cref="CloudEvent"/>. </returns>
152-
public static CloudEvent[] ParseEvents(string jsonContent, bool skipValidation = false)
157+
/// <returns> An array of <see cref="CloudEvent"/> instances.</returns>
158+
public static CloudEvent[] ParseMany(BinaryData json, bool skipValidation = false)
153159
{
154-
Argument.AssertNotNull(jsonContent, nameof(jsonContent));
160+
Argument.AssertNotNull(json, nameof(json));
155161

156162
CloudEvent[]? cloudEvents = null;
157-
JsonDocument requestDocument = JsonDocument.Parse(jsonContent);
163+
JsonDocument requestDocument = JsonDocument.Parse(json);
158164

159165
// Parse JsonElement into separate events, deserialize event envelope properties
160166
if (requestDocument.RootElement.ValueKind == JsonValueKind.Object)
@@ -180,30 +186,42 @@ public static CloudEvent[] ParseEvents(string jsonContent, bool skipValidation =
180186
/// By default, if the event is missing required properties, an exception is thrown though this can be relaxed
181187
/// by setting the <paramref name="skipValidation"/> parameter.
182188
/// </summary>
183-
/// <param name="jsonEvent">Specifies the instance of <see cref="BinaryData"/>.</param>
189+
/// <param name="json">An instance of <see cref="BinaryData"/> containing the JSON for the CloudEvent.</param>
184190
/// <param name="skipValidation">Set to <see langword="true"/> to allow missing or invalid properties to still parse into a CloudEvent.
185191
/// In particular, by setting strict to <see langword="true"/>, the source, id, specversion and type properties are no longer required
186192
/// to be present in the JSON. Additionally, the casing requirements of the extension attribute names are relaxed.
187193
/// </param>
188194
/// <returns> A <see cref="CloudEvent"/>. </returns>
189-
public static CloudEvent? Parse(BinaryData jsonEvent, bool skipValidation = false)
195+
/// <exception cref="ArgumentException">
196+
/// <paramref name="json"/> contained multiple events. <see cref="ParseMany"/> should be used instead.
197+
/// </exception>
198+
public static CloudEvent? Parse(BinaryData json, bool skipValidation = false)
190199
{
191-
Argument.AssertNotNull(jsonEvent, nameof(jsonEvent));
192-
CloudEvent[]? events = ParseEvents(jsonEvent.ToString(), skipValidation);
193-
if (events.Length == 0)
200+
Argument.AssertNotNull(json, nameof(json));
201+
202+
JsonDocument requestDocument = JsonDocument.Parse(json);
203+
CloudEvent? cloudEvent = null;
204+
if (requestDocument.RootElement.ValueKind == JsonValueKind.Object)
194205
{
195-
return null;
206+
cloudEvent = CloudEventConverter.DeserializeCloudEvent(requestDocument.RootElement, skipValidation);
196207
}
197-
if (events.Length > 1)
208+
else if (requestDocument.RootElement.ValueKind == JsonValueKind.Array)
198209
{
199-
throw new ArgumentException(
200-
"The BinaryData instance contains JSON from multiple cloud events. This method " +
201-
"should only be used with BinaryData containing a single cloud event. " +
202-
Environment.NewLine +
203-
"To parse multiple events, call ToString on the BinaryData and use the " +
204-
"Parse overload that takes a string.");
210+
if (requestDocument.RootElement.GetArrayLength() > 1)
211+
{
212+
throw new ArgumentException(
213+
"The BinaryData instance contains JSON from multiple cloud events. This method " +
214+
"should only be used with BinaryData containing a single cloud event. " +
215+
Environment.NewLine +
216+
$"To parse multiple events, use the {nameof(ParseMany)} overload.");
217+
}
218+
foreach (JsonElement property in requestDocument.RootElement.EnumerateArray())
219+
{
220+
cloudEvent = CloudEventConverter.DeserializeCloudEvent(property, skipValidation);
221+
break;
222+
}
205223
}
206-
return events[0];
224+
return cloudEvent;
207225
}
208226
}
209227
}

0 commit comments

Comments
 (0)