Skip to content
Merged
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
13 changes: 10 additions & 3 deletions bin/dart-petstore.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,16 @@ fi

# if you've executed sbt assembly previously it will use that instead.
export JAVA_OPTS="${JAVA_OPTS} -XX:MaxPermSize=256M -Xmx1024M -DloggerPath=conf/log4j.properties"
ags="$@ generate -i modules/swagger-codegen/src/test/resources/2_0/petstore.yaml -l dart -o samples/client/petstore/dart"

# for dart vm lib generation:
#ags="$@ generate -i modules/swagger-codegen/src/test/resources/2_0/petstore.yaml -l dart -o samples/client/petstore/dart --additional-properties browserClient=false"
ags="$@ generate -i modules/swagger-codegen/src/test/resources/2_0/petstore.yaml -l dart -o samples/client/petstore/dart/swagger"

# then options to generate the library for vm would be:
#ags="$@ generate -i modules/swagger-codegen/src/test/resources/2_0/petstore.yaml -l dart -o samples/client/petstore/dart/swagger_vm -DbrowserClient=false -DpubName=swagger_vm"
java $JAVA_OPTS -jar $executable $ags

# There is a proposal to allow importing different libraries depending on the environment:
# https://github.com/munificent/dep-interface-libraries
# When this is implemented there will only be one library.

# The current petstore test will then work for both: the browser library and the vm library.

Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ public void processOpts() {

final String libFolder = sourceFolder + File.separator + "lib";
supportingFiles.add(new SupportingFile("pubspec.mustache", "", "pubspec.yaml"));
supportingFiles.add(new SupportingFile("analysis_options.mustache", "", ".analysis_options"));
supportingFiles.add(new SupportingFile("api_client.mustache", libFolder, "api_client.dart"));
supportingFiles.add(new SupportingFile("api_exception.mustache", libFolder, "api_exception.dart"));
supportingFiles.add(new SupportingFile("api_helper.mustache", libFolder, "api_helper.dart"));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
analyzer:
strong-mode: true
8 changes: 6 additions & 2 deletions modules/swagger-codegen/src/main/resources/dart/api.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,13 @@ class {{classname}} {
Map<String, String> headerParams = {};
Map<String, String> formParams = {};
{{#queryParams}}
if("null" != {{paramName}}) {
{{^required}}
if({{paramName}} != null) {
{{/required}}
queryParams.addAll(_convertParametersForCollectionFormat("{{collectionFormat}}", "{{baseName}}", {{paramName}}));
{{^required}}
}
{{/required}}
{{/queryParams}}
{{#headerParams}}headerParams["{{baseName}}"] = {{paramName}};
{{/headerParams}}
Expand Down Expand Up @@ -84,7 +88,7 @@ class {{classname}} {
if(response.statusCode >= 400) {
throw new ApiException(response.statusCode, response.body);
} else if(response.body != null) {
return {{#returnType}} apiClient.deserialize(response.body, '{{{returnType}}}') {{/returnType}};
return {{#returnType}}apiClient.deserialize(response.body, '{{{returnType}}}') as {{{returnType}}} {{/returnType}};
} else {
return {{#returnType}}null{{/returnType}};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ class ApiClient {
Map<String, String> _defaultHeaderMap = {};
Map<String, Authentication> _authentications = {};

final dson = new Dartson.JSON();
final dson = new Dartson.JSON()
..addTransformer(new DateTimeParser(), DateTime);
final DateFormat _dateFormatter = new DateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");

final _RegList = new RegExp(r'^List<(.*)>$');
Expand Down Expand Up @@ -72,20 +73,18 @@ class ApiClient {
Match match;
if (value is List &&
(match = _RegList.firstMatch(targetType)) != null) {
var valueL = value as List;
var newTargetType = match[1];
return valueL.map((v) => _deserialize(v, newTargetType)).toList();
return value.map((v) => _deserialize(v, newTargetType)).toList();
} else if (value is Map &&
(match = _RegMap.firstMatch(targetType)) != null) {
var valueM = value as Map;
var newTargetType = match[1];
return new Map.fromIterables(valueM.keys,
valueM.values.map((v) => _deserialize(v, newTargetType)));
return new Map.fromIterables(value.keys,
value.values.map((v) => _deserialize(v, newTargetType)));
}
}
}
} catch(e) {
// Just throw the ApiException below
} catch (e, stack) {
throw new ApiException.withInner(500, 'Exception during deserialization.', e, stack);
}
throw new ApiException(500, 'Could not find a suitable class for deserialization');
}
Expand Down Expand Up @@ -116,7 +115,7 @@ class ApiClient {
// If collectionFormat is 'multi' a key might appear multiple times.
Future<Response> invokeAPI(String path,
String method,
List<QueryParam> queryParams,
Iterable<QueryParam> queryParams,
Object body,
Map<String, String> headerParams,
Map<String, String> formParams,
Expand Down Expand Up @@ -152,6 +151,8 @@ class ApiClient {
return client.put(url, headers: headerParams, body: msgBody);
case "DELETE":
return client.delete(url, headers: headerParams);
case "PATCH":
return client.patch(url, headers: headerParams, body: msgBody);
default:
return client.get(url, headers: headerParams);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,21 @@ part of {{pubName}}.api;
class ApiException implements Exception {
int code = 0;
String message = null;
Exception innerException = null;
StackTrace stackTrace = null;

ApiException(this.code, this.message);

}
ApiException.withInner(this.code, this.message, this.innerException, this.stackTrace);

String toString() {
if (message == null) return "ApiException";

if (innerException == null) {
return "ApiException $code: $message";
}

return "ApiException $code: $message (Inner exception: ${innerException})\n\n" +
stackTrace.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,27 @@ part of {{pubName}}.api;
const _delimiters = const {'csv': ',', 'ssv': ' ', 'tsv': '\t', 'pipes': '|'};

// port from Java version
List<QueryParam> _convertParametersForCollectionFormat(
Iterable<QueryParam> _convertParametersForCollectionFormat(
String collectionFormat, String name, dynamic value) {
var params = [];
var params = <QueryParam>[];

// preconditions
if (name == null || name.isEmpty || value == null) return params;

if (value is! List) {
params.add(new QueryParam(name, value as String));
params.add(new QueryParam(name, '$value'));
return params;
}

List<String> values = value as List<String>;
List values = value as List;

// get the collection format
collectionFormat = (collectionFormat == null || collectionFormat.isEmpty)
? "csv"
: collectionFormat; // default: csv

if (collectionFormat == "multi") {
return values.map((v) => new QueryParam(name, v));
return values.map((v) => new QueryParam(name, '$v'));
}

String delimiter = _delimiters[collectionFormat] ?? ",";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'dart:convert';{{#browserClient}}
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:intl/intl.dart';

part 'api_client.dart';
Expand Down
2 changes: 2 additions & 0 deletions samples/client/petstore/dart/.analysis_options
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
analyzer:
strong-mode: true
23 changes: 23 additions & 0 deletions samples/client/petstore/dart/.swagger-codegen-ignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Swagger Codegen Ignore
# Generated by swagger-codegen https://github.com/swagger-api/swagger-codegen

# Use this file to prevent files from being overwritten by the generator.
# The patterns follow closely to .gitignore or .dockerignore.

# As an example, the C# client generator defines ApiClient.cs.
# You can make changes and tell Swagger Codgen to ignore just this file by uncommenting the following line:
#ApiClient.cs

# You can match any string of characters against a directory, file or extension with a single asterisk (*):
#foo/*/qux
# The above matches foo/bar/qux and foo/baz/qux, but not foo/bar/baz/qux

# You can recursively match patterns against a directory, file or extension with a double asterisk (**):
#foo/**/qux
# This matches foo/bar/qux, foo/baz/qux, and foo/bar/baz/qux

# You can also negate patterns with an exclamation (!).
# For example, you can ignore all files in a docs folder with the file extension .md:
#docs/*.md
# Then explicitly reverse the ignore rule for a single file:
#!docs/README.md
9 changes: 0 additions & 9 deletions samples/client/petstore/dart/lib/api_exception.dart

This file was deleted.

2 changes: 2 additions & 0 deletions samples/client/petstore/dart/petstore/.analysis_options
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
analyzer:
strong-mode: true
9 changes: 9 additions & 0 deletions samples/client/petstore/dart/petstore/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
name: petstore_client
version: 1.0.0
description: Petstore client using swagger API library
dependencies:
swagger:
path: ../swagger

dev_dependencies:
guinness: '^0.1.17'
Original file line number Diff line number Diff line change
Expand Up @@ -71,34 +71,6 @@ testPetApi() {
});
});

it('finds pets by tag', () async {
var snowyId = newId();
var grumpyId = newId();
var snowyTags = [
new Tag()
..id = newId()
..name = 'terrier'
];
var grumpyTags = [
new Tag()
..id = newId()
..name = 'grumpy'
];
await petApi.addPet(new Pet()
..id = snowyId
..name = 'Snowy'
..tags = snowyTags);
await petApi.addPet(new Pet()
..id = grumpyId
..name = 'Grumpy Cat'
..tags = grumpyTags);

var pets = await petApi.findPetsByTags(['grumpy']);
var petIds = pets.map((pet) => pet.id).toList();
expect(petIds).toContain(grumpyId);
expect(petIds).not.toContain(snowyId);
});

it('uploads a pet image', () async {
var id = newId();
await petApi.addPet(new Pet()..id = id);
Expand Down
Loading