Skip to content

Commit 49a96e2

Browse files
committed
Provide support for nested lists, reorganize some tests
1 parent d317ed9 commit 49a96e2

File tree

2 files changed

+92
-40
lines changed

2 files changed

+92
-40
lines changed

openapi-generator/lib/src/gen_on_spec_changes.dart

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -149,29 +149,16 @@ bool isSpecDirty({
149149
/// Converts a [YamlMap] and it's children into a [Map]. This involes expanding
150150
/// [YamlList] & [YamlMap] nodes into their entries. [YamlScalar] values are
151151
/// directly set.
152-
///
153-
/// Currently this makes the assumption that [YamlList] node shouldn't be a valid
154-
/// child entry within another [YamlList] and ignores the contents.
155152
Map<String, dynamic> convertYamlMapToDartMap({required YamlMap yamlMap}) {
156153
final transformed = <String, dynamic>{};
157154

158155
yamlMap.forEach((key, value) {
159156
late dynamic content;
160157
if (value is YamlList) {
161158
// Parse list entries
162-
content = [];
163-
value.forEach((element) {
164-
if (element is YamlList) {
165-
// TODO: Is this a potential case.
166-
log('Found a YamlList within a YamlList');
167-
} else if (element is YamlMap) {
168-
content.add(convertYamlMapToDartMap(yamlMap: element));
169-
} else {
170-
content.add(element);
171-
}
172-
});
159+
content = convertYamlListToDartList(yamlList: value);
173160
} else if (value is YamlMap) {
174-
// Parse the sub mapyamlParsedMap
161+
// Parse the sub map
175162
content = convertYamlMapToDartMap(
176163
yamlMap: YamlMap.internal(value.nodes, value.span, value.style));
177164
} else if (value is YamlScalar) {
@@ -187,6 +174,26 @@ Map<String, dynamic> convertYamlMapToDartMap({required YamlMap yamlMap}) {
187174
return transformed;
188175
}
189176

177+
/// Converts the given [yamlList] into a Dart [List].
178+
///
179+
/// Recursively converts the given [yamlList] to a Dart [List]; converting all
180+
/// nested lists into their constituent values.
181+
List<dynamic> convertYamlListToDartList({required YamlList yamlList}) {
182+
final converted = [];
183+
184+
yamlList.forEach((element) {
185+
if (element is YamlList) {
186+
converted.add(convertYamlListToDartList(yamlList: element));
187+
} else if (element is YamlMap) {
188+
converted.add(convertYamlMapToDartMap(yamlMap: element));
189+
} else {
190+
converted.add(element);
191+
}
192+
});
193+
194+
return converted;
195+
}
196+
190197
Future<void> cacheSpec({
191198
required String outputDirectory,
192199
required Map<String, dynamic> spec,

openapi-generator/test/gen_on_spec_changes_test.dart

Lines changed: 70 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,24 @@ void main() {
4949
fail('should have successfully loaded json spec');
5050
}
5151
});
52+
test('yaml (requires transformation)', () async {
53+
try {
54+
final loaded =
55+
await loadSpec(specPath: supportedExtensions['yaml']!);
56+
expect(loaded, jsonSpecFile);
57+
} catch (_, __) {
58+
fail('Should successfully convert yaml to Map');
59+
}
60+
});
61+
test('yml (requires transformation)', () async {
62+
try {
63+
final loaded =
64+
await loadSpec(specPath: supportedExtensions['yml']!);
65+
expect(loaded, jsonSpecFile);
66+
} catch (_, __) {
67+
fail('Should successfully convert yml to Map');
68+
}
69+
});
5270
});
5371
});
5472
group('verifies dirty status', () {
@@ -127,37 +145,64 @@ void main() {
127145
});
128146
});
129147
group('transforms yaml to dart map', () {
130-
test('returns a map from yaml', () async {
131-
expect(await loadSpec(specPath: supportedExtensions['yaml']!),
132-
jsonSpecFile);
133-
});
134-
test('returns a map from yml', () async {
135-
expect(await loadSpec(specPath: supportedExtensions['yml']!),
136-
jsonSpecFile);
137-
});
138148
test('converts scalars', () {
139149
expect(convertYamlMapToDartMap(yamlMap: YamlMap.wrap({'scalar': 5})),
140150
{'scalar': 5});
141151
});
142-
test('converts list', () {
143-
final listContent = [
144-
1,
145-
2,
146-
3,
147-
4,
148-
YamlMap.wrap(<String, dynamic>{'entry': 'value'})
149-
];
150-
final listContentExpected = [
151-
1,
152-
2,
153-
3,
154-
4,
155-
<String, dynamic>{'entry': 'value'}
156-
];
152+
group('converts lists', () {
153+
test('with YamlMaps', () {
154+
final listContent = [
155+
1,
156+
2,
157+
3,
158+
4,
159+
YamlMap.wrap(<String, dynamic>{'entry': 'value'})
160+
];
161+
final listContentExpected = [
162+
1,
163+
2,
164+
3,
165+
4,
166+
<String, dynamic>{'entry': 'value'}
167+
];
168+
expect(
169+
convertYamlListToDartList(yamlList: YamlList.wrap(listContent)),
170+
listContentExpected);
171+
});
172+
test('with nested lists', () {
173+
final listContent = [
174+
1,
175+
2,
176+
3,
177+
4,
178+
YamlList.wrap(
179+
["one", "two", "three"],
180+
)
181+
];
182+
final listContentExpected = [
183+
1,
184+
2,
185+
3,
186+
4,
187+
["one", "two", "three"],
188+
];
189+
expect(
190+
convertYamlListToDartList(yamlList: YamlList.wrap(listContent)),
191+
listContentExpected);
192+
});
193+
});
194+
test('converts submap to map', () {
195+
final expectedMap = <String, dynamic>{
196+
'mapWithSubMap': {
197+
'subMap': {'scalar': 5, 'meh': 'value'},
198+
}
199+
};
157200
expect(
158201
convertYamlMapToDartMap(
159-
yamlMap: YamlMap.wrap({'scalar': YamlList.wrap(listContent)})),
160-
{'scalar': listContentExpected});
202+
yamlMap: YamlMap.wrap({
203+
'mapWithSubMap': YamlMap.wrap(expectedMap['mapWithSubMap'])
204+
})),
205+
expectedMap);
161206
});
162207
});
163208
});

0 commit comments

Comments
 (0)