Skip to content

Commit

Permalink
improve enum support in java okhttp-gson client
Browse files Browse the repository at this point in the history
  • Loading branch information
xhh authored and wing328 committed Apr 27, 2016
1 parent 39267a2 commit 82d23be
Show file tree
Hide file tree
Showing 21 changed files with 513 additions and 253 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,78 +8,7 @@ import com.google.gson.annotations.SerializedName;

{{#serializableModel}}import java.io.Serializable;{{/serializableModel}}
{{#models}}

{{#model}}{{#description}}
/**
* {{description}}
**/{{/description}}
{{#description}}@ApiModel(description = "{{{description}}}"){{/description}}
public class {{classname}} {{#parent}}extends {{{parent}}}{{/parent}} {{#serializableModel}}implements Serializable{{/serializableModel}} {
{{#vars}}{{#isEnum}}

{{>libraries/okhttp-gson/enumClass}}{{/isEnum}}{{#items.isEnum}}{{#items}}

{{>libraries/okhttp-gson/enumClass}}{{/items}}{{/items.isEnum}}
@SerializedName("{{baseName}}")
private {{{datatypeWithEnum}}} {{name}} = {{{defaultValue}}};
{{/vars}}

{{#vars}}
/**{{#description}}
* {{{description}}}{{/description}}{{#minimum}}
* minimum: {{minimum}}{{/minimum}}{{#maximum}}
* maximum: {{maximum}}{{/maximum}}
**/
@ApiModelProperty({{#required}}required = {{required}}, {{/required}}value = "{{{description}}}")
public {{{datatypeWithEnum}}} {{getter}}() {
return {{name}};
}{{^isReadOnly}}
public void {{setter}}({{{datatypeWithEnum}}} {{name}}) {
this.{{name}} = {{name}};
}{{/isReadOnly}}

{{/vars}}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}{{#hasVars}}
{{classname}} {{classVarName}} = ({{classname}}) o;
return {{#vars}}Objects.equals(this.{{name}}, {{classVarName}}.{{name}}){{#hasMore}} &&
{{/hasMore}}{{/vars}}{{#parent}} &&
super.equals(o){{/parent}};{{/hasVars}}{{^hasVars}}
return true;{{/hasVars}}
}

@Override
public int hashCode() {
return Objects.hash({{#vars}}{{name}}{{#hasMore}}, {{/hasMore}}{{/vars}}{{#parent}}{{#hasVars}}, {{/hasVars}}super.hashCode(){{/parent}});
}

@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("class {{classname}} {\n");
{{#parent}}sb.append(" ").append(toIndentedString(super.toString())).append("\n");{{/parent}}
{{#vars}}sb.append(" {{name}}: ").append(toIndentedString({{name}})).append("\n");
{{/vars}}sb.append("}");
return sb.toString();
}

/**
* Convert the given object to string with each line indented by 4 spaces
* (except the first line).
*/
private String toIndentedString(Object o) {
if (o == null) {
return "null";
}
return o.toString().replace("\n", "\n ");
}
}
{{#model}}
{{#isEnum}}{{>modelEnum}}{{/isEnum}}{{^isEnum}}{{>pojo}}{{/isEnum}}
{{/model}}
{{/models}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* {{^description}}Gets or Sets {{name}}{{/description}}{{#description}}{{description}}{{/description}}
*/
public enum {{#datatypeWithEnum}}{{.}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{classname}}{{/datatypeWithEnum}} {
{{#allowableValues}}{{#enumVars}}@SerializedName({{#isInteger}}"{{/isInteger}}{{#isDouble}}"{{/isDouble}}{{{value}}}{{#isInteger}}"{{/isInteger}}{{#isDouble}}"{{/isDouble}})
{{{name}}}({{{value}}}){{^-last}},

{{/-last}}{{#-last}};{{/-last}}{{/enumVars}}{{/allowableValues}}

private {{dataType}} value;

{{#datatypeWithEnum}}{{.}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{classname}}{{/datatypeWithEnum}}({{dataType}} value) {
this.value = value;
}

@Override
public String toString() {
return String.valueOf(value);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* {{^description}}Gets or Sets {{{name}}}{{/description}}{{#description}}{{{description}}}{{/description}}
*/
public enum {{#datatypeWithEnum}}{{.}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{classname}}{{/datatypeWithEnum}} {
{{#allowableValues}}{{#enumVars}}@SerializedName({{#isInteger}}"{{/isInteger}}{{#isDouble}}"{{/isDouble}}{{{value}}}{{#isInteger}}"{{/isInteger}}{{#isDouble}}"{{/isDouble}})
{{{name}}}({{{value}}}){{^-last}},

{{/-last}}{{#-last}};{{/-last}}{{/enumVars}}{{/allowableValues}}

private {{datatype}} value;

{{#datatypeWithEnum}}{{.}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{classname}}{{/datatypeWithEnum}}({{datatype}} value) {
this.value = value;
}

@Override
public String toString() {
return String.valueOf(value);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/**
* {{#description}}{{description}}{{/description}}{{^description}}{{classname}}{{/description}}
*/{{#description}}
@ApiModel(description = "{{{description}}}"){{/description}}
public class {{classname}} {{#parent}}extends {{{parent}}}{{/parent}} {{#serializableModel}}implements Serializable{{/serializableModel}} {
{{#vars}}{{#isEnum}}

{{>libraries/okhttp-gson/modelInnerEnum}}{{/isEnum}}{{#items.isEnum}}{{#items}}

{{>libraries/okhttp-gson/modelInnerEnum}}{{/items}}{{/items.isEnum}}
@SerializedName("{{baseName}}")
private {{{datatypeWithEnum}}} {{name}} = {{{defaultValue}}};
{{/vars}}

{{#vars}}
/**{{#description}}
* {{{description}}}{{/description}}{{#minimum}}
* minimum: {{minimum}}{{/minimum}}{{#maximum}}
* maximum: {{maximum}}{{/maximum}}
**/
@ApiModelProperty({{#required}}required = {{required}}, {{/required}}value = "{{{description}}}")
public {{{datatypeWithEnum}}} {{getter}}() {
return {{name}};
}{{^isReadOnly}}
public void {{setter}}({{{datatypeWithEnum}}} {{name}}) {
this.{{name}} = {{name}};
}{{/isReadOnly}}

{{/vars}}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}{{#hasVars}}
{{classname}} {{classVarName}} = ({{classname}}) o;
return {{#vars}}Objects.equals(this.{{name}}, {{classVarName}}.{{name}}){{#hasMore}} &&
{{/hasMore}}{{/vars}}{{#parent}} &&
super.equals(o){{/parent}};{{/hasVars}}{{^hasVars}}
return true;{{/hasVars}}
}

@Override
public int hashCode() {
return Objects.hash({{#vars}}{{name}}{{#hasMore}}, {{/hasMore}}{{/vars}}{{#parent}}{{#hasVars}}, {{/hasVars}}super.hashCode(){{/parent}});
}

@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("class {{classname}} {\n");
{{#parent}}sb.append(" ").append(toIndentedString(super.toString())).append("\n");{{/parent}}
{{#vars}}sb.append(" {{name}}: ").append(toIndentedString({{name}})).append("\n");
{{/vars}}sb.append("}");
return sb.toString();
}

/**
* Convert the given object to string with each line indented by 4 spaces
* (except the first line).
*/
private String toIndentedString(Object o) {
if (o == null) {
return "null";
}
return o.toString().replace("\n", "\n ");
}
}
6 changes: 3 additions & 3 deletions samples/client/petstore/java/okhttp-gson/git_push.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ git_repo_id=$2
release_note=$3

if [ "$git_user_id" = "" ]; then
git_user_id="YOUR_GIT_USR_ID"
git_user_id=""
echo "[INFO] No command line input provided. Set \$git_user_id to $git_user_id"
fi

if [ "$git_repo_id" = "" ]; then
git_repo_id="YOUR_GIT_REPO_ID"
git_repo_id=""
echo "[INFO] No command line input provided. Set \$git_repo_id to $git_repo_id"
fi

if [ "$release_note" = "" ]; then
release_note="Minor update"
release_note=""
echo "[INFO] No command line input provided. Set \$release_note to $release_note"
fi

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@
import com.google.gson.annotations.SerializedName;





/**
* Animal
*/
public class Animal {

@SerializedName("className")
private String className = null;



/**
**/
@ApiModelProperty(required = true, value = "")
Expand All @@ -25,6 +27,7 @@ public void setClassName(String className) {
this.className = className;
}



@Override
public boolean equals(Object o) {
Expand Down Expand Up @@ -64,3 +67,4 @@ private String toIndentedString(Object o) {
return o.toString().replace("\n", "\n ");
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,19 @@
import com.google.gson.annotations.SerializedName;





/**
* Cat
*/
public class Cat extends Animal {

@SerializedName("className")
private String className = null;

@SerializedName("declawed")
private Boolean declawed = null;



/**
**/
@ApiModelProperty(required = true, value = "")
Expand All @@ -29,6 +31,7 @@ public void setClassName(String className) {
this.className = className;
}


/**
**/
@ApiModelProperty(value = "")
Expand All @@ -39,6 +42,7 @@ public void setDeclawed(Boolean declawed) {
this.declawed = declawed;
}



@Override
public boolean equals(Object o) {
Expand Down Expand Up @@ -81,3 +85,4 @@ private String toIndentedString(Object o) {
return o.toString().replace("\n", "\n ");
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
import com.google.gson.annotations.SerializedName;





/**
* Category
*/
public class Category {

@SerializedName("id")
Expand Down Expand Up @@ -79,3 +79,4 @@ private String toIndentedString(Object o) {
return o.toString().replace("\n", "\n ");
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,19 @@
import com.google.gson.annotations.SerializedName;





/**
* Dog
*/
public class Dog extends Animal {

@SerializedName("className")
private String className = null;

@SerializedName("breed")
private String breed = null;



/**
**/
@ApiModelProperty(required = true, value = "")
Expand All @@ -29,6 +31,7 @@ public void setClassName(String className) {
this.className = className;
}


/**
**/
@ApiModelProperty(value = "")
Expand All @@ -39,6 +42,7 @@ public void setBreed(String breed) {
this.breed = breed;
}



@Override
public boolean equals(Object o) {
Expand Down Expand Up @@ -81,3 +85,4 @@ private String toIndentedString(Object o) {
return o.toString().replace("\n", "\n ");
}
}

Loading

0 comments on commit 82d23be

Please sign in to comment.