Skip to content

Commit

Permalink
fix: Fixed bug when enum contains duplicates.
Browse files Browse the repository at this point in the history
  • Loading branch information
HavenDV committed Nov 2, 2024
1 parent fa32514 commit 2084dd2
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 34 deletions.
1 change: 1 addition & 0 deletions src/libs/AutoSDK/Extensions/OpenApiExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,7 @@ public static Dictionary<string, PropertyData> ComputeEnum(
description: description,
settings))
.Where(value => !string.IsNullOrWhiteSpace(value.Name))
.Distinct()
.ToDictionary(x => x.Id, x => x);

if (values.All(x => x.Value.Name.ToUpperInvariant().Contains(enumName.ToUpperInvariant())))
Expand Down
14 changes: 14 additions & 0 deletions src/libs/AutoSDK/Helpers/EquatableArray{T}.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,15 @@ public bool IsEmpty
get => AsImmutableArray().IsEmpty;
}

/// <summary>
/// Gets a length of the current array.
/// </summary>
public int Length
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => AsImmutableArray().Length;
}

/// <sinheritdoc/>
public bool Equals(EquatableArray<T> other)
{
Expand Down Expand Up @@ -104,6 +113,11 @@ public override int GetHashCode()
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ImmutableArray<T> AsImmutableArray()
{
if (_array is null)
{
return ImmutableArray<T>.Empty;
}

return Unsafe.As<T[]?, ImmutableArray<T>>(ref Unsafe.AsRef(in _array));

Check warning on line 121 in src/libs/AutoSDK/Helpers/EquatableArray{T}.cs

View workflow job for this annotation

GitHub Actions / test-cli (ai21.yaml)

Possible null reference assignment.

Check warning on line 121 in src/libs/AutoSDK/Helpers/EquatableArray{T}.cs

View workflow job for this annotation

GitHub Actions / test-cli (anthropic.yaml)

Possible null reference assignment.

Check warning on line 121 in src/libs/AutoSDK/Helpers/EquatableArray{T}.cs

View workflow job for this annotation

GitHub Actions / test-cli (assemblyai.yaml)

Possible null reference assignment.

Check warning on line 121 in src/libs/AutoSDK/Helpers/EquatableArray{T}.cs

View workflow job for this annotation

GitHub Actions / test-cli (cohere.yaml)

Possible null reference assignment.

Check warning on line 121 in src/libs/AutoSDK/Helpers/EquatableArray{T}.cs

View workflow job for this annotation

GitHub Actions / test-cli (huggingface.yaml)

Possible null reference assignment.

Check warning on line 121 in src/libs/AutoSDK/Helpers/EquatableArray{T}.cs

View workflow job for this annotation

GitHub Actions / test-cli (ipinfo.yaml)

Possible null reference assignment.

Check warning on line 121 in src/libs/AutoSDK/Helpers/EquatableArray{T}.cs

View workflow job for this annotation

GitHub Actions / test-cli (langsmith.json)

Possible null reference assignment.

Check warning on line 121 in src/libs/AutoSDK/Helpers/EquatableArray{T}.cs

View workflow job for this annotation

GitHub Actions / test-cli (leonardo.yaml)

Possible null reference assignment.

Check warning on line 121 in src/libs/AutoSDK/Helpers/EquatableArray{T}.cs

View workflow job for this annotation

GitHub Actions / test-cli (mystic.yaml)

Possible null reference assignment.

Check warning on line 121 in src/libs/AutoSDK/Helpers/EquatableArray{T}.cs

View workflow job for this annotation

GitHub Actions / test-cli (ollama.yaml)

Possible null reference assignment.

Check warning on line 121 in src/libs/AutoSDK/Helpers/EquatableArray{T}.cs

View workflow job for this annotation

GitHub Actions / test-cli (openai.yaml)

Possible null reference assignment.

Check warning on line 121 in src/libs/AutoSDK/Helpers/EquatableArray{T}.cs

View workflow job for this annotation

GitHub Actions / test-cli (petstore.yaml)

Possible null reference assignment.

Check warning on line 121 in src/libs/AutoSDK/Helpers/EquatableArray{T}.cs

View workflow job for this annotation

GitHub Actions / test-cli (replicate.yaml)

Possible null reference assignment.

Check warning on line 121 in src/libs/AutoSDK/Helpers/EquatableArray{T}.cs

View workflow job for this annotation

GitHub Actions / test-cli (special-cases.yaml)

Possible null reference assignment.

Check warning on line 121 in src/libs/AutoSDK/Helpers/EquatableArray{T}.cs

View workflow job for this annotation

GitHub Actions / test-cli (together.yaml)

Possible null reference assignment.

Check warning on line 121 in src/libs/AutoSDK/Helpers/EquatableArray{T}.cs

View workflow job for this annotation

GitHub Actions / test-cli (heygen.yaml)

Possible null reference assignment.

Check warning on line 121 in src/libs/AutoSDK/Helpers/EquatableArray{T}.cs

View workflow job for this annotation

GitHub Actions / test-cli (instill.yaml)

Possible null reference assignment.

Check warning on line 121 in src/libs/AutoSDK/Helpers/EquatableArray{T}.cs

View workflow job for this annotation

GitHub Actions / test-cli (ideogram.yaml)

Possible null reference assignment.

Check warning on line 121 in src/libs/AutoSDK/Helpers/EquatableArray{T}.cs

View workflow job for this annotation

GitHub Actions / test-cli (google-gemini.yaml)

Possible null reference assignment.

Check warning on line 121 in src/libs/AutoSDK/Helpers/EquatableArray{T}.cs

View workflow job for this annotation

GitHub Actions / test-cli (vectara.yaml)

Possible null reference assignment.

Check warning on line 121 in src/libs/AutoSDK/Helpers/EquatableArray{T}.cs

View workflow job for this annotation

GitHub Actions / test-cli (mistral.yaml)

Possible null reference assignment.

Check warning on line 121 in src/libs/AutoSDK/Helpers/EquatableArray{T}.cs

View workflow job for this annotation

GitHub Actions / test-cli (elevenlabs.json)

Possible null reference assignment.

Check warning on line 121 in src/libs/AutoSDK/Helpers/EquatableArray{T}.cs

View workflow job for this annotation

GitHub Actions / test-cli (jina.json)

Possible null reference assignment.

Check warning on line 121 in src/libs/AutoSDK/Helpers/EquatableArray{T}.cs

View workflow job for this annotation

GitHub Actions / test-cli (recraft.yaml)

Possible null reference assignment.

Check warning on line 121 in src/libs/AutoSDK/Helpers/EquatableArray{T}.cs

View workflow job for this annotation

GitHub Actions / Publish / Build, test and publish

Possible null reference assignment.

Check warning on line 121 in src/libs/AutoSDK/Helpers/EquatableArray{T}.cs

View workflow job for this annotation

GitHub Actions / Publish / Build, test and publish

Possible null reference assignment.

Check warning on line 121 in src/libs/AutoSDK/Helpers/EquatableArray{T}.cs

View workflow job for this annotation

GitHub Actions / Publish / Build, test and publish

Possible null reference assignment.

Check warning on line 121 in src/libs/AutoSDK/Helpers/EquatableArray{T}.cs

View workflow job for this annotation

GitHub Actions / Publish / Build, test and publish

Possible null reference assignment.

Check warning on line 121 in src/libs/AutoSDK/Helpers/EquatableArray{T}.cs

View workflow job for this annotation

GitHub Actions / Publish / Build, test and publish

Possible null reference assignment.

Check warning on line 121 in src/libs/AutoSDK/Helpers/EquatableArray{T}.cs

View workflow job for this annotation

GitHub Actions / Publish / Build, test and publish

Possible null reference assignment.

Check warning on line 121 in src/libs/AutoSDK/Helpers/EquatableArray{T}.cs

View workflow job for this annotation

GitHub Actions / Publish / Build, test and publish

Possible null reference assignment.

Check warning on line 121 in src/libs/AutoSDK/Helpers/EquatableArray{T}.cs

View workflow job for this annotation

GitHub Actions / Publish / Build, test and publish

Possible null reference assignment.
}

Expand Down
6 changes: 3 additions & 3 deletions src/libs/AutoSDK/Models/TypeData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ public readonly record struct TypeData(
int AllOfCount,
bool IsComponent,
bool HasDiscriminator,
ImmutableArray<string> Properties,
ImmutableArray<string> EnumValues,
ImmutableArray<TypeData> SubTypes,
EquatableArray<string> Properties,
EquatableArray<string> EnumValues,
EquatableArray<TypeData> SubTypes,
string Namespace,
bool IsDeprecated,
Settings Settings)
Expand Down
63 changes: 32 additions & 31 deletions src/tests/AutoSDK.UnitTests/JsonTests.JsonSerializerContextTypes.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using AutoSDK.Models;
using System.Collections.Immutable;
using AutoSDK.Models;
using static AutoSDK.Serialization.Json.SystemTextJsonSerializer;

namespace AutoSDK.UnitTests;
Expand All @@ -13,97 +14,97 @@ public void ContextTypes()
Namespace = "System.Collections.Generic",
CSharpTypeRaw = "global::System.Collections.Generic.IList<string>",
IsArray = true,
SubTypes = [
SubTypes = ImmutableArray.Create([
TypeData.Default with { CSharpTypeRaw = "string" }
],
]),
}, makeNullableRootIfValueType: true).Should().Be("IListString");

GetContextType(TypeData.Default with
{
Namespace = "System.Collections.Generic",
CSharpTypeRaw = "global::System.Collections.Generic.Dictionary<string, int>",
SubTypes = [
SubTypes = ImmutableArray.Create([
TypeData.Default with { CSharpTypeRaw = "string" },
TypeData.Default with { CSharpTypeRaw = "int" }
],
]),
}, makeNullableRootIfValueType: true).Should().Be("DictionaryStringInt32");

GetContextType(TypeData.Default with
{
Namespace = "System.Collections.Generic",
CSharpTypeRaw = "global::System.Collections.Generic.IList<global::System.Collections.Generic.IList<string>>",
IsArray = true,
SubTypes = [
SubTypes = ImmutableArray.Create([
TypeData.Default with
{
CSharpTypeRaw = "global::System.Collections.Generic.IList<string>",
IsArray = true,
SubTypes = [
SubTypes = ImmutableArray.Create([
TypeData.Default with { CSharpTypeRaw = "string" }
],
]),
}
],
]),
}, makeNullableRootIfValueType: true).Should().Be("IListIListString");

GetContextType(TypeData.Default with
{
Namespace = "System.Collections.Generic",
CSharpTypeRaw = "global::System.Collections.Generic.IList<global::System.Guid>",
IsArray = true,
SubTypes = [
SubTypes = ImmutableArray.Create([
TypeData.Default with {
Namespace = "System",
CSharpTypeRaw = "global::System.Guid" }
],
]),
}, makeNullableRootIfValueType: true).Should().Be("IListGuid");

GetContextType(TypeData.Default with
{
CSharpTypeRaw = "byte[]",
IsBinary = true,
SubTypes = [
SubTypes = ImmutableArray.Create([
TypeData.Default with { CSharpTypeRaw = "byte" }
],
]),
}, makeNullableRootIfValueType: true).Should().Be("ByteArray");

GetContextType(TypeData.Default with
{
CSharpTypeRaw = "byte[][]",
IsBinary = true,
SubTypes = [
SubTypes = ImmutableArray.Create([
TypeData.Default with
{
CSharpTypeRaw = "byte[]",
IsBinary = true,
SubTypes = [
SubTypes = ImmutableArray.Create([
TypeData.Default with { CSharpTypeRaw = "byte" }
],
]),
}
],
]),
}, makeNullableRootIfValueType: true).Should().Be("ByteArrayArray");

GetContextType(TypeData.Default with
{
CSharpTypeRaw = "global::System.Collections.Generic.IList<byte[]>",
IsArray = true,
SubTypes = [
SubTypes = ImmutableArray.Create([
TypeData.Default with
{
CSharpTypeRaw = "byte[]",
IsBinary = true,
SubTypes = [
SubTypes = ImmutableArray.Create([
TypeData.Default with { CSharpTypeRaw = "byte" }
],
]),
}
],
]),
}, makeNullableRootIfValueType: true).Should().Be("IListByteArray");

GetContextType(TypeData.Default with
{
Namespace = "G",
IsValueType = true,
CSharpTypeRaw = "global::G.AllOf<Integration?, AppsCreateFromManifestResponse>",
SubTypes = [
SubTypes = ImmutableArray.Create([
TypeData.Default with
{
CSharpTypeRaw = "Integration",
Expand All @@ -113,15 +114,15 @@ TypeData.Default with
{
CSharpTypeRaw = "AppsCreateFromManifestResponse"
},
],
]),
}, makeNullableRootIfValueType: true).Should().Be("NullableAllOfIntegrationAppsCreateFromManifestResponse");

GetContextType(TypeData.Default with
{
Namespace = "Cohere",
IsValueType = true,
CSharpTypeRaw = "global::Cohere.OneOf<global::Cohere.NonStreamedChatResponse, global::Cohere.StreamedChatResponse?>",
SubTypes = [
SubTypes = ImmutableArray.Create([
TypeData.Default with
{
Namespace = "Cohere",
Expand All @@ -134,7 +135,7 @@ TypeData.Default with
CSharpTypeNullability = true,
IsValueType = true,
}
],
]),
}, makeNullableRootIfValueType: true).Should().Be("NullableOneOfNonStreamedChatResponseNullableStreamedChatResponse");

GetContextType(TypeData.Default with
Expand All @@ -147,23 +148,23 @@ TypeData.Default with
{
CSharpTypeRaw = "global::System.Collections.Generic.IList<byte[][]>",
IsArray = true,
SubTypes = [
SubTypes = ImmutableArray.Create([
TypeData.Default with
{
CSharpTypeRaw = "byte[][]",
IsBinary = true,
SubTypes = [
SubTypes = ImmutableArray.Create([
TypeData.Default with
{
CSharpTypeRaw = "byte[]",
IsBinary = true,
SubTypes = [
SubTypes = ImmutableArray.Create([
TypeData.Default with { CSharpTypeRaw = "byte" }
],
]),
}
],
]),
}
],
]),
}, makeNullableRootIfValueType: true).Should().Be("IListByteArrayArray");
}
}

0 comments on commit 2084dd2

Please sign in to comment.