mirror of
https://github.com/OpenAPITools/openapi-generator.git
synced 2025-07-04 14:40:53 +00:00
Encode a Set
in toJson()
properly (#15435)
* Convert `Set` to `List` in `toJson()` for properly encoding a set. * Generate samples code. * Convert to List before encoding in `toJson()`. * Add `!` for nullable properties.
This commit is contained in:
parent
e152799569
commit
c84b949df1
@ -176,7 +176,7 @@ class {{{classname}}} {
|
|||||||
final responseBody = await _decodeBodyBytes(response);
|
final responseBody = await _decodeBodyBytes(response);
|
||||||
return (await apiClient.deserializeAsync(responseBody, '{{{returnType}}}') as List)
|
return (await apiClient.deserializeAsync(responseBody, '{{{returnType}}}') as List)
|
||||||
.cast<{{{returnBaseType}}}>()
|
.cast<{{{returnBaseType}}}>()
|
||||||
.{{#uniqueItems}}toSet(){{/uniqueItems}}{{^uniqueItems}}toList(){{/uniqueItems}};
|
.{{#uniqueItems}}toSet(){{/uniqueItems}}{{^uniqueItems}}toList(growable: false){{/uniqueItems}};
|
||||||
{{/isArray}}
|
{{/isArray}}
|
||||||
{{^isArray}}
|
{{^isArray}}
|
||||||
{{#isMap}}
|
{{#isMap}}
|
||||||
|
@ -86,7 +86,7 @@ class {{{classname}}} {
|
|||||||
{{/isDate}}
|
{{/isDate}}
|
||||||
{{^isDateTime}}
|
{{^isDateTime}}
|
||||||
{{^isDate}}
|
{{^isDate}}
|
||||||
json[r'{{{baseName}}}'] = this.{{{name}}};
|
json[r'{{{baseName}}}'] = this.{{{name}}}{{#isArray}}{{#uniqueItems}}{{#isNullable}}!{{/isNullable}}.toList(growable: false){{/uniqueItems}}{{/isArray}};
|
||||||
{{/isDate}}
|
{{/isDate}}
|
||||||
{{/isDateTime}}
|
{{/isDateTime}}
|
||||||
{{#isNullable}}
|
{{#isNullable}}
|
||||||
@ -200,8 +200,8 @@ class {{{classname}}} {
|
|||||||
{{{name}}}: {{{items.datatypeWithEnum}}}.listFromJson(json[r'{{{baseName}}}']){{#uniqueItems}}.toSet(){{/uniqueItems}},
|
{{{name}}}: {{{items.datatypeWithEnum}}}.listFromJson(json[r'{{{baseName}}}']){{#uniqueItems}}.toSet(){{/uniqueItems}},
|
||||||
{{/isEnum}}
|
{{/isEnum}}
|
||||||
{{^isEnum}}
|
{{^isEnum}}
|
||||||
{{{name}}}: json[r'{{{baseName}}}'] is {{#uniqueItems}}Set{{/uniqueItems}}{{^uniqueItems}}List{{/uniqueItems}}
|
{{{name}}}: json[r'{{{baseName}}}'] is Iterable
|
||||||
? (json[r'{{{baseName}}}'] as {{#uniqueItems}}Set{{/uniqueItems}}{{^uniqueItems}}List{{/uniqueItems}}).cast<{{{items.datatype}}}>()
|
? (json[r'{{{baseName}}}'] as Iterable).cast<{{{items.datatype}}}>().{{#uniqueItems}}toSet(){{/uniqueItems}}{{^uniqueItems}}toList(growable: false){{/uniqueItems}}
|
||||||
: {{#defaultValue}}{{{.}}}{{/defaultValue}}{{^defaultValue}}null{{/defaultValue}},
|
: {{#defaultValue}}{{{.}}}{{/defaultValue}}{{^defaultValue}}null{{/defaultValue}},
|
||||||
{{/isEnum}}
|
{{/isEnum}}
|
||||||
{{/isArray}}
|
{{/isArray}}
|
||||||
|
@ -190,7 +190,7 @@ class PetApi {
|
|||||||
final responseBody = await _decodeBodyBytes(response);
|
final responseBody = await _decodeBodyBytes(response);
|
||||||
return (await apiClient.deserializeAsync(responseBody, 'List<Pet>') as List)
|
return (await apiClient.deserializeAsync(responseBody, 'List<Pet>') as List)
|
||||||
.cast<Pet>()
|
.cast<Pet>()
|
||||||
.toList();
|
.toList(growable: false);
|
||||||
|
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
@ -253,7 +253,7 @@ class PetApi {
|
|||||||
final responseBody = await _decodeBodyBytes(response);
|
final responseBody = await _decodeBodyBytes(response);
|
||||||
return (await apiClient.deserializeAsync(responseBody, 'List<Pet>') as List)
|
return (await apiClient.deserializeAsync(responseBody, 'List<Pet>') as List)
|
||||||
.cast<Pet>()
|
.cast<Pet>()
|
||||||
.toList();
|
.toList(growable: false);
|
||||||
|
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
|
@ -113,8 +113,8 @@ class Pet {
|
|||||||
id: mapValueOfType<int>(json, r'id'),
|
id: mapValueOfType<int>(json, r'id'),
|
||||||
category: Category.fromJson(json[r'category']),
|
category: Category.fromJson(json[r'category']),
|
||||||
name: mapValueOfType<String>(json, r'name')!,
|
name: mapValueOfType<String>(json, r'name')!,
|
||||||
photoUrls: json[r'photoUrls'] is List
|
photoUrls: json[r'photoUrls'] is Iterable
|
||||||
? (json[r'photoUrls'] as List).cast<String>()
|
? (json[r'photoUrls'] as Iterable).cast<String>().toList(growable: false)
|
||||||
: const [],
|
: const [],
|
||||||
tags: Tag.listFromJson(json[r'tags']),
|
tags: Tag.listFromJson(json[r'tags']),
|
||||||
status: PetStatusEnum.fromJson(json[r'status']),
|
status: PetStatusEnum.fromJson(json[r'status']),
|
||||||
|
@ -182,7 +182,7 @@ class PetApi {
|
|||||||
final responseBody = await _decodeBodyBytes(response);
|
final responseBody = await _decodeBodyBytes(response);
|
||||||
return (await apiClient.deserializeAsync(responseBody, 'List<Pet>') as List)
|
return (await apiClient.deserializeAsync(responseBody, 'List<Pet>') as List)
|
||||||
.cast<Pet>()
|
.cast<Pet>()
|
||||||
.toList();
|
.toList(growable: false);
|
||||||
|
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
|
@ -55,8 +55,8 @@ class ArrayOfNumberOnly {
|
|||||||
}());
|
}());
|
||||||
|
|
||||||
return ArrayOfNumberOnly(
|
return ArrayOfNumberOnly(
|
||||||
arrayNumber: json[r'ArrayNumber'] is List
|
arrayNumber: json[r'ArrayNumber'] is Iterable
|
||||||
? (json[r'ArrayNumber'] as List).cast<num>()
|
? (json[r'ArrayNumber'] as Iterable).cast<num>().toList(growable: false)
|
||||||
: const [],
|
: const [],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -67,8 +67,8 @@ class ArrayTest {
|
|||||||
}());
|
}());
|
||||||
|
|
||||||
return ArrayTest(
|
return ArrayTest(
|
||||||
arrayOfString: json[r'array_of_string'] is List
|
arrayOfString: json[r'array_of_string'] is Iterable
|
||||||
? (json[r'array_of_string'] as List).cast<String>()
|
? (json[r'array_of_string'] as Iterable).cast<String>().toList(growable: false)
|
||||||
: const [],
|
: const [],
|
||||||
arrayArrayOfInteger: json[r'array_array_of_integer'] is List
|
arrayArrayOfInteger: json[r'array_array_of_integer'] is List
|
||||||
? (json[r'array_array_of_integer'] as List).map((e) =>
|
? (json[r'array_array_of_integer'] as List).map((e) =>
|
||||||
|
@ -108,8 +108,8 @@ class ObjectWithDeprecatedFields {
|
|||||||
? null
|
? null
|
||||||
: num.parse(json[r'id'].toString()),
|
: num.parse(json[r'id'].toString()),
|
||||||
deprecatedRef: DeprecatedObject.fromJson(json[r'deprecatedRef']),
|
deprecatedRef: DeprecatedObject.fromJson(json[r'deprecatedRef']),
|
||||||
bars: json[r'bars'] is List
|
bars: json[r'bars'] is Iterable
|
||||||
? (json[r'bars'] as List).cast<String>()
|
? (json[r'bars'] as Iterable).cast<String>().toList(growable: false)
|
||||||
: const [],
|
: const [],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -81,7 +81,7 @@ class Pet {
|
|||||||
json[r'category'] = null;
|
json[r'category'] = null;
|
||||||
}
|
}
|
||||||
json[r'name'] = this.name;
|
json[r'name'] = this.name;
|
||||||
json[r'photoUrls'] = this.photoUrls;
|
json[r'photoUrls'] = this.photoUrls.toList(growable: false);
|
||||||
json[r'tags'] = this.tags;
|
json[r'tags'] = this.tags;
|
||||||
if (this.status != null) {
|
if (this.status != null) {
|
||||||
json[r'status'] = this.status;
|
json[r'status'] = this.status;
|
||||||
@ -113,8 +113,8 @@ class Pet {
|
|||||||
id: mapValueOfType<int>(json, r'id'),
|
id: mapValueOfType<int>(json, r'id'),
|
||||||
category: Category.fromJson(json[r'category']),
|
category: Category.fromJson(json[r'category']),
|
||||||
name: mapValueOfType<String>(json, r'name')!,
|
name: mapValueOfType<String>(json, r'name')!,
|
||||||
photoUrls: json[r'photoUrls'] is Set
|
photoUrls: json[r'photoUrls'] is Iterable
|
||||||
? (json[r'photoUrls'] as Set).cast<String>()
|
? (json[r'photoUrls'] as Iterable).cast<String>().toSet()
|
||||||
: const {},
|
: const {},
|
||||||
tags: Tag.listFromJson(json[r'tags']),
|
tags: Tag.listFromJson(json[r'tags']),
|
||||||
status: PetStatusEnum.fromJson(json[r'status']),
|
status: PetStatusEnum.fromJson(json[r'status']),
|
||||||
|
Loading…
x
Reference in New Issue
Block a user