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

Fixes the union deserializer code generation in Go #5618

Merged
merged 3 commits into from
Oct 16, 2024

Conversation

nickfloyd
Copy link
Contributor

Fixes: #4844

What

When generating Go based SDKs from GitHub's OpenAPI definitions we noticed that the GetFieldDeserializers function was empty for any operations using discriminators and OneOf

Why

It turns out that the issue is in WriteDeserializerBodyForUnionModel or at least why the method body is not getting generated.
So the LINQ statement for Go is:

var otherPropGetters = parentClass
 .GetPropertiesOfKind(CodePropertyKind.Custom)
 .Where(static x => !x.ExistsInBaseType && x.Getter != null)
 .Where(static x => x.Type is CodeType propertyType && !propertyType.IsCollection && propertyType.TypeDefinition is CodeClass)
 .OrderBy(static x => x, CodePropertyTypeForwardComparer)
 .ThenBy(static x => x.Name)
 .Select(static x => x.Getter!.Name.ToFirstCharacterUpperCase())
 .ToArray();

The problem is that for these discriminators the TypeDefinition is not of type CodeClass but of type CodeInterface
When the LINQ is rewritten like this, it works:

var otherPropGetters = parentClass
 .GetPropertiesOfKind(CodePropertyKind.Custom)
 .Where(static x => !x.ExistsInBaseType && x.Getter != null)
 .Where(static x => x.Type is CodeType propertyType && !propertyType.IsCollection && propertyType.TypeDefinition is CodeInterface)
 .OrderBy(static x => x, CodePropertyTypeForwardComparer)
 .ThenBy(static x => x.Name)
 .Select(static x => x.Getter!.Name.ToFirstCharacterUpperCase())
 .ToArray();
  • Tests have been updated
  • Change log has been updated
  • Verified to be working using GitHub's OpenAPI definitions

@nickfloyd nickfloyd requested a review from a team as a code owner October 16, 2024 21:37
@nickfloyd
Copy link
Contributor Author

@baywet we have question regarding the inner workings of kiota to help us have a better understanding on things related to this PR if you have time:

Do union, intersection, and inheritance deserializers map to OneOf, AnyOf, and AllOf usage in OpenAPI? Based on some preliminary testing there does seem to be a correlation. If they do not, then what do union, intersection, and inheritance represent here?

Also, there are other instances of CodeClass being used in CodeMethodWriter. Instances here and here. Modifying these cause fairly significant changes in the code that is generated (according to the unit tests).

Our question is, is there a way to definitively determine what the expected output should be? Since the delta is fairly large here this tells me that either the current source that is generated pre-fix (converting CodeClass to CodeInterface) is currently not working or I am approaching the problem wrong - which my guess is the actual issue here.

Any guidance would be appreciated. Also, let me know what your thoughts are around landing this change, if you approve of it, and then working on the other areas in a separate PR.

Copy link
Member

@baywet baywet left a comment

Choose a reason for hiding this comment

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

Thanks for the contribution!

@baywet baywet merged commit f124023 into microsoft:main Oct 16, 2024
208 checks passed
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.

Null results for GitHub users GET request
2 participants