swagger-codegen icon indicating copy to clipboard operation
swagger-codegen copied to clipboard

[Java] codegen with parcelableMode fails to build if using arrays in swagger

Open morriscodes opened this issue 9 years ago • 9 comments

Description

Java codegen with parcelableMode enabled causing build errors if the swagger file has arrays.

Swagger-codegen version

snapshot 2.2.2

Swagger declaration file content or url

This is an example of how I create the array:

    {
    "WeatherValueTypes": {
        "type": "array",
        "items": {
        "type": "string",
         "enum": [
              "airTemp"
          ]
       }
    }
Command line used for generation

java -jar swagger-codegen-cli.jar generate -i swagger.json -l java -c config.json -o data

{
"artifactId": "Data",
  "artifactVersion": "1.0.0",
  "library": "retrofit2",
  "parcelableModel": true,
  "hideGenerationTimestamp": true
}
Steps to reproduce

command line generation works fine. It fails when you try to build from gradle.

Error Output

/git/swagger/data/src/main/java/com/test/data/model/WeatherValueTypes.java:66: error: cannot find symbol
     super.writeToParcel(out, flags);    }
          ^
  symbol: method writeToParcel(Parcel,int)
/git/swagger/data/src/main/java/com/test/data/model/WeatherValueTypes.java:73: error: no suitable constructor found for ArrayList(Parcel)
     super(in); 
     ^
    constructor ArrayList.ArrayList(int) is not applicable
      (argument mismatch; Parcel cannot be converted to int)
    constructor ArrayList.ArrayList(Collection<? extends String>) is not applicable
      (argument mismatch; Parcel cannot be converted to Collection<? extends String>)
Related issues
Suggest a Fix

This is the PR that added parcelable support:

https://github.com/swagger-api/swagger-codegen/pull/3320/files

{{#parent}} super(in); {{/parent}} and {{#parent}} super.writeToParcel(out, flags); {{/parent}} seem to be the primary issues when dealing with arrays.

morriscodes avatar Feb 04 '17 01:02 morriscodes

@morriscodes thanks for reporting the issue.

What would be the correct code looks like for handling array of string?

wing328 avatar Feb 04 '17 05:02 wing328

The implementation of Parcelable needs to be further thought out. My example above is easily reproducible but the problem applies to any arrays.

Example:

"Wallet": {
    "type": "array",
    "items": {
        "$ref": "#\/definitions\/Money"
    }
 }

Swagger generates objects like this as extends ArrayList<Money> but when the Parcelable code is generated it checks for a parent, and if exists delegate the parcelable work up {{#parent}} super(in); {{/parent}}. This would work if the object extended Money and Money implements Parcelable but it doesn't, it extends ArrayList. ArrayList doesn't implement parcelable so your build fails.

Here is a stackoverflow example for handling parcelable on objects that extend ArrayList: http://stackoverflow.com/questions/10953121/android-arraylistmyobject-pass-as-parcelable

As for the specific example of implementing parcelable on an object that extends ArrayList<String>.. as far as I can tell, you shouldn't. Parcelable has specific methods for handling String Lists. So if an object contains a property that is an ArrayList<String> you would implement that with writeStringList(List<String> val) and readStringList(List<String> list).

I'm no parcelable expert, the above is just what I discovered reading the docs this morning. Hopefully there are some stronger Mustache guys around that understand the correct way to implement something like this for swagger.

morriscodes avatar Feb 04 '17 17:02 morriscodes

@morriscodes thanks for sharing more with me.

Just to confirm, is WeatherValueTypes a model/object or property of a model/object?

wing328 avatar Feb 06 '17 05:02 wing328

@wing328 Swagger generates it as its own model.

morriscodes avatar Feb 06 '17 16:02 morriscodes

To define an array of model, you can define something like https://github.com/swagger-api/swagger-codegen/blob/master/modules/swagger-codegen/src/test/resources/2_0/petstore.yaml#L112-L117

Would that help?

In other words, I want to avoid defining something like the following:

"Wallet": {
    "type": "array",
    "items": {
        "$ref": "#\/definitions\/Money"
    }
 }

wing328 avatar Feb 06 '17 16:02 wing328

@wing328 That would produce a better structure. I don't have access to change my swagger file but I will see if I can get my server team to update. Are there docs on creating arrays in this way that I can point them to?

morriscodes avatar Feb 08 '17 16:02 morriscodes

@morriscodes the official OpenAPI 2.0 spec can be found in https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md

petstore.yaml is also a good starting point to understand how array of properties or models is defined.

wing328 avatar Feb 08 '17 16:02 wing328

Fixed in #7867

SergeyLyakhov avatar Mar 19 '18 20:03 SergeyLyakhov

Thanks for the fix by @SergeyLyakhov.

@morriscodes please pull the latest master or use the SNAPSHOT version to give it a try.

wing328 avatar Mar 20 '18 16:03 wing328