Skip to content

Commit d3b10da

Browse files
algolia-botFluf22
andcommitted
fix(generators): auto-infer discriminator fields from unique required properties (generated)
algolia/api-clients-automation#6199 Co-authored-by: algolia-bot <accounts+algolia-api-client-bot@algolia.com> Co-authored-by: Thomas Raffray <Fluf22@users.noreply.github.com>
1 parent b48b5f9 commit d3b10da

9 files changed

Lines changed: 179 additions & 163 deletions

File tree

algoliasearch/Models/Composition/BatchCompositionAction.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,11 @@ JsonSerializerOptions options
168168
{
169169
var jsonDocument = JsonDocument.ParseValue(ref reader);
170170
var root = jsonDocument.RootElement;
171-
if (root.ValueKind == JsonValueKind.Object)
171+
if (
172+
root.ValueKind == JsonValueKind.Object
173+
&& root.TryGetProperty("behavior", out _)
174+
&& root.TryGetProperty("name", out _)
175+
)
172176
{
173177
try
174178
{

algoliasearch/Models/Composition/CompositionBehavior.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ JsonSerializerOptions options
168168
{
169169
var jsonDocument = JsonDocument.ParseValue(ref reader);
170170
var root = jsonDocument.RootElement;
171-
if (root.ValueKind == JsonValueKind.Object)
171+
if (root.ValueKind == JsonValueKind.Object && root.TryGetProperty("injection", out _))
172172
{
173173
try
174174
{
@@ -184,7 +184,7 @@ JsonSerializerOptions options
184184
);
185185
}
186186
}
187-
if (root.ValueKind == JsonValueKind.Object)
187+
if (root.ValueKind == JsonValueKind.Object && root.TryGetProperty("multifeed", out _))
188188
{
189189
try
190190
{

algoliasearch/Models/Composition/RulesBatchCompositionAction.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ JsonSerializerOptions options
168168
{
169169
var jsonDocument = JsonDocument.ParseValue(ref reader);
170170
var root = jsonDocument.RootElement;
171-
if (root.ValueKind == JsonValueKind.Object)
171+
if (root.ValueKind == JsonValueKind.Object && root.TryGetProperty("consequence", out _))
172172
{
173173
try
174174
{

algoliasearch/Models/Ingestion/TaskInput.cs

Lines changed: 38 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -22,32 +22,32 @@ public partial class TaskInput : AbstractSchema
2222
{
2323
/// <summary>
2424
/// Initializes a new instance of the TaskInput class
25-
/// with a StreamingInput
25+
/// with a ShopifyInput
2626
/// </summary>
27-
/// <param name="actualInstance">An instance of StreamingInput.</param>
28-
public TaskInput(StreamingInput actualInstance)
27+
/// <param name="actualInstance">An instance of ShopifyInput.</param>
28+
public TaskInput(ShopifyInput actualInstance)
2929
{
3030
ActualInstance =
3131
actualInstance ?? throw new ArgumentException("Invalid instance found. Must not be null.");
3232
}
3333

3434
/// <summary>
3535
/// Initializes a new instance of the TaskInput class
36-
/// with a DockerStreamsInput
36+
/// with a StreamingInput
3737
/// </summary>
38-
/// <param name="actualInstance">An instance of DockerStreamsInput.</param>
39-
public TaskInput(DockerStreamsInput actualInstance)
38+
/// <param name="actualInstance">An instance of StreamingInput.</param>
39+
public TaskInput(StreamingInput actualInstance)
4040
{
4141
ActualInstance =
4242
actualInstance ?? throw new ArgumentException("Invalid instance found. Must not be null.");
4343
}
4444

4545
/// <summary>
4646
/// Initializes a new instance of the TaskInput class
47-
/// with a ShopifyInput
47+
/// with a DockerStreamsInput
4848
/// </summary>
49-
/// <param name="actualInstance">An instance of ShopifyInput.</param>
50-
public TaskInput(ShopifyInput actualInstance)
49+
/// <param name="actualInstance">An instance of DockerStreamsInput.</param>
50+
public TaskInput(DockerStreamsInput actualInstance)
5151
{
5252
ActualInstance =
5353
actualInstance ?? throw new ArgumentException("Invalid instance found. Must not be null.");
@@ -58,6 +58,16 @@ public TaskInput(ShopifyInput actualInstance)
5858
/// </summary>
5959
public sealed override object ActualInstance { get; set; }
6060

61+
/// <summary>
62+
/// Get the actual instance of `ShopifyInput`. If the actual instance is not `ShopifyInput`,
63+
/// the InvalidClassException will be thrown
64+
/// </summary>
65+
/// <returns>An instance of ShopifyInput</returns>
66+
public ShopifyInput AsShopifyInput()
67+
{
68+
return (ShopifyInput)ActualInstance;
69+
}
70+
6171
/// <summary>
6272
/// Get the actual instance of `StreamingInput`. If the actual instance is not `StreamingInput`,
6373
/// the InvalidClassException will be thrown
@@ -79,13 +89,12 @@ public DockerStreamsInput AsDockerStreamsInput()
7989
}
8090

8191
/// <summary>
82-
/// Get the actual instance of `ShopifyInput`. If the actual instance is not `ShopifyInput`,
83-
/// the InvalidClassException will be thrown
92+
/// Check if the actual instance is of `ShopifyInput` type.
8493
/// </summary>
85-
/// <returns>An instance of ShopifyInput</returns>
86-
public ShopifyInput AsShopifyInput()
94+
/// <returns>Whether or not the instance is the type</returns>
95+
public bool IsShopifyInput()
8796
{
88-
return (ShopifyInput)ActualInstance;
97+
return ActualInstance.GetType() == typeof(ShopifyInput);
8998
}
9099

91100
/// <summary>
@@ -106,15 +115,6 @@ public bool IsDockerStreamsInput()
106115
return ActualInstance.GetType() == typeof(DockerStreamsInput);
107116
}
108117

109-
/// <summary>
110-
/// Check if the actual instance is of `ShopifyInput` type.
111-
/// </summary>
112-
/// <returns>Whether or not the instance is the type</returns>
113-
public bool IsShopifyInput()
114-
{
115-
return ActualInstance.GetType() == typeof(ShopifyInput);
116-
}
117-
118118
/// <summary>
119119
/// Returns the string presentation of the object
120120
/// </summary>
@@ -198,44 +198,48 @@ JsonSerializerOptions options
198198
{
199199
var jsonDocument = JsonDocument.ParseValue(ref reader);
200200
var root = jsonDocument.RootElement;
201-
if (root.ValueKind == JsonValueKind.Object && root.TryGetProperty("mapping", out _))
201+
if (
202+
root.ValueKind == JsonValueKind.Object
203+
&& root.TryGetProperty("market", out _)
204+
&& root.TryGetProperty("metafields", out _)
205+
)
202206
{
203207
try
204208
{
205-
return new TaskInput(jsonDocument.Deserialize<StreamingInput>(JsonConfig.Options));
209+
return new TaskInput(jsonDocument.Deserialize<ShopifyInput>(JsonConfig.Options));
206210
}
207211
catch (Exception exception)
208212
{
209213
// deserialization failed, try the next one
210-
System.Diagnostics.Debug.WriteLine(
211-
$"Failed to deserialize into StreamingInput: {exception}"
212-
);
214+
System.Diagnostics.Debug.WriteLine($"Failed to deserialize into ShopifyInput: {exception}");
213215
}
214216
}
215-
if (root.ValueKind == JsonValueKind.Object && root.TryGetProperty("streams", out _))
217+
if (root.ValueKind == JsonValueKind.Object && root.TryGetProperty("mapping", out _))
216218
{
217219
try
218220
{
219-
return new TaskInput(jsonDocument.Deserialize<DockerStreamsInput>(JsonConfig.Options));
221+
return new TaskInput(jsonDocument.Deserialize<StreamingInput>(JsonConfig.Options));
220222
}
221223
catch (Exception exception)
222224
{
223225
// deserialization failed, try the next one
224226
System.Diagnostics.Debug.WriteLine(
225-
$"Failed to deserialize into DockerStreamsInput: {exception}"
227+
$"Failed to deserialize into StreamingInput: {exception}"
226228
);
227229
}
228230
}
229-
if (root.ValueKind == JsonValueKind.Object)
231+
if (root.ValueKind == JsonValueKind.Object && root.TryGetProperty("streams", out _))
230232
{
231233
try
232234
{
233-
return new TaskInput(jsonDocument.Deserialize<ShopifyInput>(JsonConfig.Options));
235+
return new TaskInput(jsonDocument.Deserialize<DockerStreamsInput>(JsonConfig.Options));
234236
}
235237
catch (Exception exception)
236238
{
237239
// deserialization failed, try the next one
238-
System.Diagnostics.Debug.WriteLine($"Failed to deserialize into ShopifyInput: {exception}");
240+
System.Diagnostics.Debug.WriteLine(
241+
$"Failed to deserialize into DockerStreamsInput: {exception}"
242+
);
239243
}
240244
}
241245
throw new InvalidDataException(

algoliasearch/Models/Ingestion/TransformationInput.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ JsonSerializerOptions options
168168
{
169169
var jsonDocument = JsonDocument.ParseValue(ref reader);
170170
var root = jsonDocument.RootElement;
171-
if (root.ValueKind == JsonValueKind.Object)
171+
if (root.ValueKind == JsonValueKind.Object && root.TryGetProperty("code", out _))
172172
{
173173
try
174174
{
@@ -184,7 +184,7 @@ JsonSerializerOptions options
184184
);
185185
}
186186
}
187-
if (root.ValueKind == JsonValueKind.Object)
187+
if (root.ValueKind == JsonValueKind.Object && root.TryGetProperty("steps", out _))
188188
{
189189
try
190190
{

algoliasearch/Models/Ingestion/Trigger.cs

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,21 @@ public partial class Trigger : AbstractSchema
2222
{
2323
/// <summary>
2424
/// Initializes a new instance of the Trigger class
25-
/// with a OnDemandTrigger
25+
/// with a ScheduleTrigger
2626
/// </summary>
27-
/// <param name="actualInstance">An instance of OnDemandTrigger.</param>
28-
public Trigger(OnDemandTrigger actualInstance)
27+
/// <param name="actualInstance">An instance of ScheduleTrigger.</param>
28+
public Trigger(ScheduleTrigger actualInstance)
2929
{
3030
ActualInstance =
3131
actualInstance ?? throw new ArgumentException("Invalid instance found. Must not be null.");
3232
}
3333

3434
/// <summary>
3535
/// Initializes a new instance of the Trigger class
36-
/// with a ScheduleTrigger
36+
/// with a OnDemandTrigger
3737
/// </summary>
38-
/// <param name="actualInstance">An instance of ScheduleTrigger.</param>
39-
public Trigger(ScheduleTrigger actualInstance)
38+
/// <param name="actualInstance">An instance of OnDemandTrigger.</param>
39+
public Trigger(OnDemandTrigger actualInstance)
4040
{
4141
ActualInstance =
4242
actualInstance ?? throw new ArgumentException("Invalid instance found. Must not be null.");
@@ -70,23 +70,23 @@ public Trigger(StreamingTrigger actualInstance)
7070
public sealed override object ActualInstance { get; set; }
7171

7272
/// <summary>
73-
/// Get the actual instance of `OnDemandTrigger`. If the actual instance is not `OnDemandTrigger`,
73+
/// Get the actual instance of `ScheduleTrigger`. If the actual instance is not `ScheduleTrigger`,
7474
/// the InvalidClassException will be thrown
7575
/// </summary>
76-
/// <returns>An instance of OnDemandTrigger</returns>
77-
public OnDemandTrigger AsOnDemandTrigger()
76+
/// <returns>An instance of ScheduleTrigger</returns>
77+
public ScheduleTrigger AsScheduleTrigger()
7878
{
79-
return (OnDemandTrigger)ActualInstance;
79+
return (ScheduleTrigger)ActualInstance;
8080
}
8181

8282
/// <summary>
83-
/// Get the actual instance of `ScheduleTrigger`. If the actual instance is not `ScheduleTrigger`,
83+
/// Get the actual instance of `OnDemandTrigger`. If the actual instance is not `OnDemandTrigger`,
8484
/// the InvalidClassException will be thrown
8585
/// </summary>
86-
/// <returns>An instance of ScheduleTrigger</returns>
87-
public ScheduleTrigger AsScheduleTrigger()
86+
/// <returns>An instance of OnDemandTrigger</returns>
87+
public OnDemandTrigger AsOnDemandTrigger()
8888
{
89-
return (ScheduleTrigger)ActualInstance;
89+
return (OnDemandTrigger)ActualInstance;
9090
}
9191

9292
/// <summary>
@@ -110,21 +110,21 @@ public StreamingTrigger AsStreamingTrigger()
110110
}
111111

112112
/// <summary>
113-
/// Check if the actual instance is of `OnDemandTrigger` type.
113+
/// Check if the actual instance is of `ScheduleTrigger` type.
114114
/// </summary>
115115
/// <returns>Whether or not the instance is the type</returns>
116-
public bool IsOnDemandTrigger()
116+
public bool IsScheduleTrigger()
117117
{
118-
return ActualInstance.GetType() == typeof(OnDemandTrigger);
118+
return ActualInstance.GetType() == typeof(ScheduleTrigger);
119119
}
120120

121121
/// <summary>
122-
/// Check if the actual instance is of `ScheduleTrigger` type.
122+
/// Check if the actual instance is of `OnDemandTrigger` type.
123123
/// </summary>
124124
/// <returns>Whether or not the instance is the type</returns>
125-
public bool IsScheduleTrigger()
125+
public bool IsOnDemandTrigger()
126126
{
127-
return ActualInstance.GetType() == typeof(ScheduleTrigger);
127+
return ActualInstance.GetType() == typeof(OnDemandTrigger);
128128
}
129129

130130
/// <summary>
@@ -228,31 +228,35 @@ JsonSerializerOptions options
228228
{
229229
var jsonDocument = JsonDocument.ParseValue(ref reader);
230230
var root = jsonDocument.RootElement;
231-
if (root.ValueKind == JsonValueKind.Object)
231+
if (
232+
root.ValueKind == JsonValueKind.Object
233+
&& root.TryGetProperty("cron", out _)
234+
&& root.TryGetProperty("nextRun", out _)
235+
)
232236
{
233237
try
234238
{
235-
return new Trigger(jsonDocument.Deserialize<OnDemandTrigger>(JsonConfig.Options));
239+
return new Trigger(jsonDocument.Deserialize<ScheduleTrigger>(JsonConfig.Options));
236240
}
237241
catch (Exception exception)
238242
{
239243
// deserialization failed, try the next one
240244
System.Diagnostics.Debug.WriteLine(
241-
$"Failed to deserialize into OnDemandTrigger: {exception}"
245+
$"Failed to deserialize into ScheduleTrigger: {exception}"
242246
);
243247
}
244248
}
245249
if (root.ValueKind == JsonValueKind.Object)
246250
{
247251
try
248252
{
249-
return new Trigger(jsonDocument.Deserialize<ScheduleTrigger>(JsonConfig.Options));
253+
return new Trigger(jsonDocument.Deserialize<OnDemandTrigger>(JsonConfig.Options));
250254
}
251255
catch (Exception exception)
252256
{
253257
// deserialization failed, try the next one
254258
System.Diagnostics.Debug.WriteLine(
255-
$"Failed to deserialize into ScheduleTrigger: {exception}"
259+
$"Failed to deserialize into OnDemandTrigger: {exception}"
256260
);
257261
}
258262
}

0 commit comments

Comments
 (0)