Skip to content

Commit b38634f

Browse files
fix(specs): more accurate composition behavior typing (generated)
algolia/api-clients-automation#5892 Co-authored-by: algolia-bot <accounts+algolia-api-client-bot@algolia.com> Co-authored-by: Gavin Wade <gavin.wade12@gmail.com>
1 parent 6a96ca1 commit b38634f

File tree

3 files changed

+338
-23
lines changed

3 files changed

+338
-23
lines changed

algoliasearch/Models/Composition/CompositionBehavior.cs

Lines changed: 148 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,46 +3,97 @@
33
//
44
using System;
55
using System.Collections.Generic;
6+
using System.IO;
67
using System.Linq;
8+
using System.Reflection;
79
using System.Text;
810
using System.Text.Json;
911
using System.Text.Json.Serialization;
12+
using Algolia.Search.Models.Common;
1013
using Algolia.Search.Serializer;
1114

1215
namespace Algolia.Search.Models.Composition;
1316

1417
/// <summary>
1518
/// An object containing either an `injection` or `multifeed` behavior schema, but not both.
1619
/// </summary>
17-
public partial class CompositionBehavior
20+
[JsonConverter(typeof(CompositionBehaviorJsonConverter))]
21+
public partial class CompositionBehavior : AbstractSchema
1822
{
1923
/// <summary>
20-
/// Initializes a new instance of the CompositionBehavior class.
24+
/// Initializes a new instance of the CompositionBehavior class
25+
/// with a CompositionInjectionBehavior
2126
/// </summary>
22-
public CompositionBehavior() { }
27+
/// <param name="actualInstance">An instance of CompositionInjectionBehavior.</param>
28+
public CompositionBehavior(CompositionInjectionBehavior actualInstance)
29+
{
30+
ActualInstance =
31+
actualInstance ?? throw new ArgumentException("Invalid instance found. Must not be null.");
32+
}
33+
34+
/// <summary>
35+
/// Initializes a new instance of the CompositionBehavior class
36+
/// with a CompositionMultifeedBehavior
37+
/// </summary>
38+
/// <param name="actualInstance">An instance of CompositionMultifeedBehavior.</param>
39+
public CompositionBehavior(CompositionMultifeedBehavior actualInstance)
40+
{
41+
ActualInstance =
42+
actualInstance ?? throw new ArgumentException("Invalid instance found. Must not be null.");
43+
}
2344

2445
/// <summary>
25-
/// Gets or Sets Injection
46+
/// Gets or Sets ActualInstance
2647
/// </summary>
27-
[JsonPropertyName("injection")]
28-
public Injection Injection { get; set; }
48+
public sealed override object ActualInstance { get; set; }
2949

3050
/// <summary>
31-
/// Gets or Sets Multifeed
51+
/// Get the actual instance of `CompositionInjectionBehavior`. If the actual instance is not `CompositionInjectionBehavior`,
52+
/// the InvalidClassException will be thrown
3253
/// </summary>
33-
[JsonPropertyName("multifeed")]
34-
public Multifeed Multifeed { get; set; }
54+
/// <returns>An instance of CompositionInjectionBehavior</returns>
55+
public CompositionInjectionBehavior AsCompositionInjectionBehavior()
56+
{
57+
return (CompositionInjectionBehavior)ActualInstance;
58+
}
59+
60+
/// <summary>
61+
/// Get the actual instance of `CompositionMultifeedBehavior`. If the actual instance is not `CompositionMultifeedBehavior`,
62+
/// the InvalidClassException will be thrown
63+
/// </summary>
64+
/// <returns>An instance of CompositionMultifeedBehavior</returns>
65+
public CompositionMultifeedBehavior AsCompositionMultifeedBehavior()
66+
{
67+
return (CompositionMultifeedBehavior)ActualInstance;
68+
}
69+
70+
/// <summary>
71+
/// Check if the actual instance is of `CompositionInjectionBehavior` type.
72+
/// </summary>
73+
/// <returns>Whether or not the instance is the type</returns>
74+
public bool IsCompositionInjectionBehavior()
75+
{
76+
return ActualInstance.GetType() == typeof(CompositionInjectionBehavior);
77+
}
78+
79+
/// <summary>
80+
/// Check if the actual instance is of `CompositionMultifeedBehavior` type.
81+
/// </summary>
82+
/// <returns>Whether or not the instance is the type</returns>
83+
public bool IsCompositionMultifeedBehavior()
84+
{
85+
return ActualInstance.GetType() == typeof(CompositionMultifeedBehavior);
86+
}
3587

3688
/// <summary>
3789
/// Returns the string presentation of the object
3890
/// </summary>
3991
/// <returns>String presentation of the object</returns>
4092
public override string ToString()
4193
{
42-
StringBuilder sb = new StringBuilder();
94+
var sb = new StringBuilder();
4395
sb.Append("class CompositionBehavior {\n");
44-
sb.Append(" Injection: ").Append(Injection).Append("\n");
45-
sb.Append(" Multifeed: ").Append(Multifeed).Append("\n");
96+
sb.Append(" ActualInstance: ").Append(ActualInstance).Append("\n");
4697
sb.Append("}\n");
4798
return sb.ToString();
4899
}
@@ -51,9 +102,9 @@ public override string ToString()
51102
/// Returns the JSON string presentation of the object
52103
/// </summary>
53104
/// <returns>JSON string presentation of the object</returns>
54-
public virtual string ToJson()
105+
public override string ToJson()
55106
{
56-
return JsonSerializer.Serialize(this, JsonConfig.Options);
107+
return JsonSerializer.Serialize(ActualInstance, JsonConfig.Options);
57108
}
58109

59110
/// <summary>
@@ -68,10 +119,7 @@ public override bool Equals(object obj)
68119
return false;
69120
}
70121

71-
return (
72-
Injection == input.Injection || (Injection != null && Injection.Equals(input.Injection))
73-
)
74-
&& (Multifeed == input.Multifeed || (Multifeed != null && Multifeed.Equals(input.Multifeed)));
122+
return ActualInstance.Equals(input.ActualInstance);
75123
}
76124

77125
/// <summary>
@@ -83,15 +131,92 @@ public override int GetHashCode()
83131
unchecked // Overflow is fine, just wrap
84132
{
85133
int hashCode = 41;
86-
if (Injection != null)
134+
if (ActualInstance != null)
135+
hashCode = hashCode * 59 + ActualInstance.GetHashCode();
136+
return hashCode;
137+
}
138+
}
139+
}
140+
141+
/// <summary>
142+
/// Custom JSON converter for CompositionBehavior
143+
/// </summary>
144+
public class CompositionBehaviorJsonConverter : JsonConverter<CompositionBehavior>
145+
{
146+
/// <summary>
147+
/// Check if the object can be converted
148+
/// </summary>
149+
/// <param name="objectType">Object type</param>
150+
/// <returns>True if the object can be converted</returns>
151+
public override bool CanConvert(Type objectType)
152+
{
153+
return objectType == typeof(CompositionBehavior);
154+
}
155+
156+
/// <summary>
157+
/// To convert a JSON string into an object
158+
/// </summary>
159+
/// <param name="reader">JSON reader</param>
160+
/// <param name="typeToConvert">Object type</param>
161+
/// <param name="options">Serializer options</param>
162+
/// <returns>The object converted from the JSON string</returns>
163+
public override CompositionBehavior Read(
164+
ref Utf8JsonReader reader,
165+
Type typeToConvert,
166+
JsonSerializerOptions options
167+
)
168+
{
169+
var jsonDocument = JsonDocument.ParseValue(ref reader);
170+
var root = jsonDocument.RootElement;
171+
if (root.ValueKind == JsonValueKind.Object)
172+
{
173+
try
87174
{
88-
hashCode = (hashCode * 59) + Injection.GetHashCode();
175+
return new CompositionBehavior(
176+
jsonDocument.Deserialize<CompositionInjectionBehavior>(JsonConfig.Options)
177+
);
89178
}
90-
if (Multifeed != null)
179+
catch (Exception exception)
91180
{
92-
hashCode = (hashCode * 59) + Multifeed.GetHashCode();
181+
// deserialization failed, try the next one
182+
System.Diagnostics.Debug.WriteLine(
183+
$"Failed to deserialize into CompositionInjectionBehavior: {exception}"
184+
);
93185
}
94-
return hashCode;
95186
}
187+
if (root.ValueKind == JsonValueKind.Object)
188+
{
189+
try
190+
{
191+
return new CompositionBehavior(
192+
jsonDocument.Deserialize<CompositionMultifeedBehavior>(JsonConfig.Options)
193+
);
194+
}
195+
catch (Exception exception)
196+
{
197+
// deserialization failed, try the next one
198+
System.Diagnostics.Debug.WriteLine(
199+
$"Failed to deserialize into CompositionMultifeedBehavior: {exception}"
200+
);
201+
}
202+
}
203+
throw new InvalidDataException(
204+
$"The JSON string cannot be deserialized into any schema defined."
205+
);
206+
}
207+
208+
/// <summary>
209+
/// To write the JSON string
210+
/// </summary>
211+
/// <param name="writer">JSON writer</param>
212+
/// <param name="value">CompositionBehavior to be converted into a JSON string</param>
213+
/// <param name="options">JSON Serializer options</param>
214+
public override void Write(
215+
Utf8JsonWriter writer,
216+
CompositionBehavior value,
217+
JsonSerializerOptions options
218+
)
219+
{
220+
writer.WriteRawValue(value.ToJson());
96221
}
97222
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
//
2+
// Code generated by OpenAPI Generator (https://openapi-generator.tech), manual changes will be lost - read more on https://github.com/algolia/api-clients-automation. DO NOT EDIT.
3+
//
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Linq;
7+
using System.Text;
8+
using System.Text.Json;
9+
using System.Text.Json.Serialization;
10+
using Algolia.Search.Serializer;
11+
12+
namespace Algolia.Search.Models.Composition;
13+
14+
/// <summary>
15+
/// An object containing an `injection` behavior.
16+
/// </summary>
17+
public partial class CompositionInjectionBehavior
18+
{
19+
/// <summary>
20+
/// Initializes a new instance of the CompositionInjectionBehavior class.
21+
/// </summary>
22+
[JsonConstructor]
23+
public CompositionInjectionBehavior() { }
24+
25+
/// <summary>
26+
/// Initializes a new instance of the CompositionInjectionBehavior class.
27+
/// </summary>
28+
/// <param name="injection">injection (required).</param>
29+
public CompositionInjectionBehavior(Injection injection)
30+
{
31+
Injection = injection ?? throw new ArgumentNullException(nameof(injection));
32+
}
33+
34+
/// <summary>
35+
/// Gets or Sets Injection
36+
/// </summary>
37+
[JsonPropertyName("injection")]
38+
public Injection Injection { get; set; }
39+
40+
/// <summary>
41+
/// Returns the string presentation of the object
42+
/// </summary>
43+
/// <returns>String presentation of the object</returns>
44+
public override string ToString()
45+
{
46+
StringBuilder sb = new StringBuilder();
47+
sb.Append("class CompositionInjectionBehavior {\n");
48+
sb.Append(" Injection: ").Append(Injection).Append("\n");
49+
sb.Append("}\n");
50+
return sb.ToString();
51+
}
52+
53+
/// <summary>
54+
/// Returns the JSON string presentation of the object
55+
/// </summary>
56+
/// <returns>JSON string presentation of the object</returns>
57+
public virtual string ToJson()
58+
{
59+
return JsonSerializer.Serialize(this, JsonConfig.Options);
60+
}
61+
62+
/// <summary>
63+
/// Returns true if objects are equal
64+
/// </summary>
65+
/// <param name="obj">Object to be compared</param>
66+
/// <returns>Boolean</returns>
67+
public override bool Equals(object obj)
68+
{
69+
if (obj is not CompositionInjectionBehavior input)
70+
{
71+
return false;
72+
}
73+
74+
return (
75+
Injection == input.Injection || (Injection != null && Injection.Equals(input.Injection))
76+
);
77+
}
78+
79+
/// <summary>
80+
/// Gets the hash code
81+
/// </summary>
82+
/// <returns>Hash code</returns>
83+
public override int GetHashCode()
84+
{
85+
unchecked // Overflow is fine, just wrap
86+
{
87+
int hashCode = 41;
88+
if (Injection != null)
89+
{
90+
hashCode = (hashCode * 59) + Injection.GetHashCode();
91+
}
92+
return hashCode;
93+
}
94+
}
95+
}

0 commit comments

Comments
 (0)