Skip to content

Commit

Permalink
fix(kotlin): deserialization (#3822) (generated) [skip ci]
Browse files Browse the repository at this point in the history
Co-authored-by: Thomas Raffray <Fluf22@users.noreply.github.com>
  • Loading branch information
algolia-bot and Fluf22 committed Sep 23, 2024
1 parent 4e82751 commit c7c68ff
Show file tree
Hide file tree
Showing 54 changed files with 756 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,20 @@ public partial class RecommendationsHit : AbstractSchema
{
/// <summary>
/// Initializes a new instance of the RecommendationsHit class
/// with a RecommendHit
/// with a TrendingFacetHit
/// </summary>
/// <param name="actualInstance">An instance of RecommendHit.</param>
public RecommendationsHit(RecommendHit actualInstance)
/// <param name="actualInstance">An instance of TrendingFacetHit.</param>
public RecommendationsHit(TrendingFacetHit actualInstance)
{
ActualInstance = actualInstance ?? throw new ArgumentException("Invalid instance found. Must not be null.");
}

/// <summary>
/// Initializes a new instance of the RecommendationsHit class
/// with a TrendingFacetHit
/// with a RecommendHit
/// </summary>
/// <param name="actualInstance">An instance of TrendingFacetHit.</param>
public RecommendationsHit(TrendingFacetHit actualInstance)
/// <param name="actualInstance">An instance of RecommendHit.</param>
public RecommendationsHit(RecommendHit actualInstance)
{
ActualInstance = actualInstance ?? throw new ArgumentException("Invalid instance found. Must not be null.");
}
Expand All @@ -47,42 +47,42 @@ public RecommendationsHit(TrendingFacetHit actualInstance)
public sealed override object ActualInstance { get; set; }

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

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


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

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

/// <summary>
Expand Down Expand Up @@ -169,28 +169,28 @@ public override RecommendationsHit Read(ref Utf8JsonReader reader, Type typeToCo
{
var jsonDocument = JsonDocument.ParseValue(ref reader);
var root = jsonDocument.RootElement;
if (root.ValueKind == JsonValueKind.Object)
if (root.ValueKind == JsonValueKind.Object && root.TryGetProperty("facetName", out _) && root.TryGetProperty("facetValue", out _))
{
try
{
return new RecommendationsHit(jsonDocument.Deserialize<RecommendHit>(JsonConfig.Options));
return new RecommendationsHit(jsonDocument.Deserialize<TrendingFacetHit>(JsonConfig.Options));
}
catch (Exception exception)
{
// deserialization failed, try the next one
System.Diagnostics.Debug.WriteLine($"Failed to deserialize into RecommendHit: {exception}");
System.Diagnostics.Debug.WriteLine($"Failed to deserialize into TrendingFacetHit: {exception}");
}
}
if (root.ValueKind == JsonValueKind.Object)
{
try
{
return new RecommendationsHit(jsonDocument.Deserialize<TrendingFacetHit>(JsonConfig.Options));
return new RecommendationsHit(jsonDocument.Deserialize<RecommendHit>(JsonConfig.Options));
}
catch (Exception exception)
{
// deserialization failed, try the next one
System.Diagnostics.Debug.WriteLine($"Failed to deserialize into TrendingFacetHit: {exception}");
System.Diagnostics.Debug.WriteLine($"Failed to deserialize into RecommendHit: {exception}");
}
}
throw new InvalidDataException($"The JSON string cannot be deserialized into any schema defined.");
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -49,23 +49,23 @@ class Deserializer extends JsonDeserializer<RecommendationsHit> {
@Override
public RecommendationsHit deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
JsonNode tree = jp.readValueAsTree();
// deserialize RecommendHit
if (tree.isObject()) {
// deserialize TrendingFacetHit
if (tree.isObject() && tree.has("facetName") && tree.has("facetValue")) {
try (JsonParser parser = tree.traverse(jp.getCodec())) {
RecommendHit value = parser.readValueAs(RecommendHit.class);
return new RecommendationsHit.RecommendHitWrapper(value);
return parser.readValueAs(TrendingFacetHit.class);
} catch (Exception e) {
// deserialization failed, continue
LOGGER.finest("Failed to deserialize oneOf RecommendHit (error: " + e.getMessage() + ") (type: RecommendHit)");
LOGGER.finest("Failed to deserialize oneOf TrendingFacetHit (error: " + e.getMessage() + ") (type: TrendingFacetHit)");
}
}
// deserialize TrendingFacetHit
// deserialize RecommendHit
if (tree.isObject()) {
try (JsonParser parser = tree.traverse(jp.getCodec())) {
return parser.readValueAs(TrendingFacetHit.class);
RecommendHit value = parser.readValueAs(RecommendHit.class);
return new RecommendationsHit.RecommendHitWrapper(value);
} catch (Exception e) {
// deserialization failed, continue
LOGGER.finest("Failed to deserialize oneOf TrendingFacetHit (error: " + e.getMessage() + ") (type: TrendingFacetHit)");
LOGGER.finest("Failed to deserialize oneOf RecommendHit (error: " + e.getMessage() + ") (type: RecommendHit)");
}
}
throw new AlgoliaRuntimeException(String.format("Failed to deserialize json element: %s", tree));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import kotlinx.serialization.builtins.*
import kotlinx.serialization.descriptors.*
import kotlinx.serialization.encoding.*
import kotlinx.serialization.json.*
import kotlin.jvm.JvmInline

/**
* AddABTestsVariant
Expand All @@ -18,8 +19,22 @@ import kotlinx.serialization.json.*
*/
@Serializable(AddABTestsVariantSerializer::class)
public sealed interface AddABTestsVariant {
@Serializable
@JvmInline
public value class AbTestsVariantSearchParamsValue(public val value: AbTestsVariantSearchParams) : AddABTestsVariant

@Serializable
@JvmInline
public value class AbTestsVariantValue(public val value: AbTestsVariant) : AddABTestsVariant

public companion object {

public fun of(value: AbTestsVariantSearchParams): AddABTestsVariant {
return AbTestsVariantSearchParamsValue(value)
}
public fun of(value: AbTestsVariant): AddABTestsVariant {
return AbTestsVariantValue(value)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import kotlinx.serialization.builtins.*
import kotlinx.serialization.descriptors.*
import kotlinx.serialization.encoding.*
import kotlinx.serialization.json.*
import kotlin.jvm.JvmInline

/**
* GetTopHitsResponse
Expand All @@ -19,8 +20,29 @@ import kotlinx.serialization.json.*
*/
@Serializable(GetTopHitsResponseSerializer::class)
public sealed interface GetTopHitsResponse {
@Serializable
@JvmInline
public value class TopHitsResponseValue(public val value: TopHitsResponse) : GetTopHitsResponse

@Serializable
@JvmInline
public value class TopHitsResponseWithAnalyticsValue(public val value: TopHitsResponseWithAnalytics) : GetTopHitsResponse

@Serializable
@JvmInline
public value class TopHitsResponseWithRevenueAnalyticsValue(public val value: TopHitsResponseWithRevenueAnalytics) : GetTopHitsResponse

public companion object {

public fun of(value: TopHitsResponse): GetTopHitsResponse {
return TopHitsResponseValue(value)
}
public fun of(value: TopHitsResponseWithAnalytics): GetTopHitsResponse {
return TopHitsResponseWithAnalyticsValue(value)
}
public fun of(value: TopHitsResponseWithRevenueAnalytics): GetTopHitsResponse {
return TopHitsResponseWithRevenueAnalyticsValue(value)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import kotlinx.serialization.builtins.*
import kotlinx.serialization.descriptors.*
import kotlinx.serialization.encoding.*
import kotlinx.serialization.json.*
import kotlin.jvm.JvmInline

/**
* GetTopSearchesResponse
Expand All @@ -19,8 +20,29 @@ import kotlinx.serialization.json.*
*/
@Serializable(GetTopSearchesResponseSerializer::class)
public sealed interface GetTopSearchesResponse {
@Serializable
@JvmInline
public value class TopSearchesResponseValue(public val value: TopSearchesResponse) : GetTopSearchesResponse

@Serializable
@JvmInline
public value class TopSearchesResponseWithAnalyticsValue(public val value: TopSearchesResponseWithAnalytics) : GetTopSearchesResponse

@Serializable
@JvmInline
public value class TopSearchesResponseWithRevenueAnalyticsValue(public val value: TopSearchesResponseWithRevenueAnalytics) : GetTopSearchesResponse

public companion object {

public fun of(value: TopSearchesResponse): GetTopSearchesResponse {
return TopSearchesResponseValue(value)
}
public fun of(value: TopSearchesResponseWithAnalytics): GetTopSearchesResponse {
return TopSearchesResponseWithAnalyticsValue(value)
}
public fun of(value: TopSearchesResponseWithRevenueAnalytics): GetTopSearchesResponse {
return TopSearchesResponseWithRevenueAnalyticsValue(value)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import kotlinx.serialization.builtins.*
import kotlinx.serialization.descriptors.*
import kotlinx.serialization.encoding.*
import kotlinx.serialization.json.*
import kotlin.jvm.JvmInline

/**
* AuthInput
Expand All @@ -22,8 +23,50 @@ import kotlinx.serialization.json.*
*/
@Serializable(AuthInputSerializer::class)
public sealed interface AuthInput {
@Serializable
@JvmInline
public value class AuthOAuthValue(public val value: AuthOAuth) : AuthInput

@Serializable
@JvmInline
public value class AuthGoogleServiceAccountValue(public val value: AuthGoogleServiceAccount) : AuthInput

@Serializable
@JvmInline
public value class AuthBasicValue(public val value: AuthBasic) : AuthInput

@Serializable
@JvmInline
public value class AuthAPIKeyValue(public val value: AuthAPIKey) : AuthInput

@Serializable
@JvmInline
public value class AuthAlgoliaValue(public val value: AuthAlgolia) : AuthInput

@Serializable
@JvmInline
public value class AuthAlgoliaInsightsValue(public val value: AuthAlgoliaInsights) : AuthInput

public companion object {

public fun of(value: AuthOAuth): AuthInput {
return AuthOAuthValue(value)
}
public fun of(value: AuthGoogleServiceAccount): AuthInput {
return AuthGoogleServiceAccountValue(value)
}
public fun of(value: AuthBasic): AuthInput {
return AuthBasicValue(value)
}
public fun of(value: AuthAPIKey): AuthInput {
return AuthAPIKeyValue(value)
}
public fun of(value: AuthAlgolia): AuthInput {
return AuthAlgoliaValue(value)
}
public fun of(value: AuthAlgoliaInsights): AuthInput {
return AuthAlgoliaInsightsValue(value)
}
}
}

Expand Down
Loading

0 comments on commit c7c68ff

Please sign in to comment.