Skip to content

Commit

Permalink
[dart-dio] Fixes issues with DateTime import and form date paramaterT…
Browse files Browse the repository at this point in the history
…oString function (#4929)

- due to the addition of the timemachine library, models were importing ‘DateTime’ when using the core library, which is not a valid import.

- the parameterToString function was copied from the dart2 generator and had some errors when some enums were classes.
  • Loading branch information
josh-burton authored and wing328 committed Jan 17, 2020
1 parent bb10e8c commit f48325a
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,6 @@ public void processOpts() {
additionalProperties.put("core", "true");
typeMapping.put("Date", "DateTime");
typeMapping.put("date", "DateTime");
importMapping.put("DateTime", "DateTime");
importMapping.put("OffsetDateTime", "DateTime");
} else if ("timemachine".equals(dateLibrary)) {
additionalProperties.put("timeMachine", "true");
typeMapping.put("date", "LocalDate");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class {{classname}} {
/// {{notes}}
Future<Response{{#returnType}}<{{{returnType}}}>{{/returnType}}>{{nickname}}({{#allParams}}{{#required}}{{{dataType}}} {{paramName}},{{/required}}{{/allParams}}{ {{#allParams}}{{^required}}{{{dataType}}} {{paramName}},{{/required}}{{/allParams}}CancelToken cancelToken, Map<String, String> headers,}) async {
String path = "{{{path}}}"{{#pathParams}}.replaceAll("{" + "{{baseName}}" + "}", {{{paramName}}}.toString()){{/pathParams}};
String path = "{{{path}}}"{{#pathParams}}.replaceAll("{" + "{{baseName}}" + "}", {{{paramName}}}.toString()){{/pathParams}};

Map<String, dynamic> queryParams = {};
Map<String, String> headerParams = Map.from(headers ?? {});
Expand All @@ -46,7 +46,7 @@ class {{classname}} {
{{#isMultipart}}
{{^isFile}}
if ({{paramName}} != null) {
formData['{{baseName}}'] = parameterToString({{paramName}});
formData['{{baseName}}'] = parameterToString(_serializers, {{paramName}});
}
{{/isFile}}
{{#isFile}}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
import 'dart:convert';

import 'package:built_value/serializer.dart';

/// Format the given parameter object into string.
String parameterToString(dynamic value) {
if (value == null) {
return '';
} else if (value is DateTime) {
return value.toUtc().toIso8601String();
{{#models}}
{{#model}}
{{#isEnum}}
} else if (value is {{classname}}) {
return {{classname}}TypeTransformer().encode(value).toString();
{{/isEnum}}
{{/model}}
{{/models}}
} else {
return value.toString();
}
String parameterToString(Serializers serializers, dynamic value) {
if (value == null) {
return '';
} else if (value is String || value is num) {
return value.toString();
} else {
return json.encode(serializers.serialize(value));
}
}
52 changes: 26 additions & 26 deletions samples/client/petstore/dart-dio/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,36 +57,36 @@ All URIs are relative to *http://petstore.swagger.io/v2*

Class | Method | HTTP request | Description
------------ | ------------- | ------------- | -------------
*PetApi* | [**addPet**](doc\/PetApi.md#addpet) | **post** /pet | Add a new pet to the store
*PetApi* | [**deletePet**](doc\/PetApi.md#deletepet) | **delete** /pet/{petId} | Deletes a pet
*PetApi* | [**findPetsByStatus**](doc\/PetApi.md#findpetsbystatus) | **get** /pet/findByStatus | Finds Pets by status
*PetApi* | [**findPetsByTags**](doc\/PetApi.md#findpetsbytags) | **get** /pet/findByTags | Finds Pets by tags
*PetApi* | [**getPetById**](doc\/PetApi.md#getpetbyid) | **get** /pet/{petId} | Find pet by ID
*PetApi* | [**updatePet**](doc\/PetApi.md#updatepet) | **put** /pet | Update an existing pet
*PetApi* | [**updatePetWithForm**](doc\/PetApi.md#updatepetwithform) | **post** /pet/{petId} | Updates a pet in the store with form data
*PetApi* | [**uploadFile**](doc\/PetApi.md#uploadfile) | **post** /pet/{petId}/uploadImage | uploads an image
*StoreApi* | [**deleteOrder**](doc\/StoreApi.md#deleteorder) | **delete** /store/order/{orderId} | Delete purchase order by ID
*StoreApi* | [**getInventory**](doc\/StoreApi.md#getinventory) | **get** /store/inventory | Returns pet inventories by status
*StoreApi* | [**getOrderById**](doc\/StoreApi.md#getorderbyid) | **get** /store/order/{orderId} | Find purchase order by ID
*StoreApi* | [**placeOrder**](doc\/StoreApi.md#placeorder) | **post** /store/order | Place an order for a pet
*UserApi* | [**createUser**](doc\/UserApi.md#createuser) | **post** /user | Create user
*UserApi* | [**createUsersWithArrayInput**](doc\/UserApi.md#createuserswitharrayinput) | **post** /user/createWithArray | Creates list of users with given input array
*UserApi* | [**createUsersWithListInput**](doc\/UserApi.md#createuserswithlistinput) | **post** /user/createWithList | Creates list of users with given input array
*UserApi* | [**deleteUser**](doc\/UserApi.md#deleteuser) | **delete** /user/{username} | Delete user
*UserApi* | [**getUserByName**](doc\/UserApi.md#getuserbyname) | **get** /user/{username} | Get user by user name
*UserApi* | [**loginUser**](doc\/UserApi.md#loginuser) | **get** /user/login | Logs user into the system
*UserApi* | [**logoutUser**](doc\/UserApi.md#logoutuser) | **get** /user/logout | Logs out current logged in user session
*UserApi* | [**updateUser**](doc\/UserApi.md#updateuser) | **put** /user/{username} | Updated user
*PetApi* | [**addPet**](doc//PetApi.md#addpet) | **post** /pet | Add a new pet to the store
*PetApi* | [**deletePet**](doc//PetApi.md#deletepet) | **delete** /pet/{petId} | Deletes a pet
*PetApi* | [**findPetsByStatus**](doc//PetApi.md#findpetsbystatus) | **get** /pet/findByStatus | Finds Pets by status
*PetApi* | [**findPetsByTags**](doc//PetApi.md#findpetsbytags) | **get** /pet/findByTags | Finds Pets by tags
*PetApi* | [**getPetById**](doc//PetApi.md#getpetbyid) | **get** /pet/{petId} | Find pet by ID
*PetApi* | [**updatePet**](doc//PetApi.md#updatepet) | **put** /pet | Update an existing pet
*PetApi* | [**updatePetWithForm**](doc//PetApi.md#updatepetwithform) | **post** /pet/{petId} | Updates a pet in the store with form data
*PetApi* | [**uploadFile**](doc//PetApi.md#uploadfile) | **post** /pet/{petId}/uploadImage | uploads an image
*StoreApi* | [**deleteOrder**](doc//StoreApi.md#deleteorder) | **delete** /store/order/{orderId} | Delete purchase order by ID
*StoreApi* | [**getInventory**](doc//StoreApi.md#getinventory) | **get** /store/inventory | Returns pet inventories by status
*StoreApi* | [**getOrderById**](doc//StoreApi.md#getorderbyid) | **get** /store/order/{orderId} | Find purchase order by ID
*StoreApi* | [**placeOrder**](doc//StoreApi.md#placeorder) | **post** /store/order | Place an order for a pet
*UserApi* | [**createUser**](doc//UserApi.md#createuser) | **post** /user | Create user
*UserApi* | [**createUsersWithArrayInput**](doc//UserApi.md#createuserswitharrayinput) | **post** /user/createWithArray | Creates list of users with given input array
*UserApi* | [**createUsersWithListInput**](doc//UserApi.md#createuserswithlistinput) | **post** /user/createWithList | Creates list of users with given input array
*UserApi* | [**deleteUser**](doc//UserApi.md#deleteuser) | **delete** /user/{username} | Delete user
*UserApi* | [**getUserByName**](doc//UserApi.md#getuserbyname) | **get** /user/{username} | Get user by user name
*UserApi* | [**loginUser**](doc//UserApi.md#loginuser) | **get** /user/login | Logs user into the system
*UserApi* | [**logoutUser**](doc//UserApi.md#logoutuser) | **get** /user/logout | Logs out current logged in user session
*UserApi* | [**updateUser**](doc//UserApi.md#updateuser) | **put** /user/{username} | Updated user


## Documentation For Models

- [ApiResponse](doc\/ApiResponse.md)
- [Category](doc\/Category.md)
- [Order](doc\/Order.md)
- [Pet](doc\/Pet.md)
- [Tag](doc\/Tag.md)
- [User](doc\/User.md)
- [ApiResponse](doc//ApiResponse.md)
- [Category](doc//Category.md)
- [Order](doc//Order.md)
- [Pet](doc//Pet.md)
- [Tag](doc//Tag.md)
- [User](doc//User.md)


## Documentation For Authorization
Expand Down
18 changes: 9 additions & 9 deletions samples/client/petstore/dart-dio/lib/api/pet_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class PetApi {
///
Future<Response>addPet(Pet body,{ CancelToken cancelToken, Map<String, String> headers,}) async {

String path = "/pet";
String path = "/pet";

Map<String, dynamic> queryParams = {};
Map<String, String> headerParams = Map.from(headers ?? {});
Expand Down Expand Up @@ -54,7 +54,7 @@ class PetApi {
///
Future<Response>deletePet(int petId,{ String apiKey,CancelToken cancelToken, Map<String, String> headers,}) async {

String path = "/pet/{petId}".replaceAll("{" + "petId" + "}", petId.toString());
String path = "/pet/{petId}".replaceAll("{" + "petId" + "}", petId.toString());

Map<String, dynamic> queryParams = {};
Map<String, String> headerParams = Map.from(headers ?? {});
Expand Down Expand Up @@ -85,7 +85,7 @@ class PetApi {
/// Multiple status values can be provided with comma separated strings
Future<Response<List<Pet>>>findPetsByStatus(List<String> status,{ CancelToken cancelToken, Map<String, String> headers,}) async {

String path = "/pet/findByStatus";
String path = "/pet/findByStatus";

Map<String, dynamic> queryParams = {};
Map<String, String> headerParams = Map.from(headers ?? {});
Expand Down Expand Up @@ -131,7 +131,7 @@ class PetApi {
/// Multiple tags can be provided with comma separated strings. Use tag1, tag2, tag3 for testing.
Future<Response<List<Pet>>>findPetsByTags(List<String> tags,{ CancelToken cancelToken, Map<String, String> headers,}) async {

String path = "/pet/findByTags";
String path = "/pet/findByTags";

Map<String, dynamic> queryParams = {};
Map<String, String> headerParams = Map.from(headers ?? {});
Expand Down Expand Up @@ -177,7 +177,7 @@ class PetApi {
/// Returns a single pet
Future<Response<Pet>>getPetById(int petId,{ CancelToken cancelToken, Map<String, String> headers,}) async {

String path = "/pet/{petId}".replaceAll("{" + "petId" + "}", petId.toString());
String path = "/pet/{petId}".replaceAll("{" + "petId" + "}", petId.toString());

Map<String, dynamic> queryParams = {};
Map<String, String> headerParams = Map.from(headers ?? {});
Expand Down Expand Up @@ -221,7 +221,7 @@ class PetApi {
///
Future<Response>updatePet(Pet body,{ CancelToken cancelToken, Map<String, String> headers,}) async {

String path = "/pet";
String path = "/pet";

Map<String, dynamic> queryParams = {};
Map<String, String> headerParams = Map.from(headers ?? {});
Expand Down Expand Up @@ -254,7 +254,7 @@ class PetApi {
///
Future<Response>updatePetWithForm(int petId,{ String name,String status,CancelToken cancelToken, Map<String, String> headers,}) async {

String path = "/pet/{petId}".replaceAll("{" + "petId" + "}", petId.toString());
String path = "/pet/{petId}".replaceAll("{" + "petId" + "}", petId.toString());

Map<String, dynamic> queryParams = {};
Map<String, String> headerParams = Map.from(headers ?? {});
Expand Down Expand Up @@ -286,7 +286,7 @@ class PetApi {
///
Future<Response<ApiResponse>>uploadFile(int petId,{ String additionalMetadata,Uint8List file,CancelToken cancelToken, Map<String, String> headers,}) async {

String path = "/pet/{petId}/uploadImage".replaceAll("{" + "petId" + "}", petId.toString());
String path = "/pet/{petId}/uploadImage".replaceAll("{" + "petId" + "}", petId.toString());

Map<String, dynamic> queryParams = {};
Map<String, String> headerParams = Map.from(headers ?? {});
Expand All @@ -299,7 +299,7 @@ class PetApi {

Map<String, dynamic> formData = {};
if (additionalMetadata != null) {
formData['additionalMetadata'] = parameterToString(additionalMetadata);
formData['additionalMetadata'] = parameterToString(_serializers, additionalMetadata);
}
if (file != null) {
formData['file'] = MultipartFile.fromBytes(file, filename: "file");
Expand Down
8 changes: 4 additions & 4 deletions samples/client/petstore/dart-dio/lib/api/store_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class StoreApi {
/// For valid response try integer IDs with value &lt; 1000. Anything above 1000 or nonintegers will generate API errors
Future<Response>deleteOrder(String orderId,{ CancelToken cancelToken, Map<String, String> headers,}) async {

String path = "/store/order/{orderId}".replaceAll("{" + "orderId" + "}", orderId.toString());
String path = "/store/order/{orderId}".replaceAll("{" + "orderId" + "}", orderId.toString());

Map<String, dynamic> queryParams = {};
Map<String, String> headerParams = Map.from(headers ?? {});
Expand Down Expand Up @@ -48,7 +48,7 @@ class StoreApi {
/// Returns a map of status codes to quantities
Future<Response<Map<String, int>>>getInventory({ CancelToken cancelToken, Map<String, String> headers,}) async {

String path = "/store/inventory";
String path = "/store/inventory";

Map<String, dynamic> queryParams = {};
Map<String, String> headerParams = Map.from(headers ?? {});
Expand Down Expand Up @@ -92,7 +92,7 @@ class StoreApi {
/// For valid response try integer IDs with value &lt;&#x3D; 5 or &gt; 10. Other values will generated exceptions
Future<Response<Order>>getOrderById(int orderId,{ CancelToken cancelToken, Map<String, String> headers,}) async {

String path = "/store/order/{orderId}".replaceAll("{" + "orderId" + "}", orderId.toString());
String path = "/store/order/{orderId}".replaceAll("{" + "orderId" + "}", orderId.toString());

Map<String, dynamic> queryParams = {};
Map<String, String> headerParams = Map.from(headers ?? {});
Expand Down Expand Up @@ -136,7 +136,7 @@ class StoreApi {
///
Future<Response<Order>>placeOrder(Order body,{ CancelToken cancelToken, Map<String, String> headers,}) async {

String path = "/store/order";
String path = "/store/order";

Map<String, dynamic> queryParams = {};
Map<String, String> headerParams = Map.from(headers ?? {});
Expand Down
16 changes: 8 additions & 8 deletions samples/client/petstore/dart-dio/lib/api/user_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class UserApi {
/// This can only be done by the logged in user.
Future<Response>createUser(User body,{ CancelToken cancelToken, Map<String, String> headers,}) async {

String path = "/user";
String path = "/user";

Map<String, dynamic> queryParams = {};
Map<String, String> headerParams = Map.from(headers ?? {});
Expand Down Expand Up @@ -51,7 +51,7 @@ class UserApi {
///
Future<Response>createUsersWithArrayInput(List<User> body,{ CancelToken cancelToken, Map<String, String> headers,}) async {

String path = "/user/createWithArray";
String path = "/user/createWithArray";

Map<String, dynamic> queryParams = {};
Map<String, String> headerParams = Map.from(headers ?? {});
Expand Down Expand Up @@ -85,7 +85,7 @@ class UserApi {
///
Future<Response>createUsersWithListInput(List<User> body,{ CancelToken cancelToken, Map<String, String> headers,}) async {

String path = "/user/createWithList";
String path = "/user/createWithList";

Map<String, dynamic> queryParams = {};
Map<String, String> headerParams = Map.from(headers ?? {});
Expand Down Expand Up @@ -119,7 +119,7 @@ class UserApi {
/// This can only be done by the logged in user.
Future<Response>deleteUser(String username,{ CancelToken cancelToken, Map<String, String> headers,}) async {

String path = "/user/{username}".replaceAll("{" + "username" + "}", username.toString());
String path = "/user/{username}".replaceAll("{" + "username" + "}", username.toString());

Map<String, dynamic> queryParams = {};
Map<String, String> headerParams = Map.from(headers ?? {});
Expand Down Expand Up @@ -149,7 +149,7 @@ class UserApi {
///
Future<Response<User>>getUserByName(String username,{ CancelToken cancelToken, Map<String, String> headers,}) async {

String path = "/user/{username}".replaceAll("{" + "username" + "}", username.toString());
String path = "/user/{username}".replaceAll("{" + "username" + "}", username.toString());

Map<String, dynamic> queryParams = {};
Map<String, String> headerParams = Map.from(headers ?? {});
Expand Down Expand Up @@ -193,7 +193,7 @@ class UserApi {
///
Future<Response<String>>loginUser(String username,String password,{ CancelToken cancelToken, Map<String, String> headers,}) async {

String path = "/user/login";
String path = "/user/login";

Map<String, dynamic> queryParams = {};
Map<String, String> headerParams = Map.from(headers ?? {});
Expand Down Expand Up @@ -239,7 +239,7 @@ class UserApi {
///
Future<Response>logoutUser({ CancelToken cancelToken, Map<String, String> headers,}) async {

String path = "/user/logout";
String path = "/user/logout";

Map<String, dynamic> queryParams = {};
Map<String, String> headerParams = Map.from(headers ?? {});
Expand Down Expand Up @@ -269,7 +269,7 @@ class UserApi {
/// This can only be done by the logged in user.
Future<Response>updateUser(String username,User body,{ CancelToken cancelToken, Map<String, String> headers,}) async {

String path = "/user/{username}".replaceAll("{" + "username" + "}", username.toString());
String path = "/user/{username}".replaceAll("{" + "username" + "}", username.toString());

Map<String, dynamic> queryParams = {};
Map<String, String> headerParams = Map.from(headers ?? {});
Expand Down
20 changes: 12 additions & 8 deletions samples/client/petstore/dart-dio/lib/api_util.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import 'dart:convert';

import 'package:built_value/serializer.dart';

/// Format the given parameter object into string.
String parameterToString(dynamic value) {
if (value == null) {
return '';
} else if (value is DateTime) {
return value.toUtc().toIso8601String();
} else {
return value.toString();
}
String parameterToString(Serializers serializers, dynamic value) {
if (value == null) {
return '';
} else if (value is String || value is num) {
return value.toString();
} else {
return json.encode(serializers.serialize(value));
}
}
1 change: 0 additions & 1 deletion samples/client/petstore/dart-dio/lib/model/order.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import 'DateTime';
import 'package:built_value/built_value.dart';
import 'package:built_value/serializer.dart';

Expand Down

0 comments on commit f48325a

Please sign in to comment.