forked from loafle/openapi-generator-original
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:
parent
eaa01e15f1
commit
526f1b48f8
@ -107,6 +107,12 @@ class {{classname}} {
|
|||||||
{{/complexType}}
|
{{/complexType}}
|
||||||
{{/isDate}}
|
{{/isDate}}
|
||||||
{{/isDateTime}}
|
{{/isDateTime}}
|
||||||
|
{{#required}}
|
||||||
|
{{^isNullable}}
|
||||||
|
if ({{name}} == null)
|
||||||
|
throw ArgumentError("$json has no {{name}} field which is required for {{classname}}");
|
||||||
|
{{/isNullable}}
|
||||||
|
{{/required}}
|
||||||
{{/vars}}
|
{{/vars}}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,24 +1,36 @@
|
|||||||
class {{classname}} {
|
class {{classname}} {
|
||||||
dynamic instance;
|
/// Can be {{#oneOf}}[{{{.}}}], {{/oneOf}}
|
||||||
|
dynamic _instance;
|
||||||
|
|
||||||
// default constructor
|
// 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
|
@override
|
||||||
String toString() {
|
String toString() {
|
||||||
// TODO
|
return _instance.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
{{classname}}.fromJson(Map<String, dynamic> json) {
|
{{classname}}.fromJson(Map<String, dynamic> json) {
|
||||||
// TODO
|
if (json == null) return;
|
||||||
// loop through models/primitive types defined in {{#oneOf}} {{{.}}} {{/oneOf}}
|
// TODO primitives, lists, maps
|
||||||
// and make sure the payload `json` deserializes to one and only one schema defined in oneOf
|
{{#oneOf}}
|
||||||
|
try {
|
||||||
|
_instance = {{{.}}}.fromJson(json);
|
||||||
|
} on ArgumentError {
|
||||||
|
}
|
||||||
|
{{/oneOf}}
|
||||||
}
|
}
|
||||||
|
|
||||||
Map<String, dynamic> toJson() {
|
Map<String, dynamic> toJson() {
|
||||||
// TOOD there should be a class member/property called "instance"
|
// TODO primitives, lists, maps
|
||||||
// which is dynamic type and it stores the actual instance of the schema defined in oneOf
|
return _instance.toJson();
|
||||||
// if oneOf is [Dog, Cat], then the instance can store either an instance of Dog or Cat
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,27 +1,41 @@
|
|||||||
part of openapi.api;
|
part of openapi.api;
|
||||||
|
|
||||||
class OneOfTest {
|
class OneOfTest {
|
||||||
dynamic instance;
|
/// Can be [Pet], [User],
|
||||||
|
dynamic _instance;
|
||||||
|
|
||||||
// default constructor
|
// 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
|
@override
|
||||||
String toString() {
|
String toString() {
|
||||||
// TODO
|
return _instance.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
OneOfTest.fromJson(Map<String, dynamic> json) {
|
OneOfTest.fromJson(Map<String, dynamic> json) {
|
||||||
// TODO
|
if (json == null) return;
|
||||||
// loop through models/primitive types defined in Pet User
|
// TODO primitives, lists, maps
|
||||||
// and make sure the payload `json` deserializes to one and only one schema defined in oneOf
|
try {
|
||||||
|
_instance = Pet.fromJson(json);
|
||||||
|
} on ArgumentError {
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
_instance = User.fromJson(json);
|
||||||
|
} on ArgumentError {
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Map<String, dynamic> toJson() {
|
Map<String, dynamic> toJson() {
|
||||||
// TOOD there should be a class member/property called "instance"
|
// TODO primitives, lists, maps
|
||||||
// which is dynamic type and it stores the actual instance of the schema defined in oneOf
|
return _instance.toJson();
|
||||||
// if oneOf is [Dog, Cat], then the instance can store either an instance of Dog or Cat
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -35,9 +35,13 @@ class Pet {
|
|||||||
null :
|
null :
|
||||||
Category.fromJson(json['category']);
|
Category.fromJson(json['category']);
|
||||||
name = json['name'];
|
name = json['name'];
|
||||||
|
if (name == null)
|
||||||
|
throw ArgumentError("$json has no name field which is required for Pet");
|
||||||
photoUrls = (json['photoUrls'] == null) ?
|
photoUrls = (json['photoUrls'] == null) ?
|
||||||
null :
|
null :
|
||||||
(json['photoUrls'] as List).cast<String>();
|
(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) ?
|
tags = (json['tags'] == null) ?
|
||||||
null :
|
null :
|
||||||
Tag.listFromJson(json['tags']);
|
Tag.listFromJson(json['tags']);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user