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

[chore] Remove reflection usage from KiotaSerialization #957

Merged
merged 2 commits into from
Jan 18, 2024

Conversation

andreaTP
Copy link
Contributor

@andreaTP andreaTP commented Jan 3, 2024

I started picking up #923 again.
Before opening Java to GA I think it makes sense to remove those reflective methods, they look convenient, but I think that a cleaner smaller API is the way to go to start with.

Also, this will prevent accidental usage of reflection on platforms that do not support it completely (like GraalVM native-image).

In case we receive feedback and ask to re-introduce those helpers we can re-evaluate.

Copy link
Contributor

github-actions bot commented Jan 4, 2024

This pull request has conflicting changes, the author must resolve the conflicts before this pull request can be merged.

@andreaTP andreaTP force-pushed the rem-reflect-kiota-ser branch from f72852a to 9500d3d Compare January 4, 2024 11:03
Copy link
Contributor

github-actions bot commented Jan 6, 2024

Conflicts have been resolved. A maintainer will take a look shortly.

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.
The reflection option was requested by @sebastienlevert in the specification so people don't have to provide the factory method. It's a shorthand for simplicity and since there are overloads accepting the method I don't think it hurts to have those.
They are not used by the generated code.

@andreaTP
Copy link
Contributor Author

andreaTP commented Jan 8, 2024

Thanks for the context @baywet !

It's a shorthand for simplicity

If this is the desired outcome I'm convinced they should be removed.

Although at first look they provide a "nice to have" API which is slightly nicer than the plain one, the risk of invoking one of those methods and breaking a GraalVM native-image build is extremely high, especially when the error comes from a transitive dependency.

Debugging and understanding the issues with a native-image build is, at times, extremely challenging and I would give this gun to the users only as a deliberate choice and not masked behind a simple "convenience method".

They are not used by the generated code.

Correct, and it's very very little tested too ...

If, after these motivations, you still feel strongly about keeping the methods, I want to propose, at least, moving them to a location where the usage of reflection is very very clear, for example changing the package name:

com.microsoft.kiota.serialization.KiotaJsonSerialization -> com.microsoft.kiota.serialization.reflective.KiotaJsonSerialization

I'm sorry to push a little hard on this, but I have spent quite enough hours of my life debugging those issues 😢.
I settled on these rules of thumb:

  • avoid reflection at all costs
  • if you really need to use it, make it very explicit where and how it's used

@baywet
Copy link
Member

baywet commented Jan 8, 2024

These are most of the arguments I gave to @sebastienlevert + reliability/performance. I'll let you folks discuss that since I'm already in the camp of "we should not be doing any reflection"

@andreaTP
Copy link
Contributor Author

andreaTP commented Jan 8, 2024

Got it 👍
Personally, the only "unusual" detail in the reflection-free API is the name of the method IMHO: createFromDiscriminatorValue which is rather alien terminology for Java developers.

The resulting API looks cleaner with a more "idiomatic" name like:

KiotaSerialization.deserialize(jsonContentType, strValue, TestEntity::deserializer);

Copy link
Contributor

github-actions bot commented Jan 9, 2024

This pull request has conflicting changes, the author must resolve the conflicts before this pull request can be merged.

@sebastienlevert
Copy link

In this case @andreaTP if we were to remove the reflection versions of these APIs, would the only thing left on your mind be the deserializer vs. createFromDiscriminatorValue? That even makes me think that this "wording" might be better in other languages but would impact our GA languages. @baywet any opinions on renaming this factory?

@andreaTP
Copy link
Contributor Author

if we were to remove the reflection versions of these APIs

I would say that no further actions are required around reflection, all the rest falls in the category of "nice to have".

@sebastienlevert
Copy link

So current state is OK? Just to make sure I understand correctly?

@andreaTP
Copy link
Contributor Author

Correct, the current status is OK and Kiota is compatible with native-image already.

To avoid introducing breaking changes after GA, I warmly encourage you to approve this PR, and we can move on from there.

@andreaTP
Copy link
Contributor Author

Any update? Should I rebase or this is not going to go in?

@sebastienlevert
Copy link

This should go in. I agree with your concerns and better to avoid reflection in this case.

@baywet make your magic happen 😁

@andreaTP andreaTP force-pushed the rem-reflect-kiota-ser branch from 9500d3d to 87dda24 Compare January 18, 2024 12:02
@baywet
Copy link
Member

baywet commented Jan 18, 2024

Just a heads up on that @sebastienlevert : this means that Java will be different from dotnet. But similar to Go/TS. But I'll proceed. Thanks everyone for the discussion.

@baywet baywet enabled auto-merge January 18, 2024 12:29
@baywet baywet merged commit a28ee7e into microsoft:main Jan 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants