Skip to content

Commit

Permalink
Read nullable property from anyOf types (#261)
Browse files Browse the repository at this point in the history
  • Loading branch information
MohiuddinM authored Sep 29, 2024
1 parent 3a5b3f3 commit 7d357f4
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 3 deletions.
15 changes: 14 additions & 1 deletion swagger_parser/lib/src/parser/parser/open_api_parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -679,7 +679,20 @@ class OpenApiParser {
final propertyValue = props[propertyName] as Map<String, dynamic>;
final nullablePropertyValue =
propertyValue[_nullableConst].toString().toBool();
final isNullable = nullablePropertyValue ?? false;

final isNullable = nullablePropertyValue ??
switch (propertyValue) {
{_anyOfConst: final List<dynamic> anyOf} => anyOf.any(
(e) => e is Map<String, dynamic> && e['type'] == 'null',
),
{_oneOfConst: final List<dynamic> oneOf} => oneOf.any(
(e) => e is Map<String, dynamic> && e['type'] == 'null',
),
{_allOfConst: final List<dynamic> allOf} => allOf.any(
(e) => e is Map<String, dynamic> && e['type'] == 'null',
),
_ => false,
};

final isRequired = requiredParameters.contains(propertyName);
final typeWithImport = _findType(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ class OneOfElement with _$OneOfElement {
required EnumClass oneClass,
required int allType,
required DateTime anyType,
required EnumClass? nullableClass,
required EnumClass? nullableButRequiredClass,
@Default([]) List<EnumClass>? nullableType,
EnumClass? nullableClass,
@Default(EnumClass.value1) EnumClass anyClass,
@Default([]) List<EnumClass> oneType,
@Default([]) List<EnumClass>? nullableType,
}) = _OneOfElement;

factory OneOfElement.fromJson(Map<String, Object?> json) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"schemas": {
"OneOfElement": {
"type": "object",
"required": ["nullableButRequiredClass"],
"properties": {
"allClass": {
"allOf": [
Expand Down Expand Up @@ -54,6 +55,16 @@
],
"default": "[]"
},
"nullableButRequiredClass": {
"anyOf": [
{
"$ref": "#/components/schemas/EnumClass"
},
{
"type": "null"
}
]
},
"nullableClass": {
"anyOf": [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ class ClassWithNullableTypes with _$ClassWithNullableTypes {
@JsonKey(name: 'p1_list') required String? p1List,
@JsonKey(name: 'p2_list') required List<String?>? p2List,
@JsonKey(name: 'p3_list') required Object1? p3List,
@JsonKey(name: 'nonNull_anyOf') required dynamic nonNullAnyOf,
@JsonKey(name: 'required_null_anyOf') required String? requiredNullAnyOf,
@JsonKey(name: 'p1_anyOf') required String? p1AnyOf,
@JsonKey(name: 'p2_anyOf') required List<String?>? p2AnyOf,
@JsonKey(name: 'p3_anyOf') required Object2? p3AnyOf,
Expand All @@ -38,6 +40,7 @@ class ClassWithNullableTypes with _$ClassWithNullableTypes {
@JsonKey(name: 'p1_n') String? p1N,
@JsonKey(name: 'p2_n') List<String?>? p2N,
@JsonKey(name: 'p3_n') P3n? p3N,
@JsonKey(name: 'optional_null_anyOf') String? optionalNullAnyOf,
}) = _ClassWithNullableTypes;

factory ClassWithNullableTypes.fromJson(Map<String, Object?> json) =>
Expand Down
26 changes: 26 additions & 0 deletions swagger_parser/test/e2e/tests/nullable_types/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,16 @@ components:
- p2_null_item
- p2_null_all
- p3
- required_null_anyOf
- p1_anyOf
- p2_anyOf
- p3_anyOf
- p1_oneOf
- p2_oneOf
- p3_oneOf
- p1_allOf
- p2_allOf
- p3_allOf
properties:
p1:
type: string
Expand Down Expand Up @@ -168,6 +178,19 @@ components:
- string
- null

nonNull_anyOf:
anyOf:
- type: string
- type: int
optional_null_anyOf:
anyOf:
- type: string
- type: null
required_null_anyOf:
anyOf:
- type: string
- type: null

p1_anyOf:
anyOf:
- type: string
Expand Down Expand Up @@ -253,6 +276,9 @@ components:
allOf:
- type: object
- type: null
required:
- p1
- p2
properties:
p1:
allOf:
Expand Down

0 comments on commit 7d357f4

Please sign in to comment.