Swagger-codegen: Java: java.lang.IllegalArgumentException: Unknown pattern character 'X' when instantiating an Api class

Created on 16 Nov 2015  路  8Comments  路  Source: swagger-api/swagger-codegen

We are getting an exception (java.lang.IllegalArgumentException: Unknown pattern character 'X') thrown whenever we try to instantiate a Api class. It appears to be due to the default simple date time format having an invalid character in it (X). See https://github.com/swagger-api/swagger-codegen/blob/master/modules/swagger-codegen/src/main/resources/Java/ApiClient.mustache#L60 for the offending line.

We are using the okhttp-gson library on Android.

Most helpful comment

You are right, Android uses ZZZZZ instead to generate time zone like +01:00 (like XXX in Java).
For now, could you try using the ApiClient#setDatetimeFormat method to customize the datetime format to make it work in Android?

Taking the petstore sample as an example:

// import io.swagger.client.Configuration;
// import io.swagger.client.ApiClient;
// import java.text.SimpleDateFormat;

// Customize for the default ApiClient
Configuration.getDefaultApiClient().setDatetimeFormat(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ"));

// Customize for a new ApiClient
ApiClient apiClient = new ApiClient();
apiClient.setDatetimeFormat(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ"));
// Use the new ApiClient
PetApi api = new PetApi(apiClient);
api.getPetById(new Long(1));

All 8 comments

Are you using Java 1.6? XXX in SimpleDateFormat was added since Java 1.7

The Android version of SimpleDateFormat does not support XXX. See http://developer.android.com/reference/java/text/SimpleDateFormat.html. Since the java generator is being pushed as a replacement for the android generator which has date parsing issues of it's own, this is a problem.

You are right, Android uses ZZZZZ instead to generate time zone like +01:00 (like XXX in Java).
For now, could you try using the ApiClient#setDatetimeFormat method to customize the datetime format to make it work in Android?

Taking the petstore sample as an example:

// import io.swagger.client.Configuration;
// import io.swagger.client.ApiClient;
// import java.text.SimpleDateFormat;

// Customize for the default ApiClient
Configuration.getDefaultApiClient().setDatetimeFormat(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ"));

// Customize for a new ApiClient
ApiClient apiClient = new ApiClient();
apiClient.setDatetimeFormat(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ"));
// Use the new ApiClient
PetApi api = new PetApi(apiClient);
api.getPetById(new Long(1));

I tried to set the format via setDatetimeFormat. However, as I mentioned above, the exception is thrown during instantiation so Android can never get to the point where we try to set the format. See https://github.com/swagger-api/swagger-codegen/blob/master/modules/swagger-codegen/src/main/resources/Java/ApiClient.mustache#L60.

Since Android doesn't support XXX, any use of new ApiClient() throws an exception (even if the format is set via Configuration).

I see. Maybe a hack of catching exception for new SimpleDateFormat would be enough here to try a different date format for Android. I'll submit a PR for it.

I think that would be good enough for us. That way it works for JDK 7 users and Android users can set their own format.

@samuelg I submitted PR #1579 to fix the datetime format issue in Android environment for Java's okhttp-gson codegen. Could you give it a try and see if it works for you?

BTW, note that for okhttp-gson codegen, this file is the one used to generate ApiClient.java:
modules/swagger-codegen/src/main/resources/Java/libraries/okhttp-gson/ApiClient.mustache
not this one:
modules/swagger-codegen/src/main/resources/Java/ApiClient.mustache

Sorry it took so long to comment, I was out for personal reasons. This works for us. Thank you!

Was this page helpful?
0 / 5 - 0 ratings