Getting error while using jira cloud api from swagger

David Smith January 14, 2022

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, ""));

 

3 answers

0 votes
Ben Kelley
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
March 15, 2023

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.

0 votes
David Smith January 15, 2022

@Ben Kelley, Can you please help?

Ben Kelley
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
January 17, 2022

What URL/API are you accessing? It's hard to get any context here.

Ben Kelley
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
January 17, 2022

Also, it looks like you are running a test that is failing. I don't think you can run the generated tests without modification.

David Smith January 17, 2022

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.

Ben Kelley
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
January 18, 2022

Perhaps you could indicate what options you used to to generate your library, and give some sample code you are calling that is failing.

David Smith January 21, 2022

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"
}

David Smith January 26, 2022

Hi, anything else should I provide?

Ben Kelley
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
January 26, 2022

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

Chandim Chatterjee
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
March 14, 2023

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?

Ben Kelley
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
March 14, 2023

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

Chandim Chatterjee
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
March 15, 2023

@Ben Kelley 

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

Like Ben Kelley likes this
Ben Kelley
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
March 15, 2023

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.

0 votes
David Smith January 15, 2022

Hi, Could anyone help in this, or mention what more context should I provide ?

Jon Martin Solaas February 3, 2023

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.

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
FREE
PERMISSIONS LEVEL
Product Admin
TAGS
AUG Leaders

Atlassian Community Events