-
Notifications
You must be signed in to change notification settings - Fork 56
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: #290 supports oneOf polymorphic types with dart_mappable #292
base: main
Are you sure you want to change the base?
Changes from 1 commit
98b3131
db65677
afc1497
ff63766
ea3c86f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,25 @@ | ||
part of 'universal_data_class.dart'; | ||
|
||
/// Universal template for containing information about component | ||
@immutable | ||
final class UniversalComponentClass extends UniversalDataClass { | ||
/// Constructor for [UniversalComponentClass] | ||
const UniversalComponentClass({ | ||
UniversalComponentClass({ | ||
required super.name, | ||
required this.imports, | ||
required this.parameters, | ||
this.allOf, | ||
this.typeDef = false, | ||
this.discriminator, | ||
this.discriminatorValue, | ||
super.description, | ||
}); | ||
|
||
/// List of additional references to components | ||
final Set<String> imports; | ||
|
||
/// The import of this class | ||
String get import => name.toPascal; | ||
|
||
/// List of class fields | ||
final List<UniversalType> parameters; | ||
|
||
|
@@ -35,6 +38,13 @@ final class UniversalComponentClass extends UniversalDataClass { | |
Map<String, List<UniversalType>> refProperties, | ||
})? discriminator; | ||
|
||
/// When using a discriminated oneOf, where this class is one of the discriminated values, this field contains the information about the parent | ||
({ | ||
// The name of the property that is used to discriminate the oneOf variants | ||
String propertyValue, | ||
String parentClass, | ||
})? discriminatorValue; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This |
||
|
||
/// Whether or not this schema is a basic type | ||
/// "Date": { | ||
/// "type": "string", | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -198,6 +198,19 @@ void main() { | |
); | ||
}); | ||
|
||
test('discriminated_one_of.3.0_mappable', () async { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be nice to combine this test with the I've opened #291 to suggest some changes to the test structure to allow this. |
||
await e2eTest( | ||
'basic/discriminated_one_of.3.0_mappable', | ||
(outputDirectory, schemaPath) => SWPConfig( | ||
outputDirectory: outputDirectory, | ||
schemaPath: schemaPath, | ||
jsonSerializer: JsonSerializer.dartMappable, | ||
putClientsInFolder: true, | ||
), | ||
schemaFileName: 'discriminated_one_of.3.0.json', | ||
); | ||
}); | ||
|
||
test('empty_class.2.0', () async { | ||
await e2eTest( | ||
'basic/empty_class.2.0', | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
{ | ||
"openapi": "3.1.0", | ||
"info": { | ||
"title": "Family API", | ||
"version": "1.0.0" | ||
}, | ||
"paths": {}, | ||
"components": { | ||
"schemas": { | ||
"Family": { | ||
"type": "object", | ||
"properties": { | ||
"members": { | ||
"type": "array", | ||
"items": { | ||
"oneOf": [ | ||
{ | ||
"$ref": "#/components/schemas/Cat" | ||
}, | ||
{ | ||
"$ref": "#/components/schemas/Dog" | ||
}, | ||
{ | ||
"$ref": "#/components/schemas/Human" | ||
} | ||
], | ||
"discriminator": { | ||
"propertyName": "type", | ||
"mapping": { | ||
"Cat": "#/components/schemas/Cat", | ||
"Dog": "#/components/schemas/Dog", | ||
"Human": "#/components/schemas/Human" | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
"Cat": { | ||
"type": "object", | ||
"properties": { | ||
"type": { | ||
"type": "string", | ||
"enum": ["Cat"] | ||
}, | ||
"mewCount": { | ||
"type": "integer", | ||
"description": "Number of times the cat meows." | ||
} | ||
}, | ||
"required": ["type", "mewCount"] | ||
}, | ||
"Dog": { | ||
"type": "object", | ||
"properties": { | ||
"type": { | ||
"type": "string", | ||
"enum": ["Dog"] | ||
}, | ||
"barkSound": { | ||
"type": "string", | ||
"description": "The sound of the dog's bark." | ||
} | ||
}, | ||
"required": ["type", "barkSound"] | ||
}, | ||
"Human": { | ||
"type": "object", | ||
"properties": { | ||
"type": { | ||
"type": "string", | ||
"enum": ["Human"] | ||
}, | ||
"job": { | ||
"type": "string", | ||
"description": "The job of the human." | ||
} | ||
}, | ||
"required": ["type", "job"] | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// coverage:ignore-file | ||
// GENERATED CODE - DO NOT MODIFY BY HAND | ||
// ignore_for_file: type=lint, unused_import | ||
|
||
// Data classes | ||
export 'models/family.dart'; | ||
export 'models/cat.dart'; | ||
export 'models/dog.dart'; | ||
export 'models/human.dart'; | ||
export 'models/family_members_union.dart'; | ||
export 'models/cat_type.dart'; | ||
export 'models/dog_type.dart'; | ||
export 'models/human_type.dart'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// coverage:ignore-file | ||
// GENERATED CODE - DO NOT MODIFY BY HAND | ||
// ignore_for_file: type=lint, unused_import | ||
|
||
import 'package:dart_mappable/dart_mappable.dart'; | ||
|
||
import 'cat_type.dart'; | ||
import 'family_members_union.dart'; | ||
|
||
part 'cat.mapper.dart'; | ||
|
||
@MappableClass(discriminatorValue: 'Cat') | ||
class Cat extends FamilyMembersUnion with CatMappable { | ||
const Cat({ | ||
required this.type, | ||
required this.mewCount, | ||
}); | ||
|
||
final CatType type; | ||
final int mewCount; | ||
|
||
static Cat fromJson(Map<String, dynamic> json) => | ||
CatMapper.ensureInitialized().decodeMap<Cat>(json); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// coverage:ignore-file | ||
// GENERATED CODE - DO NOT MODIFY BY HAND | ||
// ignore_for_file: type=lint, unused_import | ||
|
||
import 'package:dart_mappable/dart_mappable.dart'; | ||
|
||
part 'cat_type.mapper.dart'; | ||
|
||
@MappableEnum() | ||
enum CatType { | ||
@MappableValue('Cat') | ||
cat, | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// coverage:ignore-file | ||
// GENERATED CODE - DO NOT MODIFY BY HAND | ||
// ignore_for_file: type=lint, unused_import | ||
|
||
import 'package:dart_mappable/dart_mappable.dart'; | ||
|
||
import 'dog_type.dart'; | ||
import 'family_members_union.dart'; | ||
|
||
part 'dog.mapper.dart'; | ||
|
||
@MappableClass(discriminatorValue: 'Dog') | ||
class Dog extends FamilyMembersUnion with DogMappable { | ||
const Dog({ | ||
required this.type, | ||
required this.barkSound, | ||
}); | ||
|
||
final DogType type; | ||
final String barkSound; | ||
|
||
static Dog fromJson(Map<String, dynamic> json) => | ||
DogMapper.ensureInitialized().decodeMap<Dog>(json); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// coverage:ignore-file | ||
// GENERATED CODE - DO NOT MODIFY BY HAND | ||
// ignore_for_file: type=lint, unused_import | ||
|
||
import 'package:dart_mappable/dart_mappable.dart'; | ||
|
||
part 'dog_type.mapper.dart'; | ||
|
||
@MappableEnum() | ||
enum DogType { | ||
@MappableValue('Dog') | ||
dog, | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// coverage:ignore-file | ||
// GENERATED CODE - DO NOT MODIFY BY HAND | ||
// ignore_for_file: type=lint, unused_import | ||
|
||
import 'package:dart_mappable/dart_mappable.dart'; | ||
|
||
import 'family_members_union.dart'; | ||
|
||
part 'family.mapper.dart'; | ||
|
||
@MappableClass() | ||
class Family with FamilyMappable { | ||
const Family({ | ||
required this.members, | ||
}); | ||
|
||
final List<FamilyMembersUnion> members; | ||
|
||
static Family fromJson(Map<String, dynamic> json) => | ||
FamilyMapper.ensureInitialized().decodeMap<Family>(json); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had to remove the const constuctor as the
discriminatorValue
field needs to be updated after the initial UniversalComponentClass class is constructed.