Skip to content

Commit

Permalink
fix(specs): built-in ops accept also int (generated)
Browse files Browse the repository at this point in the history
algolia/api-clients-automation#3450

Co-authored-by: algolia-bot <accounts+algolia-api-client-bot@algolia.com>
Co-authored-by: Kai Welke <kai.welke@algolia.com>
Co-authored-by: Pierre Millot <pierre.millot@algolia.com>
  • Loading branch information
3 people committed Jul 31, 2024
1 parent 3e03ebd commit c83d8bd
Show file tree
Hide file tree
Showing 2 changed files with 214 additions and 5 deletions.
9 changes: 4 additions & 5 deletions algoliasearch/Models/Search/BuiltInOperation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,18 @@ public BuiltInOperation() { }
/// Initializes a new instance of the BuiltInOperation class.
/// </summary>
/// <param name="operation">operation (required).</param>
/// <param name="value">Value that corresponds to the operation, for example an &#x60;Increment&#x60; or &#x60;Decrement&#x60; step, or an &#x60;Add&#x60; or &#x60;Remove&#x60; value. (required).</param>
public BuiltInOperation(BuiltInOperationType? operation, string value)
/// <param name="value">value (required).</param>
public BuiltInOperation(BuiltInOperationType? operation, BuiltInOperationValue value)
{
Operation = operation;
Value = value ?? throw new ArgumentNullException(nameof(value));
}

/// <summary>
/// Value that corresponds to the operation, for example an `Increment` or `Decrement` step, or an `Add` or `Remove` value.
/// Gets or Sets Value
/// </summary>
/// <value>Value that corresponds to the operation, for example an `Increment` or `Decrement` step, or an `Add` or `Remove` value.</value>
[JsonPropertyName("value")]
public string Value { get; set; }
public BuiltInOperationValue Value { get; set; }

/// <summary>
/// Returns the string presentation of the object
Expand Down
210 changes: 210 additions & 0 deletions algoliasearch/Models/Search/BuiltInOperationValue.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
//
// 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.
//
using System;
using System.Text;
using System.Linq;
using System.Text.Json.Serialization;
using System.Collections.Generic;
using Algolia.Search.Serializer;
using System.Text.Json;
using System.IO;
using System.Reflection;
using Algolia.Search.Models.Common;

namespace Algolia.Search.Models.Search;

/// <summary>
/// BuiltInOperationValue
/// </summary>
[JsonConverter(typeof(BuiltInOperationValueJsonConverter))]
public partial class BuiltInOperationValue : AbstractSchema
{
/// <summary>
/// Initializes a new instance of the BuiltInOperationValue class
/// with a string
/// </summary>
/// <param name="actualInstance">An instance of string.</param>
public BuiltInOperationValue(string actualInstance)
{
ActualInstance = actualInstance ?? throw new ArgumentException("Invalid instance found. Must not be null.");
}

/// <summary>
/// Initializes a new instance of the BuiltInOperationValue class
/// with a int
/// </summary>
/// <param name="actualInstance">An instance of int.</param>
public BuiltInOperationValue(int actualInstance)
{
ActualInstance = actualInstance;
}


/// <summary>
/// Gets or Sets ActualInstance
/// </summary>
public sealed override object ActualInstance { get; set; }

/// <summary>
/// Get the actual instance of `string`. If the actual instance is not `string`,
/// the InvalidClassException will be thrown
/// </summary>
/// <returns>An instance of string</returns>
public string AsString()
{
return (string)ActualInstance;
}

/// <summary>
/// Get the actual instance of `int`. If the actual instance is not `int`,
/// the InvalidClassException will be thrown
/// </summary>
/// <returns>An instance of int</returns>
public int AsInt()
{
return (int)ActualInstance;
}


/// <summary>
/// Check if the actual instance is of `string` type.
/// </summary>
/// <returns>Whether or not the instance is the type</returns>
public bool IsString()
{
return ActualInstance.GetType() == typeof(string);
}

/// <summary>
/// Check if the actual instance is of `int` type.
/// </summary>
/// <returns>Whether or not the instance is the type</returns>
public bool IsInt()
{
return ActualInstance.GetType() == typeof(int);
}

/// <summary>
/// Returns the string presentation of the object
/// </summary>
/// <returns>String presentation of the object</returns>
public override string ToString()
{
var sb = new StringBuilder();
sb.Append("class BuiltInOperationValue {\n");
sb.Append(" ActualInstance: ").Append(ActualInstance).Append("\n");
sb.Append("}\n");
return sb.ToString();
}

/// <summary>
/// Returns the JSON string presentation of the object
/// </summary>
/// <returns>JSON string presentation of the object</returns>
public override string ToJson()
{
return JsonSerializer.Serialize(ActualInstance, JsonConfig.Options);
}

/// <summary>
/// Returns true if objects are equal
/// </summary>
/// <param name="obj">Object to be compared</param>
/// <returns>Boolean</returns>
public override bool Equals(object obj)
{
if (obj is not BuiltInOperationValue input)
{
return false;
}

return ActualInstance.Equals(input.ActualInstance);
}

/// <summary>
/// Gets the hash code
/// </summary>
/// <returns>Hash code</returns>
public override int GetHashCode()
{
unchecked // Overflow is fine, just wrap
{
int hashCode = 41;
if (ActualInstance != null)
hashCode = hashCode * 59 + ActualInstance.GetHashCode();
return hashCode;
}
}
}





/// <summary>
/// Custom JSON converter for BuiltInOperationValue
/// </summary>
public class BuiltInOperationValueJsonConverter : JsonConverter<BuiltInOperationValue>
{

/// <summary>
/// Check if the object can be converted
/// </summary>
/// <param name="objectType">Object type</param>
/// <returns>True if the object can be converted</returns>
public override bool CanConvert(Type objectType)
{
return objectType == typeof(BuiltInOperationValue);
}

/// <summary>
/// To convert a JSON string into an object
/// </summary>
/// <param name="reader">JSON reader</param>
/// <param name="typeToConvert">Object type</param>
/// <param name="options">Serializer options</param>
/// <returns>The object converted from the JSON string</returns>
public override BuiltInOperationValue Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var jsonDocument = JsonDocument.ParseValue(ref reader);
var root = jsonDocument.RootElement;
if (root.ValueKind == JsonValueKind.String)
{
try
{
return new BuiltInOperationValue(jsonDocument.Deserialize<string>(JsonConfig.Options));
}
catch (Exception exception)
{
// deserialization failed, try the next one
System.Diagnostics.Debug.WriteLine($"Failed to deserialize into string: {exception}");
}
}
if (root.ValueKind == JsonValueKind.Number)
{
try
{
return new BuiltInOperationValue(jsonDocument.Deserialize<int>(JsonConfig.Options));
}
catch (Exception exception)
{
// deserialization failed, try the next one
System.Diagnostics.Debug.WriteLine($"Failed to deserialize into int: {exception}");
}
}
throw new InvalidDataException($"The JSON string cannot be deserialized into any schema defined.");
}

/// <summary>
/// To write the JSON string
/// </summary>
/// <param name="writer">JSON writer</param>
/// <param name="value">BuiltInOperationValue to be converted into a JSON string</param>
/// <param name="options">JSON Serializer options</param>
public override void Write(Utf8JsonWriter writer, BuiltInOperationValue value, JsonSerializerOptions options)
{
writer.WriteRawValue(value.ToJson());
}
}

0 comments on commit c83d8bd

Please sign in to comment.