Getting this error while using swagger generated as discussed in this URL, Could anyone please take a look and suggest any workaround for this?
It would be a great help if you could help with this issue?
Attaching Stack Trace and possible reasons due to which it is being generated.
https://elmah.io/tools/stack-trace-formatter/80eb332a6f7a4fc7a65cb5c6a9ee7e1a
Basically, the discriminator being generated is empty, how to solve this?
return getClassByDiscriminator(
classByDiscriminatorValue,
getDiscriminatorValue(readElement, ""));
As mentioned by @Chandim Chatterjee this seems to be a known bug in swagger-codegen. https://github.com/swagger-api/swagger-codegen/issues/10196
I guess the solution for now is to use openapi-generator if this bug affects you, or else wait for the fix for swagger-codegen to be released.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
What URL/API are you accessing? It's hard to get any context here.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Also, it looks like you are running a test that is failing. I don't think you can run the generated tests without modification.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
What modification do I need to make, like added API token and userName getting data, but the problem occurs in deserialization of Data as swagger is creating multiple implementations, but doesn't give any discriminator.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Perhaps you could indicate what options you used to to generate your library, and give some sample code you are calling that is failing.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Code Failing here while deserialization of data while trying to run Tests
public static GsonBuilder createGson() {
GsonFireBuilder fireBuilder = new GsonFireBuilder()
.registerTypeSelector(UserDetails.class, new TypeSelector<UserDetails>() {
@Override
public Class<? extends UserDetails> getClassForElement(JsonElement readElement) {
Map<String, Class<? extends UserDetails>> classByDiscriminatorValue = new HashMap<>();
classByDiscriminatorValue.put("AllOfAttachmentAuthor".toUpperCase(), AllOfAttachmentAuthor.class);
classByDiscriminatorValue.put("AllOfChangelogAuthor".toUpperCase(), AllOfChangelogAuthor.class);
classByDiscriminatorValue.put("AllOfCommentAuthor".toUpperCase(), AllOfCommentAuthor.class);
classByDiscriminatorValue.put("AllOfCommentUpdateAuthor".toUpperCase(), AllOfCommentUpdateAuthor.class);
classByDiscriminatorValue.put("AllOfEventNotificationUser".toUpperCase(), AllOfEventNotificationUser.class);
classByDiscriminatorValue.put("AllOfFieldsAssignee".toUpperCase(), AllOfFieldsAssignee.class);
classByDiscriminatorValue.put("AllOfWorklogAuthor".toUpperCase(), AllOfWorklogAuthor.class);
classByDiscriminatorValue.put("AllOfWorklogUpdateAuthor".toUpperCase(), AllOfWorklogUpdateAuthor.class);
classByDiscriminatorValue.put("UserDetails".toUpperCase(), UserDetails.class);
return getClassByDiscriminator(
classByDiscriminatorValue,
getDiscriminatorValue(readElement, ""));
}
})
private static <T> Class<? extends T> getClassByDiscriminator(Map<String, Class<? extends T>> classByDiscriminatorValue, String discriminatorValue) {
Class<? extends T> clazz = classByDiscriminatorValue.get(discriminatorValue.toUpperCase());
if(null == clazz) {
throw new IllegalArgumentException("cannot determine model class of name: <" + discriminatorValue + ">");
}
return clazz;
}
private static String getDiscriminatorValue(JsonElement readElement, String discriminatorField) {
JsonElement element = readElement.getAsJsonObject().get(discriminatorField);
if(null == element) {
throw new IllegalArgumentException("missing discriminator field: <" + discriminatorField + ">");
}
return element.getAsString();
}
swagger-codegen generate \
-i ./swagger-v3.v3.json \
-l java \
-o ~/src/jira-client
Config.json
{
"modelPackage": "com.spr.jira.rest.client.model",
"apiPackage": "com.spr.jira.rest.client.api",
"invokerPackage": "com.spr.jira.rest.client",
"library": "jersey2",
"groupId": "com.spr.jira.rest",
"artifactId": "client"
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I get quite different code for the same config/command. I only see this method signature in client/JSON.java:
public static GsonBuilder createGson()
But the method I get looks like:
public static GsonBuilder createGson() {
GsonFireBuilder fireBuilder = new GsonFireBuilder()
;
return fireBuilder.createGsonBuilder();
}
I'm using swagger-codegen 3.0.16 and the latest swagger-v3.v3.json
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I got the same issue as OP and getting this exception whenever I try to fetch the changelog. @David Smith were you able to figure out this issue?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@Chandim Chatterjee how did you generate the source for your client? (e.g. which version of swagger-codegen, and did you use the latest published swagger spec) Can you confirm you're requesting this from a Jira Cloud site (and not Server or Data Centre)? (FWIW I can't see the original stack trace any more, so I can't see the actual error any more.)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I used the spec available on this site: https://developer.atlassian.com/cloud/jira/platform/rest/v3/intro/ and my swagger-codegen version is 3.0.41.
This is the config I used in generating the client in java:
{
"modelPackage": "com.squareup.jira.cloud.rest.client.model",
"apiPackage": "com.squareup.jira.cloud.rest.client.api",
"invokerPackage": "com.squareup.jira.cloud.rest.client",
"library": "retrofit2",
"groupId": "com.squareup.jira.cloud.rest",
"artifactId": "client"
}
This is the error I get:
missing discriminator field: <>
java.lang.IllegalArgumentException:
at com.squareup.jira.cloud.rest.client.JSON.getDiscriminatorValue(JSON.java:983)
at com.squareup.jira.cloud.rest.client.JSON$53.getClassForElement(JSON.java:387)
at io.gsonfire.gson.TypeSelectorTypeAdapterFactory$TypeSelectorTypeAdapter.read(TypeSelectorTypeAdapterFactory.java:65)
at io.gsonfire.gson.NullableTypeAdapter.read(NullableTypeAdapter.java:36)
at io.gsonfire.gson.HooksTypeAdapter.deserialize(HooksTypeAdapter.java:86)
at io.gsonfire.gson.HooksTypeAdapter.read(HooksTypeAdapter.java:54)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.readIntoField(ReflectiveTypeAdapterFactory.java:212)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$FieldReflectionAdapter.readField(ReflectiveTypeAdapterFactory.java:433)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:393)
at retrofit2.converter.gson.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:40)
at retrofit2.converter.gson.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:27)
at retrofit2.OkHttpCall.parseResponse(OkHttpCall.java:243)
at retrofit2.OkHttpCall.execute(OkHttpCall.java:204)
...
Looking at the auto-generated code, I see a similar error as what @David Smith found.
I believe this may be an existing error in swagger-codegen according to this bug which has been only recently fixed: https://github.com/swagger-api/swagger-codegen/issues/10196
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Great find @Chandim Chatterjee . I agree - it's probably the swagger-codegen bug you linked. There's a comment there that openapi-generator works OK, or else I guess you could wait for the bug fix.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi, Could anyone help in this, or mention what more context should I provide ?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Using openapi codegen (java jar version) instead of swagger-codegen produced code that seems to work fine for me, getting my worklog hours mostly. Have not tested everything, but at least no up-front hacking required.
Switch the -l java option with -g java, other options in use here are the same.
Used Java 8 to build it.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.