Skip to content

Commit

Permalink
Added support for enums in Dart.
Browse files Browse the repository at this point in the history
  • Loading branch information
pylaligand committed Sep 19, 2017
1 parent 8067612 commit b245242
Show file tree
Hide file tree
Showing 16 changed files with 126 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,22 @@
import io.swagger.codegen.CliOption;
import io.swagger.codegen.CodegenConfig;
import io.swagger.codegen.CodegenConstants;
import io.swagger.codegen.CodegenModel;
import io.swagger.codegen.CodegenType;
import io.swagger.codegen.DefaultCodegen;
import io.swagger.codegen.SupportingFile;
import io.swagger.models.Model;
import io.swagger.models.properties.ArrayProperty;
import io.swagger.models.properties.MapProperty;
import io.swagger.models.properties.Property;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

public class DartClientCodegen extends DefaultCodegen implements CodegenConfig {
public static final String BROWSER_CLIENT = "browserClient";
Expand Down Expand Up @@ -182,7 +187,7 @@ public void processOpts() {


@Override
public String escapeReservedWord(String name) {
public String escapeReservedWord(String name) {
if(this.reservedWordsMappings().containsKey(name)) {
return this.reservedWordsMappings().get(name);
}
Expand Down Expand Up @@ -300,6 +305,60 @@ public String getSwaggerType(Property p) {
return toModelName(type);
}

@Override
public Map<String, Object> postProcessModels(Map<String, Object> objs) {
Map<String, Object> result = postProcessModelsEnum(objs);

// Build a list of all the enum models.
List<Object> models = (List<Object>) objs.get("models");
// Check if there already exists a list.
Object storedTransformers = additionalProperties.get("transformers");
List<Map<String, String>> enumTransformers = storedTransformers != null ?
(List<Map<String, String>>) storedTransformers :
new ArrayList<Map<String, String>>();
for (Object modelObject : models) {
Map<String, Object> modelMap = (Map<String, Object>) modelObject;
CodegenModel model = (CodegenModel) modelMap.get("model");
if (model.isEnum && model.allowableValues != null) {
Map<String, String> enumObject = new HashMap<String, String>();
String className = model.classname;
enumObject.put("className", className);
enumObject.put("transformer", className + "TypeTransformer");
enumTransformers.add(enumObject);
}
}
// Store the list as an additional property.
additionalProperties.put("transformers", enumTransformers);
return result;
}

@Override
public String toEnumVarName(String value, String datatype) {
if (value.length() == 0) {
return "EMPTY";
}

// The lone difference with the super version is the replacement of non
// word characters with '__' instead of '_'. This is to handle negative
// numbers, so that e.g. -1 is transformed into '__1' and not '_1' like
// 1 is.
String var = value.replaceAll("\\W+", "__").toUpperCase();
if (var.matches("\\d.*")) {
return "_" + var;
} else {
return var;
}
}

@Override
public String toEnumValue(String value, String datatype) {
if ("number".equalsIgnoreCase(datatype) || "int".equalsIgnoreCase(datatype)) {
return value;
} else {
return "\"" + escapeText(value) + "\"";
}
}

@Override
public String toOperationId(String operationId) {
// method name cannot use reserved keyword, e.g. return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ class ApiClient {
Map<String, Authentication> _authentications = {};

final dson = new Dartson.JSON()
..addTransformer(new DateTimeParser(), DateTime);
{{#transformers}}..addTransformer(new {{transformer}}(), {{className}})
{{/transformers}}..addTransformer(new DateTimeParser(), DateTime);

final _RegList = new RegExp(r'^List<(.*)>$');
final _RegMap = new RegExp(r'^Map<String,(.*)>$');
Expand Down Expand Up @@ -46,7 +47,16 @@ class ApiClient {
{{#models}}
{{#model}}
case '{{classname}}':
{{#isEnum}}
// Enclose the value in a list so that Dartson can use a transformer
// to decode it.
final listValue = [value];
final List<dynamic> listResult = dson.map(listValue, []);
return listResult[0];
{{/isEnum}}
{{^isEnum}}
return dson.map(value, new {{classname}}());
{{/isEnum}}
{{/model}}
{{/models}}
default:
Expand Down Expand Up @@ -116,7 +126,7 @@ class ApiClient {
headerParams['Content-Type'] = contentType;
if(body is MultipartRequest) {
var request = new MultipartRequest(method, Uri.parse(url));
var request = new MultipartRequest(method, Uri.parse(url));
request.fields.addAll(body.fields);
request.files.addAll(body.files);
request.headers.addAll(body.headers);
Expand All @@ -141,7 +151,7 @@ class ApiClient {
}

/// Update query and header parameters based on authentication settings.
/// @param authNames The authentications to apply
/// @param authNames The authentications to apply
void _updateParamsForAuth(List<String> authNames, List<QueryParam> queryParams, Map<String, String> headerParams) {
authNames.forEach((authName) {
Authentication auth = _authentications[authName];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'package:http/browser_client.dart';{{/browserClient}}
import 'package:http/http.dart';
import 'package:dartson/dartson.dart';
import 'package:dartson/transformers/date_time.dart';
import 'package:dartson/type_transformer.dart';

part 'api_client.dart';
part 'api_helper.dart';
Expand All @@ -21,4 +22,3 @@ part 'auth/http_basic_auth.dart';
{{/model}}{{/models}}

ApiClient defaultApiClient = new ApiClient();

14 changes: 14 additions & 0 deletions modules/swagger-codegen/src/main/resources/dart/class.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
@Entity()
class {{classname}} {
{{#vars}}{{#description}}/* {{{description}}} */{{/description}}
@Property(name: '{{baseName}}')
{{{datatype}}} {{name}} = {{{defaultValue}}};
{{#allowableValues}}{{#min}} // range from {{min}} to {{max}}{{/min}}//{{^min}}enum {{name}}Enum { {{#values}} {{.}}, {{/values}} };{{/min}}{{/allowableValues}}
{{/vars}}
{{classname}}();

@override
String toString() {
return '{{classname}}[{{#vars}}{{name}}=${{name}}, {{/vars}}]';
}
}
26 changes: 26 additions & 0 deletions modules/swagger-codegen/src/main/resources/dart/enum.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
@Entity()
enum {{classname}} {
{{#allowableValues}}{{#enumVars}}{{name}}{{^-last}},{{/-last}}
{{/enumVars}}{{/allowableValues}}
}

class {{classname}}TypeTransformer extends TypeTransformer<{{classname}}> {
@override
dynamic encode({{classname}} data) {
switch(data) {
{{#allowableValues}}{{#enumVars}}case {{classname}}.{{name}}: return {{value}};
{{/enumVars}}{{/allowableValues}}
default: throw('Unknown enum value to encode: $data');
}
}

@override
{{classname}} decode(dynamic data) {
switch (data) {
{{#allowableValues}}{{#enumVars}}case {{value}}: return {{classname}}.{{name}};
{{/enumVars}}{{/allowableValues}}
default: throw('Unknown enum value to decode: $data');
}
}
}
22 changes: 5 additions & 17 deletions modules/swagger-codegen/src/main/resources/dart/model.mustache
Original file line number Diff line number Diff line change
@@ -1,19 +1,7 @@
part of {{pubName}}.api;

{{#models}}{{#model}}
@Entity()
class {{classname}} {
{{#vars}}{{#description}}/* {{{description}}} */{{/description}}
@Property(name: '{{baseName}}')
{{{datatype}}} {{name}} = {{{defaultValue}}};
{{#allowableValues}}{{#min}} // range from {{min}} to {{max}}{{/min}}//{{^min}}enum {{name}}Enum { {{#values}} {{.}}, {{/values}} };{{/min}}{{/allowableValues}}
{{/vars}}
{{classname}}();

@override
String toString() {
return '{{classname}}[{{#vars}}{{name}}=${{name}}, {{/vars}}]';
}

}
{{/model}}{{/models}}
{{#models}}
{{#model}}
{{#isEnum}}{{>enum}}{{/isEnum}}{{^isEnum}}{{>class}}{{/isEnum}}
{{/model}}
{{/models}}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.2.3-SNAPSHOT
2.3.0-SNAPSHOT
2 changes: 1 addition & 1 deletion samples/client/petstore/dart/swagger/docs/StoreApi.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ This endpoint does not need any parameter.

### Return type

[**Map<String, int>**](Map.md)
**Map<String, int>**

### Authorization

Expand Down
2 changes: 1 addition & 1 deletion samples/client/petstore/dart/swagger/lib/api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'package:http/browser_client.dart';
import 'package:http/http.dart';
import 'package:dartson/dartson.dart';
import 'package:dartson/transformers/date_time.dart';
import 'package:dartson/type_transformer.dart';

part 'api_client.dart';
part 'api_helper.dart';
Expand All @@ -28,4 +29,3 @@ part 'model/user.dart';


ApiClient defaultApiClient = new ApiClient();

6 changes: 3 additions & 3 deletions samples/client/petstore/dart/swagger/lib/api_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class ApiClient {
Map<String, Authentication> _authentications = {};

final dson = new Dartson.JSON()
..addTransformer(new DateTimeParser(), DateTime);
..addTransformer(new DateTimeParser(), DateTime);

final _RegList = new RegExp(r'^List<(.*)>$');
final _RegMap = new RegExp(r'^Map<String,(.*)>$');
Expand Down Expand Up @@ -121,7 +121,7 @@ class ApiClient {
headerParams['Content-Type'] = contentType;

if(body is MultipartRequest) {
var request = new MultipartRequest(method, Uri.parse(url));
var request = new MultipartRequest(method, Uri.parse(url));
request.fields.addAll(body.fields);
request.files.addAll(body.files);
request.headers.addAll(body.headers);
Expand All @@ -146,7 +146,7 @@ class ApiClient {
}

/// Update query and header parameters based on authentication settings.
/// @param authNames The authentications to apply
/// @param authNames The authentications to apply
void _updateParamsForAuth(List<String> authNames, List<QueryParam> queryParams, Map<String, String> headerParams) {
authNames.forEach((authName) {
Authentication auth = _authentications[authName];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
part of swagger.api;


@Entity()
class ApiResponse {

Expand All @@ -21,6 +20,5 @@ class ApiResponse {
String toString() {
return 'ApiResponse[code=$code, type=$type, message=$message, ]';
}

}

2 changes: 0 additions & 2 deletions samples/client/petstore/dart/swagger/lib/model/category.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
part of swagger.api;


@Entity()
class Category {

Expand All @@ -17,6 +16,5 @@ class Category {
String toString() {
return 'Category[id=$id, name=$name, ]';
}

}

2 changes: 0 additions & 2 deletions samples/client/petstore/dart/swagger/lib/model/order.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
part of swagger.api;


@Entity()
class Order {

Expand Down Expand Up @@ -33,6 +32,5 @@ class Order {
String toString() {
return 'Order[id=$id, petId=$petId, quantity=$quantity, shipDate=$shipDate, status=$status, complete=$complete, ]';
}

}

2 changes: 0 additions & 2 deletions samples/client/petstore/dart/swagger/lib/model/pet.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
part of swagger.api;


@Entity()
class Pet {

Expand Down Expand Up @@ -33,6 +32,5 @@ class Pet {
String toString() {
return 'Pet[id=$id, category=$category, name=$name, photoUrls=$photoUrls, tags=$tags, status=$status, ]';
}

}

2 changes: 0 additions & 2 deletions samples/client/petstore/dart/swagger/lib/model/tag.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
part of swagger.api;


@Entity()
class Tag {

Expand All @@ -17,6 +16,5 @@ class Tag {
String toString() {
return 'Tag[id=$id, name=$name, ]';
}

}

2 changes: 0 additions & 2 deletions samples/client/petstore/dart/swagger/lib/model/user.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
part of swagger.api;


@Entity()
class User {

Expand Down Expand Up @@ -41,6 +40,5 @@ class User {
String toString() {
return 'User[id=$id, username=$username, firstName=$firstName, lastName=$lastName, email=$email, password=$password, phone=$phone, userStatus=$userStatus, ]';
}

}

0 comments on commit b245242

Please sign in to comment.