Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect serialization Code is Generated For Extensible Enums of Union Type #5516

Open
jorgerangel-msft opened this issue Jan 7, 2025 · 1 comment
Labels
emitter:client:csharp Issue for the C# client emitter: @typespec/http-client-csharp

Comments

@jorgerangel-msft
Copy link
Contributor

jorgerangel-msft commented Jan 7, 2025

When an union model property type is customized to an extensible enum of more than 1 type, ie public readonly partial struct HyperparameterCycleCount : IEquatable<int>, IEquatable<string> the underlying model is generating deserialization code that doesn't compile.

Image.

Similarly, the serialization code is generated incorrectly.

Sample spec: https://github.com/mikhail/openai-in-typespec/blob/c22818b1e6d61ffde44d19501e0ebacb13559eb9/.typespec/fine-tuning/custom.tsp#L5.
Sample generated code: https://github.com/mikhail/openai-in-typespec/blob/c22818b1e6d61ffde44d19501e0ebacb13559eb9/.dotnet/src/Generated/Models/HyperparameterOptions.Serialization.cs#L97

@jorgerangel-msft jorgerangel-msft added the emitter:client:csharp Issue for the C# client emitter: @typespec/http-client-csharp label Jan 7, 2025
@jorgerangel-msft
Copy link
Contributor Author

jorgerangel-msft commented Jan 7, 2025

Possible solution: Check the custom enum type to see what union value types are supported to generate a deserialization block like so:

HyperparameterCycleCount cycleCount = default;
if (prop.NameEquals("n_epochs"u8))
{
    if (prop.Value.ValueKind == JsonValueKind.Null)
    {
        cycleCount = null;
    }
    else if (prop.Value.ValueKind == JsonValueKind.String)
    {
        cycleCount = new HyperparameterCycleCount(prop.Value.GetString());
    }
    else
    {
        cycleCount = new HyperparameterCycleCount(prop.Value.GetInt32());
    }
 
    continue;
}

cc: @JoshLove-msft

@jorgerangel-msft jorgerangel-msft changed the title Incorrect Deserialization Code is Generated For Extensible Enums of Union Type Incorrect serialization Code is Generated For Extensible Enums of Union Type Jan 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
emitter:client:csharp Issue for the C# client emitter: @typespec/http-client-csharp
Projects
None yet
Development

No branches or pull requests

1 participant