diff --git a/modules/openapi-generator/src/main/resources/dart2/class.mustache b/modules/openapi-generator/src/main/resources/dart2/class.mustache index a7ddbb4ae17..4e6bcfcfd08 100644 --- a/modules/openapi-generator/src/main/resources/dart2/class.mustache +++ b/modules/openapi-generator/src/main/resources/dart2/class.mustache @@ -107,6 +107,12 @@ class {{classname}} { {{/complexType}} {{/isDate}} {{/isDateTime}} + {{#required}} + {{^isNullable}} + if ({{name}} == null) + throw ArgumentError("$json has no {{name}} field which is required for {{classname}}"); + {{/isNullable}} + {{/required}} {{/vars}} } diff --git a/modules/openapi-generator/src/main/resources/dart2/class_oneof.mustache b/modules/openapi-generator/src/main/resources/dart2/class_oneof.mustache index cc944f11263..25fd98563fa 100644 --- a/modules/openapi-generator/src/main/resources/dart2/class_oneof.mustache +++ b/modules/openapi-generator/src/main/resources/dart2/class_oneof.mustache @@ -1,24 +1,36 @@ class {{classname}} { - dynamic instance; + /// Can be {{#oneOf}}[{{{.}}}], {{/oneOf}} + dynamic _instance; // default constructor - {{classname}}({ - }); + {{classname}}(); + + set instance(dynamic instance) { + if (!(instance == null{{#oneOf}} || instance is {{{.}}}{{/oneOf}})) + throw ArgumentError("${instance.runtimeType} is not a valid type for {{classname}}"); + _instance = instance; + } + + dynamic get instance => _instance; @override String toString() { - // TODO + return _instance.toString(); } {{classname}}.fromJson(Map json) { - // TODO - // loop through models/primitive types defined in {{#oneOf}} {{{.}}} {{/oneOf}} - // and make sure the payload `json` deserializes to one and only one schema defined in oneOf + if (json == null) return; + // TODO primitives, lists, maps + {{#oneOf}} + try { + _instance = {{{.}}}.fromJson(json); + } on ArgumentError { + } + {{/oneOf}} } Map toJson() { - // TOOD there should be a class member/property called "instance" - // which is dynamic type and it stores the actual instance of the schema defined in oneOf - // if oneOf is [Dog, Cat], then the instance can store either an instance of Dog or Cat + // TODO primitives, lists, maps + return _instance.toJson(); } } diff --git a/samples/client/petstore/dart2/petstore_client_lib/lib/model/one_of_test.dart b/samples/client/petstore/dart2/petstore_client_lib/lib/model/one_of_test.dart index ff8c884f4fe..3a30c1f52d2 100644 --- a/samples/client/petstore/dart2/petstore_client_lib/lib/model/one_of_test.dart +++ b/samples/client/petstore/dart2/petstore_client_lib/lib/model/one_of_test.dart @@ -1,27 +1,41 @@ part of openapi.api; class OneOfTest { - dynamic instance; + /// Can be [Pet], [User], + dynamic _instance; // default constructor - OneOfTest({ - }); + OneOfTest(); + + set instance(dynamic instance) { + if (!(instance == null || instance is Pet || instance is User)) + throw ArgumentError("${instance.runtimeType} is not a valid type for OneOfTest"); + _instance = instance; + } + + dynamic get instance => _instance; @override String toString() { - // TODO + return _instance.toString(); } OneOfTest.fromJson(Map json) { - // TODO - // loop through models/primitive types defined in Pet User - // and make sure the payload `json` deserializes to one and only one schema defined in oneOf + if (json == null) return; + // TODO primitives, lists, maps + try { + _instance = Pet.fromJson(json); + } on ArgumentError { + } + try { + _instance = User.fromJson(json); + } on ArgumentError { + } } Map toJson() { - // TOOD there should be a class member/property called "instance" - // which is dynamic type and it stores the actual instance of the schema defined in oneOf - // if oneOf is [Dog, Cat], then the instance can store either an instance of Dog or Cat + // TODO primitives, lists, maps + return _instance.toJson(); } } diff --git a/samples/client/petstore/dart2/petstore_client_lib/lib/model/pet.dart b/samples/client/petstore/dart2/petstore_client_lib/lib/model/pet.dart index b6ba8476356..5e94a8e387b 100644 --- a/samples/client/petstore/dart2/petstore_client_lib/lib/model/pet.dart +++ b/samples/client/petstore/dart2/petstore_client_lib/lib/model/pet.dart @@ -35,9 +35,13 @@ class Pet { null : Category.fromJson(json['category']); name = json['name']; + if (name == null) + throw ArgumentError("$json has no name field which is required for Pet"); photoUrls = (json['photoUrls'] == null) ? null : (json['photoUrls'] as List).cast(); + if (photoUrls == null) + throw ArgumentError("$json has no photoUrls field which is required for Pet"); tags = (json['tags'] == null) ? null : Tag.listFromJson(json['tags']);