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

[BUG][Java] ApiClient.java: error: no suitable method found for create(byte[],MediaType) #9151

Closed
5 tasks done
toby-murray-snow-software opened this issue Apr 1, 2021 · 11 comments · Fixed by #9353
Closed
5 tasks done

Comments

@toby-murray-snow-software
Copy link
Contributor

toby-murray-snow-software commented Apr 1, 2021

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • [] What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

Using the java generator against a basic spec, the generated project fails to compile.

openapi-generator version

5.1.0 and latest master (docker image 3c6b0382cd49)

Regression from 5.0.1

OpenAPI declaration file content or url
openapi: 3.0.0
info:
  version: 1.0.0
  title: Api

paths:
  /version:
    get:
      tags:
      - version
      summary: Gets the system version check
      operationId: getVersion
      responses:
        200:
          description: Success
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/VersionGetResponse'

components:
  schemas:
    VersionGetResponse:
      type: string
      description: The version of this service
      required:
        - status
      properties:
        status:
          type: string
          description: OK
Generation Details

java -jar openapi-generator-cli-5.1.0.jar generate -g java -i service.yaml

Steps to reproduce
  1. Run the generator as described above
  2. Try to compile the code with ./gradlew compileJava or similar
Expected:

It will compile

Actual:

A bunch of:

out\java\src\main\java\org\openapitools\client\ApiClient.java:818: error: no suitable method found for create(byte[],MediaType)
            return RequestBody.create((byte[]) obj, MediaType.parse(contentType));
                              ^
    method RequestBody.create(MediaType,String) is not applicable
      (argument mismatch; byte[] cannot be converted to MediaType)
    method RequestBody.create(MediaType,ByteString) is not applicable
      (argument mismatch; byte[] cannot be converted to MediaType)
    method RequestBody.create(MediaType,byte[]) is not applicable
      (argument mismatch; byte[] cannot be converted to MediaType)
    method RequestBody.create(MediaType,File) is not applicable
      (argument mismatch; byte[] cannot be converted to MediaType)
Related issues/PRs
Suggest a fix
@toby-murray-snow-software
Copy link
Contributor Author

toby-murray-snow-software commented Apr 1, 2021

In 5.0.1 the generated line in ApiClient was:

return RequestBody.create(MediaType.parse(contentType), (byte[]) obj);

in 5.1.0 it is:

return RequestBody.create((byte[]) obj, MediaType.parse(contentType));

Looks like this change was introduced in #8969

@wing328
Copy link
Member

wing328 commented Apr 3, 2021

    VersionGetResponse:
      type: string
      description: The version of this service
      required:
        - status
      properties:
        status:
          type: string
          description: OK

Should it be type: object instead?

@wing328
Copy link
Member

wing328 commented Apr 3, 2021

I did a test with the latest master and the output compiles without issues. Can you give it another try with the latest master?

@toby-murray-snow-software
Copy link
Contributor Author

toby-murray-snow-software commented Apr 7, 2021

Should it be type: object instead?

I don't believe so - I'll look at the response, it's intended to be a simple string.

I did a test with the latest master and the output compiles without issues

You used the attached YAML and it compiles? On both this sample and the real YAML I distilled it from it continues to not compile for me with the latest Docker image (563bdd246a47).

docker run --rm -v "${PWD}:/local" openapitools/openapi-generator-cli generate -i /local/service.yaml -g java -o /local
<...snip generation output...>

> Task :compileJava FAILED
C:\Users\tobmur\git\myproject\src\main\yaml\out\java2\src\main\java\org\openapitools\client\ApiClient.java:818: error: no suitable method found for create(byte[],MediaType)
            return RequestBody.create((byte[]) obj, MediaType.parse(contentType));
                              ^
    method RequestBody.create(MediaType,String) is not applicable
      (argument mismatch; byte[] cannot be converted to MediaType)
    method RequestBody.create(MediaType,ByteString) is not applicable
      (argument mismatch; byte[] cannot be converted to MediaType)
    method RequestBody.create(MediaType,byte[]) is not applicable
      (argument mismatch; byte[] cannot be converted to MediaType)
    method RequestBody.create(MediaType,File) is not applicable
      (argument mismatch; byte[] cannot be converted to MediaType)
C:\Users\tobmur\git\myproject\src\main\yaml\out\java2\src\main\java\org\openapitools\client\ApiClient.java:821: error: no suitable method found for create(File,MediaType)
            return RequestBody.create((File) obj, MediaType.parse(contentType));
                              ^
    method RequestBody.create(MediaType,String) is not applicable
      (argument mismatch; File cannot be converted to MediaType)
    method RequestBody.create(MediaType,ByteString) is not applicable
      (argument mismatch; File cannot be converted to MediaType)
    method RequestBody.create(MediaType,byte[]) is not applicable
      (argument mismatch; File cannot be converted to MediaType)
    method RequestBody.create(MediaType,File) is not applicable
      (argument mismatch; File cannot be converted to MediaType)
C:\Users\tobmur\git\myproject\src\main\yaml\out\java2\src\main\java\org\openapitools\client\ApiClient.java:829: error: no suitable method found for create(String,MediaType)
            return RequestBody.create(content, MediaType.parse(contentType));
                              ^
    method RequestBody.create(MediaType,String) is not applicable
      (argument mismatch; String cannot be converted to MediaType)
    method RequestBody.create(MediaType,ByteString) is not applicable
      (argument mismatch; String cannot be converted to MediaType)
    method RequestBody.create(MediaType,byte[]) is not applicable
      (argument mismatch; String cannot be converted to MediaType)
    method RequestBody.create(MediaType,File) is not applicable
      (argument mismatch; String cannot be converted to MediaType)
C:\Users\tobmur\git\myproject\src\main\yaml\out\java2\src\main\java\org\openapitools\client\ApiClient.java:1079: error: no suitable method found for create(String,MediaType)
                reqBody = RequestBody.create("", MediaType.parse(contentType));
                                     ^
    method RequestBody.create(MediaType,String) is not applicable
      (argument mismatch; String cannot be converted to MediaType)
    method RequestBody.create(MediaType,ByteString) is not applicable
      (argument mismatch; String cannot be converted to MediaType)
    method RequestBody.create(MediaType,byte[]) is not applicable
      (argument mismatch; String cannot be converted to MediaType)
    method RequestBody.create(MediaType,File) is not applicable
      (argument mismatch; String cannot be converted to MediaType)
C:\Users\tobmur\git\myproject\src\main\yaml\out\java2\src\main\java\org\openapitools\client\ApiClient.java:1230: error: no suitable method found for create(File,MediaType)
                mpBuilder.addPart(partHeaders, RequestBody.create(file, mediaType));
                                                          ^
    method RequestBody.create(MediaType,String) is not applicable
      (argument mismatch; File cannot be converted to MediaType)
    method RequestBody.create(MediaType,ByteString) is not applicable
      (argument mismatch; File cannot be converted to MediaType)
    method RequestBody.create(MediaType,byte[]) is not applicable
      (argument mismatch; File cannot be converted to MediaType)
    method RequestBody.create(MediaType,File) is not applicable
      (argument mismatch; File cannot be converted to MediaType)
C:\Users\tobmur\git\myproject\src\main\yaml\out\java2\src\main\java\org\openapitools\client\ApiClient.java:1233: error: no suitable method found for create(String,<null>)
                mpBuilder.addPart(partHeaders, RequestBody.create(parameterToString(param.getValue()), null));
                                                          ^
    method RequestBody.create(MediaType,String) is not applicable
      (argument mismatch; String cannot be converted to MediaType)
    method RequestBody.create(MediaType,ByteString) is not applicable
      (argument mismatch; String cannot be converted to MediaType)
    method RequestBody.create(MediaType,byte[]) is not applicable
      (argument mismatch; String cannot be converted to MediaType)
    method RequestBody.create(MediaType,File) is not applicable
      (argument mismatch; String cannot be converted to MediaType)
Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output
6 errors

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':compileJava'.
> Compilation failed; see the compiler error output for details.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

Deprecated Gradle features were used in this build, making it incompatible with Gradle 7.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/6.7.1/userguide/command_line_interface.html#sec:command_line_warnings

BUILD FAILED in 1s
1 actionable task: 1 executed

I'm on Windows with this, I wouldn't imagine this is a platform difference but maybe?

@cb-manideep
Copy link

@wing328 The okhttp3 version needs to be updated to 4.9.0
compile 'com.squareup.okhttp3:okhttp:4.9.0'

@wing328
Copy link
Member

wing328 commented Apr 11, 2021

@cb-manideep can you please file a PR with the suggested change?

@cb-manideep
Copy link

@cb-manideep can you please file a PR with the suggested change?

Sure!

@toby-murray-snow-software
Copy link
Contributor Author

toby-murray-snow-software commented Apr 26, 2021

@cb-manideep - any luck? If you're no longer intending to tackle this, I'd like to grab it in hopes of making it into the next release.

@cb-manideep
Copy link

@toby-murray-snow-software Please go ahead :)

@toby-murray-snow-software
Copy link
Contributor Author

toby-murray-snow-software commented Apr 26, 2021

@wing328 - OkHTTP 3.13+ (including 4.*) needs Java 8. The current default of the generator is Java 7 and there's a flag to use Java 8. My understanding is 5.* intended to drop Java 7 and default to Java 8.

If it's viable to drop Java 7, I can submit a PR doing so. Without that, updating OkHTTP is marginally more complicated: needs to default to a ~2 year old version and only optionally use a modern version.

toby-murray-snow-software added a commit to toby-murray-snow-software/openapi-generator that referenced this issue Apr 26, 2021
My understanding of the version 5 release of OpenAPI generator was that Java 7 support was dropped. Currently the Java client defaults to Java 7, Java 8 can be opted in with the `java8` config option. This removes the conditional logic in `build.gradle` around that config option and defaults to Java 8. This is to support OpenAPITools#9151, up-to-date dependency versions rely on Java 8 and it's easier if that is the default.

To be clear, this changes behavior - previous default version was Java 7, this changes it to Java 8.
wing328 pushed a commit that referenced this issue Apr 27, 2021
My understanding of the version 5 release of OpenAPI generator was that Java 7 support was dropped. Currently the Java client defaults to Java 7, Java 8 can be opted in with the `java8` config option. This removes the conditional logic in `build.gradle` around that config option and defaults to Java 8. This is to support #9151, up-to-date dependency versions rely on Java 8 and it's easier if that is the default.

To be clear, this changes behavior - previous default version was Java 7, this changes it to Java 8.
toby-murray-snow-software added a commit to toby-murray-snow-software/openapi-generator that referenced this issue Apr 27, 2021
In 5.0.1 the generated line in `ApiClient` was:
```
return RequestBody.create(MediaType.parse(contentType), (byte[]) obj);
```
in 5.1.0 it is:
```
return RequestBody.create((byte[]) obj, MediaType.parse(contentType));
```

Looks like this change was introduced in OpenAPITools#8969, and requires a more recent version of OkHttp to compile in some (all?) circumstances.
@Snorri1986
Copy link

The same problem on version 2.7.5 with Maven

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants