Skip to content

Commit 0cf1c3c

Browse files
committed
Merge pull request #536 from wing328/android_better_naming
better naming convention for android-java codegen
2 parents 048bea6 + fd602af commit 0cf1c3c

File tree

3 files changed

+49
-4
lines changed

3 files changed

+49
-4
lines changed

modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/languages/AndroidClientCodegen.java

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,4 +118,49 @@ public String getSwaggerType(Property p) {
118118
type = swaggerType;
119119
return toModelName(type);
120120
}
121-
}
121+
122+
@Override
123+
public String toVarName(String name) {
124+
// replace - with _ e.g. created-at => created_at
125+
name = name.replaceAll("-", "_");
126+
127+
// if it's all uppper case, do nothing
128+
if (name.matches("^[A-Z_]*$"))
129+
return name;
130+
131+
// camelize (lower first character) the variable name
132+
// pet_id => petId
133+
name = camelize(name, true);
134+
135+
// for reserved word or word starting with number, append _
136+
if(reservedWords.contains(name) || name.matches("^\\d.*"))
137+
name = escapeReservedWord(name);
138+
139+
return name;
140+
}
141+
142+
@Override
143+
public String toParamName(String name) {
144+
// should be the same as variable name
145+
return toVarName(name);
146+
}
147+
148+
@Override
149+
public String toModelName(String name) {
150+
// model name cannot use reserved keyword, e.g. return
151+
if(reservedWords.contains(name))
152+
throw new RuntimeException(name + " (reserved word) cannot be used as a model name");
153+
154+
// camelize the model name
155+
// phone_number => PhoneNumber
156+
return camelize(name);
157+
}
158+
159+
@Override
160+
public String toModelFilename(String name) {
161+
// should be the same as the model name
162+
return toModelName(name);
163+
}
164+
165+
166+
}

modules/swagger-codegen/src/main/resources/android-java/model.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public class {{classname}} {{#parent}}extends {{{parent}}}{{/parent}} {
2727
* maximum: {{maximum}}{{/maximum}}
2828
**/
2929
@ApiModelProperty(required = {{required}}, value = "{{{description}}}")
30-
@JsonProperty("{{name}}")
30+
@JsonProperty("{{baseName}}")
3131
public {{{datatypeWithEnum}}} {{getter}}() {
3232
return {{name}};
3333
}

samples/client/petstore/android-java/src/main/java/io/swagger/client/api/PetApi.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ public void updatePetWithForm (String petId, String name, String status) throws
383383
}
384384

385385

386-
public void deletePet (String api_key, Long petId) throws ApiException {
386+
public void deletePet (String apiKey, Long petId) throws ApiException {
387387
Object postBody = null;
388388

389389

@@ -400,7 +400,7 @@ public void deletePet (String api_key, Long petId) throws ApiException {
400400

401401

402402

403-
headerParams.put("api_key", ApiInvoker.parameterToString(api_key));
403+
headerParams.put("api_key", ApiInvoker.parameterToString(apiKey));
404404

405405

406406
String[] contentTypes = {

0 commit comments

Comments
 (0)