Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ public class JavaClientCodegen extends AbstractJavaCodegen {
private static final Logger LOGGER = LoggerFactory.getLogger(JavaClientCodegen.class);

public static final String USE_RX_JAVA = "useRxJava";
public static final String PARCELABLE_MODEL = "parcelableModel";

public static final String RETROFIT_1 = "retrofit";
public static final String RETROFIT_2 = "retrofit2";

protected String gradleWrapperPackage = "gradle.wrapper";
protected boolean useRxJava = false;
protected boolean parcelableModel = false;

public JavaClientCodegen() {
super();
Expand All @@ -31,11 +33,12 @@ public JavaClientCodegen() {
modelPackage = "io.swagger.client.model";

cliOptions.add(CliOption.newBoolean(USE_RX_JAVA, "Whether to use the RxJava adapter with the retrofit2 library."));
cliOptions.add(CliOption.newBoolean(PARCELABLE_MODEL, "Whether to generate models for Android that implement Parcelable with the okhttp-gson library."));

supportedLibraries.put("jersey1", "HTTP client: Jersey client 1.19.1. JSON processing: Jackson 2.7.0");
supportedLibraries.put("feign", "HTTP client: Netflix Feign 8.16.0. JSON processing: Jackson 2.7.0");
supportedLibraries.put("jersey2", "HTTP client: Jersey client 2.22.2. JSON processing: Jackson 2.7.0");
supportedLibraries.put("okhttp-gson", "HTTP client: OkHttp 2.7.5. JSON processing: Gson 2.6.2");
supportedLibraries.put("okhttp-gson", "HTTP client: OkHttp 2.7.5. JSON processing: Gson 2.6.2. Enable Parcelable modles on Android using '-DparcelableModel=true'");
supportedLibraries.put(RETROFIT_1, "HTTP client: OkHttp 2.7.5. JSON processing: Gson 2.3.1 (Retrofit 1.9.0). IMPORTANT NOTE: retrofit1.x is no longer actively maintained so please upgrade to 'retrofit2' instead.");
supportedLibraries.put(RETROFIT_2, "HTTP client: OkHttp 3.2.0. JSON processing: Gson 2.6.1 (Retrofit 2.0.2). Enable the RxJava adapter using '-DuseRxJava=true'. (RxJava 1.1.3)");

Expand Down Expand Up @@ -70,6 +73,9 @@ public void processOpts() {
if (additionalProperties.containsKey(USE_RX_JAVA)) {
this.setUseRxJava(Boolean.valueOf(additionalProperties.get(USE_RX_JAVA).toString()));
}
if (additionalProperties.containsKey(PARCELABLE_MODEL)) {
this.setParcelableModels(Boolean.valueOf(additionalProperties.get(PARCELABLE_MODEL).toString()));
}

final String invokerFolder = (sourceFolder + '/' + invokerPackage).replace(".", "/");
final String authFolder = (sourceFolder + '/' + invokerPackage + ".auth").replace(".", "/");
Expand Down Expand Up @@ -212,4 +218,7 @@ public void setUseRxJava(boolean useRxJava) {
this.useRxJava = useRxJava;
}

public void setParcelableModels(boolean parcelableModel) {
this.parcelableModel = parcelableModel;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import {{import}};
{{/imports}}

{{#serializableModel}}import java.io.Serializable;{{/serializableModel}}
{{#parcelableModel}}import android.os.Parcelable;
import android.os.Parcel;{{/parcelableModel}}
{{#models}}
{{#model}}
{{#isEnum}}{{>modelEnum}}{{/isEnum}}{{^isEnum}}{{>pojo}}{{/isEnum}}
Expand Down
44 changes: 43 additions & 1 deletion modules/swagger-codegen/src/main/resources/Java/pojo.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/{{#description}}
@ApiModel(description = "{{{description}}}"){{/description}}
{{>generatedAnnotation}}
public class {{classname}} {{#parent}}extends {{{parent}}}{{/parent}} {{#serializableModel}}implements Serializable{{/serializableModel}} {
public class {{classname}} {{#parent}}extends {{{parent}}}{{/parent}} {{#parcelableModel}}implements Parcelable{{#serializableModel}}, Serializable{{/serializableModel}}{{/parcelableModel}}{{^parcelableModel}}{{#serializableModel}}implements Serializable{{/serializableModel}}{{/parcelableModel}} {
{{#vars}}
{{#isEnum}}
{{^isContainer}}
Expand Down Expand Up @@ -105,4 +105,46 @@ public class {{classname}} {{#parent}}extends {{{parent}}}{{/parent}} {{#seriali
}
return o.toString().replace("\n", "\n ");
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice to add docstrings to the new methods (e.g. writeToParacel, describeContents).

{{#parcelableModel}}
public void writeToParcel(Parcel out, int flags) {
{{#parent}} super.writeToParcel(out, flags); {{/parent}} {{#vars}}
out.writeValue({{name}});
{{/vars}}
}

public {{classname}}() {
super();
}

{{classname}}(Parcel in) {
{{#parent}} super(in); {{/parent}}
{{#vars}}
{{#isContainer}}
{{#complexType}}
{{name}} = ({{{datatypeWithEnum}}})in.readValue({{complexType}}.class.getClassLoader());
{{/complexType}}
{{^complexType}}
{{name}} = ({{{datatypeWithEnum}}})in.readValue(null);
{{/complexType}}
{{/isContainer}}
{{^isContainer}}
{{name}} = ({{{datatypeWithEnum}}})in.readValue(null);
{{/isContainer}}
{{/vars}}
}

public int describeContents() {
return 0;
}

public static final Parcelable.Creator<{{classname}}> CREATOR = new Parcelable.Creator<{{classname}}>() {
public {{classname}} createFromParcel(Parcel in) {
return new {{classname}}(in);
}
public {{classname}}[] newArray(int size) {
return new {{classname}}[size];
}
};
{{/parcelableModel}}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public Map<String, String> createOptions() {
Map<String, String> options = new HashMap<String, String>(super.createOptions());
options.put(CodegenConstants.LIBRARY, DEFAULT_LIBRARY_VALUE);
options.put(JavaClientCodegen.USE_RX_JAVA, "false");
options.put(JavaClientCodegen.PARCELABLE_MODEL, "false");

return options;
}
Expand Down