mirror of
https://github.com/OpenAPITools/openapi-generator.git
synced 2025-05-12 12:40:53 +00:00
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}}
|
||||
{{/isDate}}
|
||||
{{/isDateTime}}
|
||||
{{#required}}
|
||||
{{^isNullable}}
|
||||
if ({{name}} == null)
|
||||
throw ArgumentError("$json has no {{name}} field which is required for {{classname}}");
|
||||
{{/isNullable}}
|
||||
{{/required}}
|
||||
{{/vars}}
|
||||
}
|
||||
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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']);
|
||||
|
Loading…
x
Reference in New Issue
Block a user