swagger-api / swagger-codegen

swagger-codegen contains a template-driven engine to generate documentation, API clients and server stubs in different languages by parsing your OpenAPI / Swagger definition.
http://swagger.io
Apache License 2.0
16.94k stars 6.03k forks source link

Android Client missing ApiKeyAuth example in the README #3835

Closed loretoparisi closed 7 years ago

loretoparisi commented 8 years ago
Description

When generating the android-client from the Swagger Editor - https://editor.swagger.io, the autogen README.md is missing an example of how the authentication works:

import io.swagger.client.api.AlbumApi;

public class AlbumApiExample {

    public static void main(String[] args) {
        AlbumApi apiInstance = new AlbumApi();
        String albumId = "albumId_example"; // String | The musiXmatch album id
        String format = "json"; // String | output format: json, jsonp, xml.
        String callback = "callback_example"; // String | jsonp callback
        try {
            InlineResponse200 result = apiInstance.albumGetGet(albumId, format, callback);
            System.out.println(result);
        } catch (ApiException e) {
            System.err.println("Exception when calling AlbumApi#albumGetGet");
            e.printStackTrace();
        }
    }
}

that actually it is missing also some exceptions, so it should be at least:


        public void AlbumAPIGet() {

            AlbumApi apiInstance = new AlbumApi();
            String albumId = "albumId_example"; // String | The musiXmatch album id
            String format = "json"; // String | output format: json, jsonp, xml.
            String callback = "callback_example"; // String | jsonp callback
            try {
                InlineResponse200 result = apiInstance.albumGetGet(albumId, format, callback);
                System.out.println(result);
            } catch (ApiException e) {
                System.err.println("ApiException when calling AlbumApi#albumGetGet");
                e.printStackTrace();
            } catch (java.util.concurrent.TimeoutException e) {
                System.err.println("TimeoutException when calling AlbumApi#albumGetGet");
                e.printStackTrace();
            } catch (java.util.concurrent.ExecutionException e) {
                System.err.println("ExecutionException when calling AlbumApi#albumGetGet");
                e.printStackTrace();
            } catch (java.lang.InterruptedException e) {
                System.err.println("InterruptedException when calling AlbumApi#albumGetGet");
                e.printStackTrace();
            }
        }
    }

In the README.md there is no reference about the ApiKeyAuth, 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
  1. import from url the swagger definition above in the Swagger Editor
  2. Generate the Android client
  3. Open README.md
    Related issues
Suggest a Fix
wing328 commented 8 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?

loretoparisi commented 8 years ago

@wing328 that makes sense, let me check.

loretoparisi commented 8 years ago

@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>)
wing328 commented 8 years ago

[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.

loretoparisi commented 8 years ago

@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?

wing328 commented 8 years ago

@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

loretoparisi commented 8 years ago

@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.

wing328 commented 8 years ago
AlbumApi apiInstance = new AlbumApi();
ApiInvoker invoker = apiInstance.getInvoker();
invoker.setApiKey("YOUR_API_KEY");

Based on my understanding the above should work.

loretoparisi commented 8 years ago

@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.

wing328 commented 8 years ago

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

loretoparisi commented 8 years ago

@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

wing328 commented 7 years ago

@loretoparisi no idea off the top of my head.

cc @Shyri to see if he's any clue.

websoftix commented 7 years ago

@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.

wing328 commented 7 years ago

@websoftix are you using the latest master, which has the mentioned fix already?

https://github.com/swagger-api/swagger-codegen/blob/1dd9ee39af6c4e2f9c016a94d16ca47a88a9cb0d/modules/swagger-codegen/src/main/resources/android/libraries/volley/apiInvoker.mustache#L225-L228

websoftix commented 7 years ago

@wing328 that's great. No, I have used the code generated by online editor.

wing328 commented 7 years ago

@websoftix @loretoparisi closing this for now. If you still find issues with the latest master, we'll look into the issue again.