Dart oneof for models (#7493)

* Type check for setting, some docs, toString and toJson

* Check for required fields in fromJson

* OneOf deserialization

* Individual check for required fields to avoid `if (false)`

* Add nullable check for required fields
This commit is contained in:
sbu 2020-10-12 12:18:06 +02:00 committed by GitHub
parent eaa01e15f1
commit 526f1b48f8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 56 additions and 20 deletions

View File

@ -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}}
}

View File

@ -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<String, dynamic> 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<String, dynamic> 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();
}
}

View File

@ -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<String, dynamic> 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<String, dynamic> 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();
}
}

View File

@ -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<String>();
if (photoUrls == null)
throw ArgumentError("$json has no photoUrls field which is required for Pet");
tags = (json['tags'] == null) ?
null :
Tag.listFromJson(json['tags']);