Closed loretoparisi closed 7 years ago
@loretoparisi can you try the Java API client (okhttp-gson) which should be compatible with Android and have the correct readme showing code samples for authentication?
@wing328 that makes sense, let me check.
@wing328 so in the java
example I have
ApiClient defaultClient = Configuration.getDefaultApiClient();
// Configure API key authorization: key
ApiKeyAuth key = (ApiKeyAuth) defaultClient.getAuthentication("key");
key.setApiKey("YOUR API KEY");
///...
the problem is that I don't see the Configuration
class in the Android
java package, while we have a Configuration.java
in the java
client in src/main/java/io/swagger/client
.
Same for client/ApiClient.java
that it is missing in the Android client.
By the way, I have built the java
client with maven
, just to be sure, but doing
maven install
I get an error on the newly generated java
client:
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building swagger-java-client 1.0.0
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- build-helper-maven-plugin:1.10:add-source (add_sources) @ swagger-java-client ---
[INFO] Source directory: /Users/admin/Projects/swagger/build/java-client/src/main/java added.
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ swagger-java-client ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /Users/admin/Projects/swagger/build/java-client/src/main/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ swagger-java-client ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 138 source files to /Users/admin/Projects/swagger/build/java-client/target/classes
[INFO] -------------------------------------------------------------
[WARNING] COMPILATION WARNING :
[INFO] -------------------------------------------------------------
[WARNING] Some messages have been simplified; recompile with -Xdiags:verbose to get full output
[INFO] 1 warning
[INFO] -------------------------------------------------------------
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] /Users/admin/Projects/swagger/build/java-client/src/main/java/io/swagger/client/api/AlbumApi.java:[162,139] variable callback is already defined in method albumGetGetAsync(java.lang.String,java.lang.String,java.lang.String,io.swagger.client.ApiCallback<io.swagger.client.model.InlineResponse200>)
[ERROR] /Users/admin/Projects/swagger/build/java-client/src/main/java/io/swagger/client/api/AlbumApi.java:[300,226] variable callback is already defined in method artistAlbumsGetGetAsync(java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.math.BigDecimal,java.math.BigDecimal,io.swagger.client.ApiCallback<io.swagger.client.model.InlineResponse2002>)
[ERROR] /Users/admin/Projects/swagger/build/java-client/src/main/java/io/swagger/client/api/AlbumApi.java:[162,139] variable callback is already defined in method albumGetGetAsync(java.lang.String,java.lang.String,java.lang.String,io.swagger.client.ApiCallback
)
Did you define "callback" as a parameter any operation/endpoint?
For Android (volley), do you have time to contribute the fix/enhancement to the documentation? I can show you the which files to modify.
@wing328 so the error was doing mvn install
in the auto generated client, so yes, it is formally correct, since it works for other clients like in the Android
client the command mvn install
will generate the jar
file, so I assume that it is correct.
For the docs, question is, is correct that the android-client
does not have Configuration
and ApiClient
objects like the java-client
has?
@loretoparisi that's correct. Android (volley) client does not have Configuration.java
, ApiClient.java
but it has ApiInvoker.java
: https://github.com/swagger-api/swagger-codegen/tree/master/samples/client/petstore/android/volley/src/main/java/io/swagger/client
@wing328 ok it's becoming clear...
I was trying something like
ApiInvoker.initializeInstance();
ApiInvoker invoker = ApiInvoker.getInstance();
invoker.setApiKey("YOUR_API_KEY");
or like
AlbumApi apiInstance = new AlbumApi();
ApiInvoker invoker = apiInstance.getInvoker();
invoker.setApiKey("YOUR_API_KEY");
but it seems that the invoker
instance it is still null
.
AlbumApi apiInstance = new AlbumApi();
ApiInvoker invoker = apiInstance.getInvoker();
invoker.setApiKey("YOUR_API_KEY");
Based on my understanding the above should work.
@wing328 right it should instead I get a
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void io.swagger.client.ApiInvoker.setApiKey(java.lang.String)' on a null object reference
method is virtual
it seems it is implemented somewhere else.
You may need to new an instance of auth helper class in https://github.com/swagger-api/swagger-codegen/tree/master/samples/client/petstore/android/volley/src/main/java/io/swagger/client/auth
@wing328 you are right, so first we need an Authentication
instance
ApiKeyAuth auth = new ApiKeyAuth("query","apikey");
auth.setApiKey("YOUR_API_KEY");
then we setup the api instance
AlbumApi apiInstance = new AlbumApi();
Last problem (but not least), the apiInstance
still null
here :(
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String io.swagger.client.ApiInvoker.invokeAPI(java.lang.String, java.lang.String, java.lang.String, java.util.List, java.lang.Object, java.util.Map, java.util.Map, java.lang.String, java.lang.String[])' on a null object reference
@loretoparisi no idea off the top of my head.
cc @Shyri to see if he's any clue.
@loretoparisi I've encountered the problem too (null INSTANCE) while trying to use apiKey auth and I fixed it by changing the ApiInvoker .getInstance() method adding a check for null and initializing it if null. It now looks like:
public static ApiInvoker getInstance() { if (INSTANCE == null) { initializeInstance(); } return INSTANCE; }
Of course, every time I modify the API definition and regenerate the client I have to "fix it" so it would be nice if this can be added into the generated code.
@websoftix are you using the latest master, which has the mentioned fix already?
@wing328 that's great. No, I have used the code generated by online editor.
@websoftix @loretoparisi closing this for now. If you still find issues with the latest master, we'll look into the issue again.
Description
When generating the
android-client
from the Swagger Editor - https://editor.swagger.io, the autogenREADME.md
is missing an example of how the authentication works:that actually it is missing also some exceptions, so it should be at least:
In the
README.md
there is no reference about theApiKeyAuth
, how to load it and how to set the api key for the current client configuration.Swagger-codegen version
Latest version available from https://editor.swagger.io
Swagger declaration file content or url
This is my swagger declaration file: https://playground.musixmatch.com/swagger.json
Command line used for generation
Latest version available from https://editor.swagger.io
Steps to reproduce
Related issues
Suggest a Fix