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

Add constant OkHttp.VERSION #5981

Merged
merged 5 commits into from
May 7, 2020

Conversation

Kevinrob
Copy link
Contributor

Related to issue #5969

Since OkHttp 4.0.0, the okhttp3.internal package is not a published API.
It's impossible to get default userAgent without access to this.

As @yschimke suggest, we can move Version from package okhttp3.internal to okhttp3.

@yschimke
Copy link
Collaborator

As @yschimke suggest, we can move Version from package okhttp3.internal to okhttp3.

By "Java package implementation version" I meant whether the right fix is to make sure this works

    OkHttpClient::class.java.`package`.implementationVersion

And not have a new Version class

@Kevinrob
Copy link
Contributor Author

As @yschimke suggest, we can move Version from package okhttp3.internal to okhttp3.

By "Java package implementation version" I meant whether the right fix is to make sure this works

    OkHttpClient::class.java.`package`.implementationVersion

And not have a new Version class

I'm not sure to understand 😅
Here, I just changed the Java package of the class Version. I'm not adding a new Version class 🙂

@yschimke
Copy link
Collaborator

Yep, rather than new API, maybe it's better to expose the version number using existing mechanics.

https://docs.oracle.com/javase/tutorial/deployment/jar/packageman.html

Anyway, I'll let others weigh in. I wouldn't change yet. The danger of this new API is it encourages people to use it, which we may not want to encourage.

@Kevinrob
Copy link
Contributor Author

Yep, rather than new API, maybe it's better to expose the version number using existing mechanics.

https://docs.oracle.com/javase/tutorial/deployment/jar/packageman.html

Anyway, I'll let others weigh in. I wouldn't change yet. The danger of this new API is it encourages people to use it, which we may not want to encourage.

Okay, I see 👍
But I think that it's a good thing to be able to get default userAgent. If someone need to change it and going back to default for example.

@yschimke
Copy link
Collaborator

yschimke commented Apr 25, 2020

Was hoping it was this simple in the build.gradle for okhttp jar, but not working.

jar {
  manifest {
    attributes('Automatic-Module-Name': 'okhttp3', 'Implementation-Version': project.version)
  }
}

So maybe this PR is the right approach?

@yschimke
Copy link
Collaborator

yschimke commented Apr 28, 2020

I'm not sold on just exposing this previously internal method as public API. For example the Conscrypt version as a public API is usable. https://github.com/google/conscrypt/blob/ce2fbf2d740437655c7774c4c0cd3c3f8ee159f9/common/src/main/java/org/conscrypt/Conscrypt.java#L58

    public static class Version {
        private final int major;
        private final int minor;
        private final int patch;

        private Version(int major, int minor, int patch) {
            this.major = major;
            this.minor = minor;
            this.patch = patch;
        }

        public int major() { return major; }
        public int minor() { return minor; }
        public int patch() { return patch; }
    }

    private static final Version VERSION;

    static {
        int major = -1;
        int minor = -1;
        int patch = -1;
        InputStream stream = null;
        try {
            stream = Conscrypt.class.getResourceAsStream("conscrypt.properties");
            if (stream != null) {
                Properties props = new Properties();
                props.load(stream);
                major = Integer.parseInt(props.getProperty("org.conscrypt.version.major", "-1"));
                minor = Integer.parseInt(props.getProperty("org.conscrypt.version.minor", "-1"));
                patch = Integer.parseInt(props.getProperty("org.conscrypt.version.patch", "-1"));
            }
        } catch (IOException e) {
        } finally {
            IoUtils.closeQuietly(stream);
        }
        if ((major >= 0) && (minor >= 0) && (patch >= 0)) {
            VERSION = new Version(major, minor, patch);
        } else {
            VERSION = null;
        }
    }

For now, I think building a properties file or including in the Jar manifest, is better than just uplifting this API.

n.b. I think JAR versioning is broken in Android

Conscrypt::class.java.`package`?.implementationVersion
# 0.0

@swankjesse
Copy link
Member

Shall we create a version constant? We can say that we do semver and sufficiently motivated callers can use the Maven rules to parse it.

OkHttp.VERSION

@yschimke
Copy link
Collaborator

yschimke commented May 3, 2020

Shall we create a version constant? We can say that we do semver and sufficiently motivated callers can use the Maven rules to parse it.

OkHttp.VERSION

Are there any subtleties to that? If we just change the @ filename annotation in a Version.kt file, does that grab this top level file class forever?

@swankjesse
Copy link
Member

Or maybe the file name is OkHttp.kt and it's a single const field.

My preference is we document the version being strictly confirmant to semver 2.0.0. We will need to be careful to avoid version names where Maven and Semver disagree.

@swankjesse
Copy link
Member

For anyone following along curious about where Maven and Semver disagree on order:
https://publicobject.com/2019/12/18/naming-versions/

@yschimke
Copy link
Collaborator

yschimke commented May 4, 2020

@Kevinrob Thanks for your patience on this, does this version field suit your needs? Care to update the PR?

@Kevinrob Kevinrob force-pushed the 5969_open_Version_userAgent branch from 98b204b to 2919d6e Compare May 5, 2020 21:51
@Kevinrob
Copy link
Contributor Author

Kevinrob commented May 5, 2020

@Kevinrob Thanks for your patience on this, does this version field suit your needs? Care to update the PR?

@yschimke Yes. I updated the PR with @swankjesse proposition 👍

@Kevinrob Kevinrob changed the title Move Version from package okhttp3.internal to okhttp3 Add constant OkHttp.VERSION May 7, 2020
@yschimke
Copy link
Collaborator

yschimke commented May 7, 2020

@yschimke Yes. I updated the PR with @swankjesse proposition 👍

Looks good, I think it's worth a test because of the build dependency. Would you add a single test for this, starts with "4." Or whatever you think makes sense.

@yschimke
Copy link
Collaborator

yschimke commented May 7, 2020

* What went wrong:

Execution failed for task ':okhttp:spotlessKotlin'.

> The following files had format violations:
      okhttp/src/test/java/okhttp3/OkHttpTest.kt
          @@ -8,4 +8,4 @@
           ········val·semVerRegex·=·Regex("^[0-9]+\\.[0-9]+\\.[0-9]+(-.+)?$")
           ········assert(semVerRegex.matches(VERSION))
           ····}
          -}
          +}
  Run 'gradlew spotlessApply' to fix these violations.

@Test
fun testVersion() {
val semVerRegex = Regex("^[0-9]+\\.[0-9]+\\.[0-9]+(-.+)?$")
assert(semVerRegex.matches(VERSION))
Copy link
Collaborator

Choose a reason for hiding this comment

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

instead of java assertions, can you replace with assertj?

import org.assertj.core.api.Assertions.assertThat
...
assertThat(VERSION).matches("[0-9]+\\.[0-9]+\\.[0-9]+(-.+)?")

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, changed ✔

@yschimke
Copy link
Collaborator

yschimke commented May 7, 2020

@Kevinrob sorry for being finicky, and thanks for working through this.

@Kevinrob
Copy link
Contributor Author

Kevinrob commented May 7, 2020

sorry for being finicky, and thanks for working through this.

@yschimke No problem 🙂

@yschimke yschimke merged commit 87c57ec into square:master May 7, 2020
@Kevinrob Kevinrob deleted the 5969_open_Version_userAgent branch May 7, 2020 19:39
@Kevinrob
Copy link
Contributor Author

Kevinrob commented May 7, 2020

Thank you @yschimke for merging this PR!
Is OkHttp project accept contribution about compilation warning correction?

@yschimke
Copy link
Collaborator

yschimke commented May 7, 2020

If it's a clear improvement to the code, but not if it's just to appease a lint rule/warning that we don't generally agree with. We don't actively sprinkle code with annotations to appease Intellij.

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.

None yet

4 participants