Skip to content

Commit a2ddac2

Browse files
committed
Improved support for nullable Map types
1 parent 12bde1d commit a2ddac2

File tree

4 files changed

+21
-6
lines changed

4 files changed

+21
-6
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## 0.7.23
4+
5+
* Improved support for nullable `Map` types
6+
37
## 0.7.22
48

59
* Ignore null values in client query parameters

lib/src/open_api/spec.dart

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -650,9 +650,10 @@ Map<String, dynamic> _formatSpecFromJson({
650650
m['type'] = 'map';
651651
}
652652
} else if (m.containsKey('anyOf')) {
653-
final anyOf = m['anyOf'];
653+
dynamic anyOf = m['anyOf'];
654654
if (anyOf is List) {
655-
final typeSet = anyOf.map((e) {
655+
final anyOfList = List<dynamic>.from(anyOf);
656+
final typeSet = anyOfList.map((e) {
656657
if (e is Map && e.containsKey('type')) {
657658
return e['type'];
658659
} else if (e is Map && e.containsKey('\$ref')) {
@@ -661,15 +662,25 @@ Map<String, dynamic> _formatSpecFromJson({
661662
return null;
662663
}).toSet();
663664
if (typeSet.length == 1) {
664-
m['type'] = anyOf.first['type'];
665+
m['type'] = anyOfList.first['type'];
665666
} else if (typeSet.length == 2 && typeSet.contains('null')) {
666667
// Starting in OpenAPI 3.1, anyOf is used to define unions for nullable properties
667668
// We do not want to create a union schema for these properties
668669
final propType = typeSet.firstWhere((e) => e != 'null');
669670
if (propType.toString().contains('#')) {
670671
m['\$ref'] = propType;
671672
} else {
672-
m['type'] = propType;
673+
// One final check to see if this is a map type
674+
for (final a in anyOfList) {
675+
if (a['type'] == propType &&
676+
propType == 'object' &&
677+
a['additionalProperties'] != null) {
678+
m['type'] = 'map';
679+
}
680+
}
681+
if (!m.containsKey('type')) {
682+
m['type'] = propType;
683+
}
673684
}
674685
m['nullable'] = true;
675686
if (propType == 'array') {

lib/src/version.dart

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: openapi_spec
22
description: OpenAPI Specification generator using native Dart code, as well as an all-in-one parser of existing specifications.
3-
version: 0.7.22
3+
version: 0.7.23
44
maintainer: Taza Technology LLC
55
repository: https://github.com/tazatechnology/openapi_spec
66
issue_tracker: https://github.com/tazatechnology/openapi_spec/issues

0 commit comments

Comments
 (0)