-
Notifications
You must be signed in to change notification settings - Fork 61
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
[FeatureRequest] Allow customizing the Gson instance for Json serialization #424
Comments
+1 |
Unfortunately, using the workaround that I posted is getting significantly more complicated with JDK12 and higher. Is there an update on when this could be fixed? |
Hi @CrawX, thanks for raising the issue for request this feature. I am trying to see how much benefit this feature will bring to our user. |
I don't consider using a String to be a viable alternative. If you use this with Spring Boot, it feels very awkward and not how you would expect Spring to behave at all. Using a String directly would require all individual users either implement the necessary boilerplate code for serialization themselves, either as a generic abstraction or in each function. The first option causes a lot of room for error and duplication amongst users, the second one feels very awkward. The fact that this is not configurable right now seems to be more a implementation detail / limitation than a conscious design decision. |
I'm fine with waiting a bit more, my priority was to understand if this is still being worked on and to get a rough timeline if possible, thanks for looking into it though! |
Azure/azure-sdk-for-java/issues/30458 seems to be available now. Any update when this can be completed? |
In the reviewing process of the PR, once it is ready will merge it. Should be done in the next one or two weeks. Thank you. |
I don't have a simple testcase ready for this but if I recall correctly, you can just return a class containing a If you can point me in the direction how I can give your PR a try locally using the usual |
@CrawX, sorry for the late reply. Please find the steps to set the new Java worker up on your local.
Let me know if you have any questions. Thanks. |
Hi @kaibocai, thanks for taking the time to PoC this and giving me the above instructions :) I tested it locally, one thing you seem to have omitted is that you need to register the service with the ServiceLoader (follow these docs for example). Then it worked for me! However, this specific ServiceLoader approach is not very compatible with Spring and its beans. We'd have to create the Is there any way this can be refactored into a Cheers |
Hi @CrawX, Thanks for testing it out. Yes, I missed the register step in the above instructions. Sorry for that. In the latest I believe for most customers, besides the control of the serialized process of the response of the invocation request, they also will need control of the deserialized process of how to build their functions' arguments from Grpc metadata. That's the main reason why in the proposed way, I inject the Gson instance far before the first invocation request happens. However, as the above link shows, injecting the Gson instance at such a point I don't really have a way to get the Gson bean from spring context as spring context itself hasn't been initialized yet. I think this probably also needs some updates for Thank you. |
Changing your logic to I'd really love to find a solution for this... |
Hi @CrawX , sorry that I am not quite getting your point, can you draft a simple PR just to explain a little more about your idea? Thank you. |
Hi @kaibocai, sorry for the extended delay. I created two versions: one uses the Supplier and the other one a small interface. The Supplier-one uses a The second one adds a tiny interface over public interface GsonInstance {
<T> T fromJson(String json, Type typeOfT) throws JsonSyntaxException;
String toJson(Object src);
} Both approaches allow the SPI to change the actual serializer while the function is starting. This way, a default I am not sure whether this makes sense as I'm not sure if the spring context will be available before the function arguments are deserialized. Ideally, it would be possible to customize the Gson instance for both serialization and deserialization before they're being used of course. I would very much like to see a way to get this supported when using Azure function with Spring Boot. Since 2.7 is EOL and 3.x requires JDK17, there are no good options left right now... Regards, |
Hi all,
Current problem
The Gson instance used by
RpcUnspecifiedDataTarget
is hardcoded inRpcJsonDataSource
azure-functions-java-worker/src/main/java/com/microsoft/azure/functions/worker/binding/RpcUnspecifiedDataTarget.java
Line 68 in 3ec27b2
azure-functions-java-worker/src/main/java/com/microsoft/azure/functions/worker/binding/RpcJsonDataSource.java
Line 20 in 3ec27b2
This means that customizing the way the runtime Json-serializes objects (especially dates) is impossible. See #419 and this on stackoverflow.
Workaround
I came up with a dirty workaround to replace the used Gson instance with the spring-provided one via Reflection:
AzureGsonConfig.java
:This requires a compile-time dependency like this
(referencing the runtime as a jar file because I couldn't find it in maven central).
Having replaced the Gson instance means you can have a second configuration that customizes the date serialization behaviour of the one from spring.
GsonConfig.java
:While this works, it's not very clean and can break easily.
Alternative
Serialize the objects to strings in the functions or handlers and return them as such. Personally, I don't like this approach.
Feature request
Somehow allow injecting or customizing the Gson instance used in the azure-functions-worker runtime.
The text was updated successfully, but these errors were encountered: