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

fix: wrapper types for intersection types should not be serialized as objects #97

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

ricardoboss
Copy link
Contributor

This fixes an edge case I have seen with the OpenAI API spec.

Some models have an intersection type property. This causes a wrapper type to be generated that holds a property for each possible type in the intersection.

Currently, the serializer writes this wrapper type as its own object. The correct serialization would "flatten" this type, so only its value remains.

@ricardoboss ricardoboss requested a review from a team as a code owner March 5, 2025 00:37
@@ -178,10 +178,15 @@ class JsonSerializationWriter implements SerializationWriter {
}
if (key?.isNotEmpty ?? false) {
final objectContents = {..._contents};
dynamic value = objectContents;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I understand why we need this here?
If we look at the dotnet implementation (or other languages), it doesn't have such a flattening logic.
To me, it seems that the generated serialization code might be incorrect and not doing the appropriate routing.

Can you please share the schema?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, see, the dotnet abstractions have an IComposedTypeWrapper and does serialization logic differently based on that. We don't have that yet, but we can align the Dart implementation with the C# impl if you want

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please go ahead!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I checked both approaches and, at least to me, this seems to be the best way. Generation looks good, .NET just uses its types to do this kind of "unwrapping". In the end it results in the same JSON, so I think we're good now

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd really like the implementations/generated code to be as aligned as possible between the different languages to ease up troubleshooting and other aspects if you don't mind?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We gain no benefit from this. Sure we can add the interface as a marker to those types, but it makes no difference in the serialization logic.

@ricardoboss
Copy link
Contributor Author

ricardoboss commented Mar 5, 2025

I also think this is related to #95. @wisamidris77 can you check if the changes in this branch also fix your issue?

@ricardoboss ricardoboss force-pushed the fixes/nested-intersection-type-serialization branch from c1a33c3 to f4d9a33 Compare March 5, 2025 19:39
@ricardoboss
Copy link
Contributor Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants