<file_sep>package com.superexercisebook.bugreport.okhttp.framework;

import okhttp3.*;

import java.io.IOException;
import java.util.Objects;


public class JavaHttpClient {
    public String test(String s) {
        OkHttpClient client = new OkHttpClient();
        String url = "https://one.one.one.one/cdn-cgi/trace";
        RequestBody body = RequestBody.create(MediaType.parse("plain/text; charset=utf-8"), s);
        Request request = new Request.Builder()
            .url(url)
            .post(body)
            .build();
        try {
            Response response = client.newCall(request).execute();
            return Objects.requireNonNull(response.body()).string();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }

}
<file_sep>plugins {
    id 'org.springframework.boot'
    id 'io.spring.dependency-management'
    id 'java'
    id 'org.jetbrains.kotlin.jvm'
}

group = 'com.superexercisebook'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'

repositories {
    mavenCentral()
}

dependencies {
    // Kotlin Support
    compile "org.jetbrains.kotlin:kotlin-reflect"
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
    api project(path: ':framework')
}

test {
    useJUnitPlatform()
}

compileKotlin {
    kotlinOptions {
        jvmTarget = "1.8"
    }
}
compileTestKotlin {
    kotlinOptions {
        jvmTarget = "1.8"
    }
}
<file_sep>package com.superexercisebook.bugreport.okhttp.framework

import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.RequestBody
import okhttp3.RequestBody.Companion.toRequestBody


class KotlinHttpClient {
    fun test(s: String): String {
        val client = OkHttpClient()
        val url = "https://one.one.one.one/cdn-cgi/trace"
        val body: RequestBody = s.toRequestBody("plain/text; charset=utf-8".toMediaTypeOrNull())
        val request: Request = Request.Builder()
            .url(url)
            .post(body)
            .build()
        client.newCall(request).execute().use { response ->
            return response.body!!.string()
        }
    }

}
<file_sep>plugins {
    id 'org.springframework.boot' version '2.4.5'
    id 'io.spring.dependency-management' version '1.0.11.RELEASE'
    id 'java'
    id 'org.jetbrains.kotlin.jvm' version '1.5.0'

    id 'maven-publish'
    id "org.springframework.boot.experimental.thin-launcher" version "1.0.25.RELEASE"
}

subprojects {
    tasks.withType(JavaCompile) {
        options.encoding = 'UTF-8'
    }
}

group 'com.marigold'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

publishing {
    publications {
        maven(MavenPublication) {
            from components.java
        }
    }
}
<file_sep># An issue in OkHttp Kotlin

## build

Use IDEA to run the `Application.kt` file.

## Description

### Dependency Graph

```
    OkHttp -> Framework -> Project
```

In this case, we can assume that framework prepares some commonly used OkHttp operations for every module in the whole project.

### What Happened

#### `framework` module

In module `framework`, we can run all the OkHttp operations successfully.

But we can't compile `MediaType.parse("plain/text; charset=utf-8")` in the main function, because the code of OkHttp blocked it.

```
    @JvmName("-deprecated_parse")
    @Deprecated(
        message = "moved to extension function",
        replaceWith = ReplaceWith(
            expression = "mediaType.toMediaTypeOrNull()",
            imports = ["okhttp3.MediaType.Companion.toMediaTypeOrNull"]),
        level = DeprecationLevel.ERROR)     // <------- this line
    fun parse(mediaType: String): MediaType? = mediaType.toMediaTypeOrNull()
```

#### `project` module

In module `project`, we can only run the OkHttp operation which is written by java in `framework` successfully.

But we can compile `MediaType.parse("plain/text; charset=utf-8")` in the main function.

## Something I guess

In `framework` module, `@Deprecated` blocks this kind of usage, but `@JvmStatic @JvmName` let the function below visible to `project`.

```
    @JvmStatic
    @JvmName("parse")
    fun String.toMediaTypeOrNull(): MediaType? {
      return try {
        toMediaType()
      } catch (_: IllegalArgumentException) {
        null
      }
    }
```

We can use `MediaType.parse("plain/text; charset=utf-8")` in `project` module.

Because of the same reason. `@JvmStatic @JvmName` let `okhttp3.MediaType.Companion.toMediaTypeOrNull` become `okhttp3.MediaType.parse`.

We can see an `Exception in thread "main" java.lang.NoSuchFieldError: Companion` error when running main function of `project` module.

## Conclusion

I am not sure what this issue blame to, but I prefer to change the `DepreciationLevel` to a lower level to resolve this issue temporary.<file_sep>package com.superexercisebook.bugreport.okhttp.framework

import okhttp3.MediaType

object Application {
    @JvmStatic
    fun main(args : Array<String>) {
        // Succeed
        println(JavaHttpClient().test("test"))

        // Success
        println(KotlinHttpClient().test("test"))

        // Blocked by @Deprecated annotation
        // MediaType.parse("plain/text; charset=utf-8")
    }
}
<file_sep>pluginManagement {
    repositories {
        maven { url 'https://dl.bintray.com/kotlin/kotlin-eap' }
        mavenCentral()
        maven { url 'https://plugins.gradle.org/m2/' }
    }
}

rootProject.name = 'Server'

include ':framework'
include ':project'

project(':framework').projectDir = "$rootDir/framework" as File
project(':project').projectDir = "$rootDir/project" as File
