-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement support for discriminated unions in Freezed DTO gener…
…ation - Added discriminator handling in `UniversalComponentClass` to support oneOf variants. - Updated `dart_freezed_dto_template.dart` to generate sealed classes and union factories based on discriminator values. - Introduced new test cases for discriminated oneOf schemas, including a sample JSON schema and expected output files. - Enhanced OpenApiParser to correctly parse and map discriminator properties from OpenAPI specifications.
- Loading branch information
Showing
20 changed files
with
452 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
swagger_parser/test/e2e/tests/basic/discriminated_one_of.3.0/discriminated_one_of.3.0.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] | ||
} | ||
} | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
swagger_parser/test/e2e/tests/basic/discriminated_one_of.3.0/expected_files/export.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
22 changes: 22 additions & 0 deletions
22
swagger_parser/test/e2e/tests/basic/discriminated_one_of.3.0/expected_files/models/cat.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// coverage:ignore-file | ||
// GENERATED CODE - DO NOT MODIFY BY HAND | ||
// ignore_for_file: type=lint, unused_import | ||
|
||
import 'package:freezed_annotation/freezed_annotation.dart'; | ||
|
||
import 'cat_type.dart'; | ||
|
||
part 'cat.freezed.dart'; | ||
part 'cat.g.dart'; | ||
|
||
@Freezed() | ||
class Cat with _$Cat { | ||
const factory Cat({ | ||
required CatType type, | ||
|
||
/// Number of times the cat meows. | ||
required int mewCount, | ||
}) = _Cat; | ||
|
||
factory Cat.fromJson(Map<String, Object?> json) => _$CatFromJson(json); | ||
} |
Oops, something went wrong.