Skip to content

Commit a17d7bb

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

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

openapi-generator/lib/src/gen_on_spec_changes.dart

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ final _supportedRegexes = [jsonRegex, yamlRegex];
2121
/// - yml
2222
///
2323
/// It also throws an error when the specification doesn't exist on disk.
24-
FutureOr<Map<String, dynamic>> loadSpec({required String specPath}) async {
24+
FutureOr<Map<String, dynamic>> loadSpec(
25+
{required String specPath, bool isCached = false}) async {
2526
// If the spec file doesn't match any of the currently supported spec formats
2627
// reject the request.
2728
if (!_supportedRegexes.any((fileEnding) => fileEnding.hasMatch(specPath))) {
@@ -43,6 +44,13 @@ FutureOr<Map<String, dynamic>> loadSpec({required String specPath}) async {
4344
return spec;
4445
}
4546

47+
// In the event that the cached spec isn't found, provide an empty mapping
48+
// to diff against. This will cause the isSpecDirty check to return true.
49+
// This can occur on a fresh build / clone.
50+
if (isCached) {
51+
return {};
52+
}
53+
4654
return Future.error('Unable to find spec file $specPath');
4755
}
4856

@@ -53,6 +61,12 @@ bool isSpecDirty({
5361
required Map<String, dynamic> cachedSpec,
5462
required Map<String, dynamic> loadedSpec,
5563
}) {
64+
// The spec always needs to be updated if the cached spec is empty, unless
65+
// the loaded spec is also empty.
66+
if (cachedSpec.isEmpty) {
67+
return true && loadedSpec.isNotEmpty;
68+
}
69+
// TODO: Should this be a future? This way the errors can be bubbled up?
5670
if (loadedSpec.keys.length == cachedSpec.keys.length) {
5771
for (final entry in cachedSpec.entries) {
5872
if (!loadedSpec.containsKey(entry.key)) {
@@ -194,6 +208,10 @@ List<dynamic> convertYamlListToDartList({required YamlList yamlList}) {
194208
return converted;
195209
}
196210

211+
/// Caches the updated [spec] to disk for use in future comparisons.
212+
///
213+
/// Caches the [spec] to the given [outputDirectory]. By default this will be likely
214+
/// be the .dart_tool or build directory.
197215
Future<void> cacheSpec({
198216
required String outputDirectory,
199217
required Map<String, dynamic> spec,

openapi-generator/test/gen_on_spec_changes_test.dart

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,16 @@ void main() {
3838
'Unable to find spec file ./thisIsSomeInvalidPath.yaml');
3939
}
4040
});
41+
test('returns empty map when cache isn\'t found', () async {
42+
try {
43+
final cached = await loadSpec(
44+
specPath: './nonValidCacheSpecPath.yaml', isCached: true);
45+
expect(cached, isEmpty);
46+
} catch (e, st) {
47+
fail(
48+
'Should return empty map when spec path is cached spec but not found');
49+
}
50+
});
4151
group('returns a map', () {
4252
test('json', () async {
4353
try {
@@ -70,6 +80,14 @@ void main() {
7080
});
7181
});
7282
group('verifies dirty status', () {
83+
test('is true when the cached spec is empty', () {
84+
expect(isSpecDirty(cachedSpec: {}, loadedSpec: {'key1': '2'}), isTrue);
85+
});
86+
test(
87+
'is false when the cached spec is empty and loaded spec is also empty',
88+
() {
89+
expect(isSpecDirty(cachedSpec: {}, loadedSpec: {}), isFalse);
90+
});
7391
test('returns false when specs match', () async {
7492
final loaded = await loadSpec(specPath: supportedExtensions['json']!);
7593
expect(
@@ -87,7 +105,8 @@ void main() {
87105
test('returns true when root/sub keys differ in length', () {
88106
expect(
89107
isSpecDirty(
90-
cachedSpec: {}, loadedSpec: {'someExtraKey': 'content'}),
108+
cachedSpec: {'someKey': 1},
109+
loadedSpec: {'someKey': 1, 'someExtraKey': 'content'}),
91110
isTrue);
92111

93112
expect(

0 commit comments

Comments
 (0)