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

Gson annotation is ignored #400

Closed
TsuyoshiUshio opened this issue Sep 11, 2020 · 2 comments
Closed

Gson annotation is ignored #400

TsuyoshiUshio opened this issue Sep 11, 2020 · 2 comments
Labels

Comments

@TsuyoshiUshio
Copy link
Contributor

TsuyoshiUshio commented Sep 11, 2020

Repro steps

Provide the steps required to reproduce the problem:

  1. Create this Azure Functions

UserTestObject.java

package com.fabrikam;

import com.google.gson.annotations.SerializedName;

class UserTestObject {
    @SerializedName("user_id") private String prop1 = null;
    @SerializedName("first_name") private String prop2 = null;
    @SerializedName("last_name") private String prop3 = null;
    @SerializedName("e_mail") private String prop4 = null;

    public void setUserId(String userId) {
        this.prop1 = userId;
    }
    public void setFirstName(String firstName) {
        this.prop2 = firstName;
    }
    public void setLastName(String lastName) {
        this.prop3 = lastName;
    }
    public void setEmail(String eMail){
        this.prop4 = eMail;
    }
}

Function.java

package com.fabrikam;

import com.microsoft.azure.functions.ExecutionContext;
import com.microsoft.azure.functions.HttpMethod;
import com.microsoft.azure.functions.HttpRequestMessage;
import com.microsoft.azure.functions.HttpResponseMessage;
import com.microsoft.azure.functions.HttpStatus;
import com.microsoft.azure.functions.annotation.AuthorizationLevel;
import com.microsoft.azure.functions.annotation.FunctionName;
import com.microsoft.azure.functions.annotation.HttpTrigger;

import java.util.Optional;

/**
 * Azure Functions with HTTP Trigger.
 */
public class Function {
    /**
     * This function listens at endpoint "/api/HttpExample". Two ways to invoke it using "curl" command in bash:
     * 1. curl -d "HTTP Body" {your host}/api/HttpExample
     * 2. curl "{your host}/api/HttpExample?name=HTTP%20Query"
     */
    @FunctionName("HttpExample")
    public HttpResponseMessage run(
            @HttpTrigger(
                name = "req",
                methods = {HttpMethod.GET, HttpMethod.POST},
                authLevel = AuthorizationLevel.ANONYMOUS)
                HttpRequestMessage<Optional<String>> request,
            final ExecutionContext context) {
        context.getLogger().info("Java HTTP trigger processed a request.");
        UserTestObject user = new UserTestObject();
        user.setUserId("ABCDE");
        user.setFirstName("Microsoft");
        user.setLastName("Test");
        user.setEmail("Test@Test.Test");

        HttpResponseMessage.Builder builder = request.createResponseBuilder(HttpStatus.OK);
        builder.body(user);
        return builder.build();
    }
}
  1. Run with the Azure Functions Java Worker with 1.7.1+ release that has shading for all libraries.

  2. Send a http request.

However, after a recent Runtime update, it is now returning. (Results from the Function App on cloud with the latest Runtime)

{
"prop1": "ABCDE",
"prop2": "Microsoft",
"prop3": "Test",
"prop4": "Test@Test.Test"
}


#### Expected behavior

```json
{
  "user_id": "ABCDE",
  "first_name": "Microsoft",
  "last_name": "Test",
  "e_mail": "Test@Test.Test"
}

Actual behavior

{
  "prop1": "ABCDE",
  "prop2": "Microsoft",
  "prop3": "Test",
  "prop4": "Test@Test.Test"
}

Known workarounds

Pin back to the The old version. For example,

az webapp config set -g YOUR_RESOURCE_GROUP -n YOUR_FUNCTION_APP_NAME --linux-fx-version "DOCKER|mcr.microsoft.com/azure-functions/java8:3.0.14191-java8-appservice"

Related information

The root cause is, we introduced shading to avoid conflict of the libraries.
https://github.com/Azure/azure-functions-java-worker/releases/tag/1.7.1

In this case, the @SerializedName is ignored. The reason is, the java worker using shading Gson that package name is com.microsoft.azure.functions.shaded.com.google.gson.annotations. However, Gson on the customer side package is com.google.gson.annotations that is why the annotation is ignored.

Set the break point here, you can find the gson is shaded. However, target class is not shaded. That is why the annotation is ignored.
https://github.com/Azure/azure-functions-java-worker/blob/dev/src/main/java/com/microsoft/azure/functions/worker/binding/RpcUnspecifiedDataTarget.java#L68

This PR will fix this issue.
#399

@amamounelsayed
Copy link
Contributor

The fix had been rolled out for both linux and windows.
Fix run time host version 3.0.14916 or higher.

@amamounelsayed
Copy link
Contributor

Related work: #559

@amamounelsayed amamounelsayed unpinned this issue Apr 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants