ballerina-platform / ballerina-lang

The Ballerina Programming Language
https://ballerina.io/
Apache License 2.0
3.68k stars 753 forks source link

[Bug]: bal build error when compiling bal bindgen generated code for google oauth2 library #41321

Open yuriylesyuk opened 1 year ago

yuriylesyuk commented 1 year ago

Description

I am successfully creating a bal package for com.google.auth.oauth2.GoogleCredentials from repository:

https://github.com/googleapis/google-auth-library-java

Inside it I successfully run bal bindgen but when I try to compile generated code, I'm getting an error:

Compiling source
    exco/google.oauth2:0.1.0
ERROR [modules/com.google.auth.oauth2/GoogleCredentials.bal:(264:5,268:6)] mismatched function signatures: expected 'public function toBuilder() returns exco/google.oauth2.com.google.auth.oauth2:0.1.0:Builder2', found 'public function toBuilder() returns exco/google.oauth2.com.google.auth.oauth2:0.1.0:Builder'
error: compilation contains errors

The compiler seems to be confused by two inner class definitions invoked in a static and instance contexts:

https://github.com/googleapis/google-auth-library-java/blob/85e38f0a7cf95f48636a4f61c7f1e93a86ba2421/oauth2_http/java/com/google/auth/oauth2/GoogleCredentials.java#L268

  public static Builder newBuilder() {
    return new Builder();
  }

  public Builder toBuilder() {
    return new Builder(this);
  }

In two different classes, each of them contains public static class Builder,

GoogleCredentials.java and OAuth2Credentials.java in the same directory.

as the bal bindgen generates Builder.bal and Builder2.bal ballerina classes for each in the same directory.

Steps to Reproduce

$ bal bindgen -mvn com.google.auth:google-auth-library-oauth2-http:1.19.0 com.google.auth.oauth2.GoogleCredentials

Ballerina package detected at: /ballerina/hw/com-google-auth-auth2

Resolving maven dependencies...

Updated the Ballerina.toml file with the new platform libraries.

The following JARs were added to the classpath: j2objc-annotations-1.3.jar listenablefuture-9999.0-empty-to-avoid-conflict-with-guava.jar google-auth-library-oauth2-http-1.19.0.jar httpclient-4.5.13.jar jsr305-3.0.2.jar failureaccess-1.0.1.jar google-http-client-1.42.3.jar google-http-client-gson-1.42.3.jar gson-2.10.jar auto-value-annotations-1.10.1.jar opencensus-contrib-http-util-0.31.1.jar checker-qual-3.33.0.jar google-auth-library-credentials-1.19.0.jar httpcore-4.4.15.jar guava-32.0.0-android.jar error_prone_annotations-2.16.jar opencensus-api-0.31.1.jar grpc-context-1.27.2.jar commons-logging-1.2.jar commons-codec-1.11.jar

Generating bindings for: com.google.auth.oauth2.GoogleCredentials com.google.auth.oauth2.OAuth2Credentials com.google.auth.Credentials java.lang.Object

Generating dependency bindings for:

bash-3.2$ bal build Resolving Maven dependencies Downloading dependencies into /ballerina/hw/com-google-auth-auth2/target/platform-libs

Compiling source exco/google.oauth2:0.1.0 ERROR [modules/com.google.auth.oauth2/GoogleCredentials.bal:(264:5,268:6)] mismatched function signatures: expected 'public function toBuilder() returns exco/google.oauth2.com.google.auth.oauth2:0.1.0:Builder2', found 'public function toBuilder() returns exco/google.oauth2.com.google.auth.oauth2:0.1.0:Builder' error: compilation contains errors

Affected Version(s)

$ bal --version Ballerina 2201.7.2 (Swan Lake Update 7) Language specification 2023R1 Update Tool 1.3.15

OS, DB, other environment details and versions

No response

Related area

-> Bindgen Tool

Related issue(s) (optional)

No response

Suggested label(s) (optional)

Area/Bindgen

Suggested assignee(s) (optional)

No response

NipunaRanasinghe commented 1 year ago

@yuriylesyuk thanks for reaching out! We were able to reproduce the same behaviour when tried with the latest Ballerina SwanLake version (2201.8.0) and, the issue seems to happen due to a missing type inclusion in one of the generated ballerina binding class from the tool. Therefore, we'll be working on a fix from the tool side to be shipped with an upcoming patch release.

In the meantime, you can manually add the missing type inclusion to the modules/com.google.auth.oauth2.Builder.bal file as follows, which should fix the aforementioned build error.

import ballerina/jballerina.java;

@java:Binding {'class: "com.google.auth.oauth2.GoogleCredentials$Builder"}
public distinct class Builder {
    *Builder2; // Here is the missing type inclusion to be added, as 'com.google.auth.oauth2.GoogleCredentials$Builder' should be extended from 'com.google.auth.oauth2.OAuth2Credentials$Builder'
    *java:JObject;

    public handle jObj;

    # The init function of the Ballerina class mapping the `com.google.auth.oauth2.GoogleCredentials$Builder` Java class.
    #
    # + obj - The `handle` value containing the Java reference of the object.
    public function init(handle obj) {
        self.jObj = obj;
    }

    # The function to retrieve the string representation of the Ballerina class mapping the `com.google.auth.oauth2.GoogleCredentials$Builder` Java class.
    #
    # + return - The `string` form of the Java object instance.
    public function toString() returns string {
        return java:toString(self.jObj) ?: "";
    }
}