dart2: Do not add non-nullable fields to model json (#2777)

* do not add non-nullable fields to model json

This fix would avoid adding and sending fields that are not isNullable in the request.

* updated sample for issue #2535
This commit is contained in:
Alexis Yushin
2019-05-02 10:31:38 +02:00
committed by William Cheng
parent 006cfa35e6
commit 4b414d81d4
22 changed files with 210 additions and 126 deletions

View File

@@ -60,21 +60,24 @@ class {{classname}} {
}
Map<String, dynamic> toJson() {
return {
Map <String, dynamic> json = {};
{{#vars}}
{{#isDateTime}}
'{{baseName}}': {{name}} == null ? '' : {{name}}.toUtc().toIso8601String(){{^-last}},{{/-last}}
{{^isNullable}}
if ({{name}} != null)
{{/isNullable}}
{{#isDateTime}}
json['{{baseName}}'] = {{name}} == null ? null : {{name}}.toUtc().toIso8601String();
{{/isDateTime}}
{{#isDate}}
'{{baseName}}': {{name}} == null ? '' : {{name}}.toUtc().toIso8601String(){{^-last}},{{/-last}}
json['{{baseName}}'] = {{name}} == null ? null : {{name}}.toUtc().toIso8601String();
{{/isDate}}
{{^isDateTime}}
{{^isDate}}
'{{baseName}}': {{name}}{{^-last}},{{/-last}}
json['{{baseName}}'] = {{name}};
{{/isDate}}
{{/isDateTime}}
{{/vars}}
};
return json;
}
static List<{{classname}}> listFromJson(List<dynamic> json) {

View File

@@ -18,7 +18,7 @@ class ApiClient {
final _regList = RegExp(r'^List<(.*)>$');
final _regMap = RegExp(r'^Map<String,(.*)>$');
ApiClient({this.basePath: "http://petstore.swagger.io/v2"}) {
ApiClient({this.basePath = "http://petstore.swagger.io/v2"}) {
// Setup authentications (key: authentication name, value: authentication).
_authentications['api_key'] = ApiKeyAuth("header", "api_key");
_authentications['petstore_auth'] = OAuth();

View File

@@ -34,11 +34,14 @@ class ApiResponse {
}
Map<String, dynamic> toJson() {
return {
'code': code,
'type': type,
'message': message
};
Map <String, dynamic> json = {};
if (code != null)
json['code'] = code;
if (type != null)
json['type'] = type;
if (message != null)
json['message'] = message;
return json;
}
static List<ApiResponse> listFromJson(List<dynamic> json) {

View File

@@ -27,10 +27,12 @@ class Category {
}
Map<String, dynamic> toJson() {
return {
'id': id,
'name': name
};
Map <String, dynamic> json = {};
if (id != null)
json['id'] = id;
if (name != null)
json['name'] = name;
return json;
}
static List<Category> listFromJson(List<dynamic> json) {

View File

@@ -56,14 +56,20 @@ class Order {
}
Map<String, dynamic> toJson() {
return {
'id': id,
'petId': petId,
'quantity': quantity,
'shipDate': shipDate == null ? '' : shipDate.toUtc().toIso8601String(),
'status': status,
'complete': complete
};
Map <String, dynamic> json = {};
if (id != null)
json['id'] = id;
if (petId != null)
json['petId'] = petId;
if (quantity != null)
json['quantity'] = quantity;
if (shipDate != null)
json['shipDate'] = shipDate == null ? null : shipDate.toUtc().toIso8601String();
if (status != null)
json['status'] = status;
if (complete != null)
json['complete'] = complete;
return json;
}
static List<Order> listFromJson(List<dynamic> json) {

View File

@@ -56,14 +56,20 @@ class Pet {
}
Map<String, dynamic> toJson() {
return {
'id': id,
'category': category,
'name': name,
'photoUrls': photoUrls,
'tags': tags,
'status': status
};
Map <String, dynamic> json = {};
if (id != null)
json['id'] = id;
if (category != null)
json['category'] = category;
if (name != null)
json['name'] = name;
if (photoUrls != null)
json['photoUrls'] = photoUrls;
if (tags != null)
json['tags'] = tags;
if (status != null)
json['status'] = status;
return json;
}
static List<Pet> listFromJson(List<dynamic> json) {

View File

@@ -27,10 +27,12 @@ class Tag {
}
Map<String, dynamic> toJson() {
return {
'id': id,
'name': name
};
Map <String, dynamic> json = {};
if (id != null)
json['id'] = id;
if (name != null)
json['name'] = name;
return json;
}
static List<Tag> listFromJson(List<dynamic> json) {

View File

@@ -69,16 +69,24 @@ class User {
}
Map<String, dynamic> toJson() {
return {
'id': id,
'username': username,
'firstName': firstName,
'lastName': lastName,
'email': email,
'password': password,
'phone': phone,
'userStatus': userStatus
};
Map <String, dynamic> json = {};
if (id != null)
json['id'] = id;
if (username != null)
json['username'] = username;
if (firstName != null)
json['firstName'] = firstName;
if (lastName != null)
json['lastName'] = lastName;
if (email != null)
json['email'] = email;
if (password != null)
json['password'] = password;
if (phone != null)
json['phone'] = phone;
if (userStatus != null)
json['userStatus'] = userStatus;
return json;
}
static List<User> listFromJson(List<dynamic> json) {

View File

@@ -18,7 +18,7 @@ class ApiClient {
final _regList = RegExp(r'^List<(.*)>$');
final _regMap = RegExp(r'^Map<String,(.*)>$');
ApiClient({this.basePath: "http://petstore.swagger.io/v2"}) {
ApiClient({this.basePath = "http://petstore.swagger.io/v2"}) {
// Setup authentications (key: authentication name, value: authentication).
_authentications['api_key'] = ApiKeyAuth("header", "api_key");
_authentications['petstore_auth'] = OAuth();

View File

@@ -34,11 +34,14 @@ class ApiResponse {
}
Map<String, dynamic> toJson() {
return {
'code': code,
'type': type,
'message': message
};
Map <String, dynamic> json = {};
if (code != null)
json['code'] = code;
if (type != null)
json['type'] = type;
if (message != null)
json['message'] = message;
return json;
}
static List<ApiResponse> listFromJson(List<dynamic> json) {

View File

@@ -27,10 +27,12 @@ class Category {
}
Map<String, dynamic> toJson() {
return {
'id': id,
'name': name
};
Map <String, dynamic> json = {};
if (id != null)
json['id'] = id;
if (name != null)
json['name'] = name;
return json;
}
static List<Category> listFromJson(List<dynamic> json) {

View File

@@ -56,14 +56,20 @@ class Order {
}
Map<String, dynamic> toJson() {
return {
'id': id,
'petId': petId,
'quantity': quantity,
'shipDate': shipDate == null ? '' : shipDate.toUtc().toIso8601String(),
'status': status,
'complete': complete
};
Map <String, dynamic> json = {};
if (id != null)
json['id'] = id;
if (petId != null)
json['petId'] = petId;
if (quantity != null)
json['quantity'] = quantity;
if (shipDate != null)
json['shipDate'] = shipDate == null ? null : shipDate.toUtc().toIso8601String();
if (status != null)
json['status'] = status;
if (complete != null)
json['complete'] = complete;
return json;
}
static List<Order> listFromJson(List<dynamic> json) {

View File

@@ -56,14 +56,20 @@ class Pet {
}
Map<String, dynamic> toJson() {
return {
'id': id,
'category': category,
'name': name,
'photoUrls': photoUrls,
'tags': tags,
'status': status
};
Map <String, dynamic> json = {};
if (id != null)
json['id'] = id;
if (category != null)
json['category'] = category;
if (name != null)
json['name'] = name;
if (photoUrls != null)
json['photoUrls'] = photoUrls;
if (tags != null)
json['tags'] = tags;
if (status != null)
json['status'] = status;
return json;
}
static List<Pet> listFromJson(List<dynamic> json) {

View File

@@ -27,10 +27,12 @@ class Tag {
}
Map<String, dynamic> toJson() {
return {
'id': id,
'name': name
};
Map <String, dynamic> json = {};
if (id != null)
json['id'] = id;
if (name != null)
json['name'] = name;
return json;
}
static List<Tag> listFromJson(List<dynamic> json) {

View File

@@ -69,16 +69,24 @@ class User {
}
Map<String, dynamic> toJson() {
return {
'id': id,
'username': username,
'firstName': firstName,
'lastName': lastName,
'email': email,
'password': password,
'phone': phone,
'userStatus': userStatus
};
Map <String, dynamic> json = {};
if (id != null)
json['id'] = id;
if (username != null)
json['username'] = username;
if (firstName != null)
json['firstName'] = firstName;
if (lastName != null)
json['lastName'] = lastName;
if (email != null)
json['email'] = email;
if (password != null)
json['password'] = password;
if (phone != null)
json['phone'] = phone;
if (userStatus != null)
json['userStatus'] = userStatus;
return json;
}
static List<User> listFromJson(List<dynamic> json) {

View File

@@ -18,7 +18,7 @@ class ApiClient {
final _regList = RegExp(r'^List<(.*)>$');
final _regMap = RegExp(r'^Map<String,(.*)>$');
ApiClient({this.basePath: "http://petstore.swagger.io/v2"}) {
ApiClient({this.basePath = "http://petstore.swagger.io/v2"}) {
// Setup authentications (key: authentication name, value: authentication).
_authentications['api_key'] = ApiKeyAuth("header", "api_key");
_authentications['petstore_auth'] = OAuth();

View File

@@ -34,11 +34,14 @@ class ApiResponse {
}
Map<String, dynamic> toJson() {
return {
'code': code,
'type': type,
'message': message
};
Map <String, dynamic> json = {};
if (code != null)
json['code'] = code;
if (type != null)
json['type'] = type;
if (message != null)
json['message'] = message;
return json;
}
static List<ApiResponse> listFromJson(List<dynamic> json) {

View File

@@ -27,10 +27,12 @@ class Category {
}
Map<String, dynamic> toJson() {
return {
'id': id,
'name': name
};
Map <String, dynamic> json = {};
if (id != null)
json['id'] = id;
if (name != null)
json['name'] = name;
return json;
}
static List<Category> listFromJson(List<dynamic> json) {

View File

@@ -56,14 +56,20 @@ class Order {
}
Map<String, dynamic> toJson() {
return {
'id': id,
'petId': petId,
'quantity': quantity,
'shipDate': shipDate == null ? '' : shipDate.toUtc().toIso8601String(),
'status': status,
'complete': complete
};
Map <String, dynamic> json = {};
if (id != null)
json['id'] = id;
if (petId != null)
json['petId'] = petId;
if (quantity != null)
json['quantity'] = quantity;
if (shipDate != null)
json['shipDate'] = shipDate == null ? null : shipDate.toUtc().toIso8601String();
if (status != null)
json['status'] = status;
if (complete != null)
json['complete'] = complete;
return json;
}
static List<Order> listFromJson(List<dynamic> json) {

View File

@@ -56,14 +56,20 @@ class Pet {
}
Map<String, dynamic> toJson() {
return {
'id': id,
'category': category,
'name': name,
'photoUrls': photoUrls,
'tags': tags,
'status': status
};
Map <String, dynamic> json = {};
if (id != null)
json['id'] = id;
if (category != null)
json['category'] = category;
if (name != null)
json['name'] = name;
if (photoUrls != null)
json['photoUrls'] = photoUrls;
if (tags != null)
json['tags'] = tags;
if (status != null)
json['status'] = status;
return json;
}
static List<Pet> listFromJson(List<dynamic> json) {

View File

@@ -27,10 +27,12 @@ class Tag {
}
Map<String, dynamic> toJson() {
return {
'id': id,
'name': name
};
Map <String, dynamic> json = {};
if (id != null)
json['id'] = id;
if (name != null)
json['name'] = name;
return json;
}
static List<Tag> listFromJson(List<dynamic> json) {

View File

@@ -69,16 +69,24 @@ class User {
}
Map<String, dynamic> toJson() {
return {
'id': id,
'username': username,
'firstName': firstName,
'lastName': lastName,
'email': email,
'password': password,
'phone': phone,
'userStatus': userStatus
};
Map <String, dynamic> json = {};
if (id != null)
json['id'] = id;
if (username != null)
json['username'] = username;
if (firstName != null)
json['firstName'] = firstName;
if (lastName != null)
json['lastName'] = lastName;
if (email != null)
json['email'] = email;
if (password != null)
json['password'] = password;
if (phone != null)
json['phone'] = phone;
if (userStatus != null)
json['userStatus'] = userStatus;
return json;
}
static List<User> listFromJson(List<dynamic> json) {