Skip to content

Commit 4a8c843

Browse files
committed
chore(generator): added more tests
1 parent 8131e8e commit 4a8c843

11 files changed

+682
-23
lines changed

.github/ISSUE_TEMPLATE/1-bug-report.yaml

+12
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,18 @@ body:
2121
description: Explain how a maintainer can reliably reproduce the bug.
2222
validations:
2323
required: true
24+
- type: textarea
25+
attributes:
26+
label: Minimal openapi specification
27+
description: Please provide a minimal specification that reproduces the error.
28+
validations:
29+
required: true
30+
- type: textarea
31+
attributes:
32+
label: Annotation used
33+
description: Please provide the @Openapi annotation you used that reproduces the error.
34+
validations:
35+
required: true
2436
- type: textarea
2537
attributes:
2638
label: Expected behavior

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,6 @@ doc/api/
1919

2020
pubspec_overrides.yaml
2121
!pubspec.lock
22+
23+
.iml
24+
openapi_generator_config.json

example/github_issue_#137.yaml

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
{
2+
"openapi": "3.0.1",
3+
"info": {
4+
"title": "Test API",
5+
"version": "1.0"
6+
},
7+
"paths": {
8+
"/Test": {
9+
"get": {
10+
"responses": {
11+
"200": {
12+
"description": "Success",
13+
"content": {
14+
"application/json": {
15+
"schema": {
16+
"$ref": "#/components/schemas/TestDto"
17+
}
18+
}
19+
}
20+
}
21+
}
22+
}
23+
}
24+
},
25+
"components": {
26+
"schemas": {
27+
"TestDto": {
28+
"required": [
29+
"values"
30+
],
31+
"type": "object",
32+
"properties": {
33+
"values": {
34+
"type": "object",
35+
"additionalProperties": {
36+
"type": "array",
37+
"items": {
38+
"type": "string"
39+
}
40+
}
41+
}
42+
},
43+
"additionalProperties": false
44+
}
45+
}
46+
}
47+
}

openapi-generator/lib/src/openapi_generator_runner.dart

+21-18
Original file line numberDiff line numberDiff line change
@@ -93,25 +93,28 @@ class OpenapiGenerator extends GeneratorForAnnotation<annots.Openapi> {
9393
arguments.cleanSubOutputDirectory?.forEach((directory) {
9494
final childDirectory =
9595
Directory('${arguments.outputDirectory}/${directory.toString()}');
96-
try {
97-
final outputDirectory = Directory('${arguments.outputDirectory}')
98-
.resolveSymbolicLinksSync();
99-
// Make sure is sub directory, avoid ../../
100-
if (childDirectory
101-
.resolveSymbolicLinksSync()
102-
.startsWith(outputDirectory)) {
103-
childDirectory.delete(recursive: true);
96+
if (childDirectory.existsSync() &&
97+
childDirectory.listSync().isNotEmpty) {
98+
try {
99+
final outputDirectory = Directory('${arguments.outputDirectory}')
100+
.resolveSymbolicLinksSync();
101+
// Make sure is sub directory, avoid ../../
102+
if (childDirectory
103+
.resolveSymbolicLinksSync()
104+
.startsWith(outputDirectory)) {
105+
childDirectory.delete(recursive: true);
106+
}
107+
} catch (e, st) {
108+
logOutputMessage(
109+
log: log,
110+
communication: OutputMessage(
111+
message: 'Output directory already empty',
112+
additionalContext: e,
113+
stackTrace: st,
114+
level: Level.WARNING,
115+
),
116+
);
104117
}
105-
} catch (e, st) {
106-
logOutputMessage(
107-
log: log,
108-
communication: OutputMessage(
109-
message: 'Output directory already empty',
110-
additionalContext: e,
111-
stackTrace: st,
112-
level: Level.WARNING,
113-
),
114-
);
115118
}
116119
});
117120
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
import 'dart:io';
2+
3+
import 'package:path/path.dart' as path;
4+
import 'package:test/expect.dart';
5+
import 'package:test/scaffolding.dart';
6+
7+
import 'utils.dart';
8+
9+
/// We test the build runner by mocking the specs and then checking the output
10+
/// content for the expected generate command.
11+
void main() {
12+
group('Github Issues', () {
13+
// setUpAll(() {
14+
// if (!f.existsSync()) {
15+
// f.createSync(recursive: true);
16+
// }
17+
// f.writeAsStringSync('{}');
18+
// });
19+
// tearDown(() {
20+
// if (f.existsSync()) {
21+
// f.deleteSync();
22+
// }
23+
// });
24+
group('#137', () {
25+
var parentFolder = path.join(testSpecPath, 'issue', '137');
26+
var workingDirectory = path.join(parentFolder, 'output');
27+
setUpAll(
28+
() {
29+
var workingDirectory = path.join(parentFolder, 'output');
30+
cleanup(workingDirectory);
31+
},
32+
);
33+
test('Test that valid model generated via List in additional properties',
34+
() async {
35+
var annotatedFile =
36+
File('$parentFolder/github_issue_137_test_config.dart');
37+
var annotatedFileContents = annotatedFile.readAsStringSync();
38+
var inputSpecFile = File('$parentFolder/github_issue_#137.yaml');
39+
// final annotations = (await resolveSource(
40+
// annotatedFileContents,
41+
// (resolver) async =>
42+
// (await resolver.findLibraryByName('test_lib'))!))
43+
// .getClass('TestClassConfig')!
44+
// .metadata
45+
// .map((e) => ConstantReader(e.computeConstantValue()!))
46+
// .first;
47+
// final args = GeneratorArguments(annotations: annotations);
48+
var generatedOutput = await generateForSource(annotatedFile.path,
49+
openapiSpecFilePath: inputSpecFile.path);
50+
51+
var workingDirectory = path.join(parentFolder, 'output');
52+
var analyzeResult = await Process.run(
53+
'dart',
54+
['analyze', '--fatal-warnings'],
55+
workingDirectory: workingDirectory,
56+
);
57+
expect(analyzeResult.exitCode, 0, reason: '${analyzeResult.stdout}');
58+
cleanup(workingDirectory);
59+
}, skip: true);
60+
});
61+
62+
group('#135', () {
63+
var parentFolder = path.join(testSpecPath, 'issue', '135');
64+
var workingDirectory = path.join(parentFolder, 'output');
65+
setUpAll(
66+
() {
67+
var workingDirectory = path.join(parentFolder, 'output');
68+
cleanup(workingDirectory);
69+
},
70+
);
71+
test(
72+
'Test that code generation succeeds on OpenAPI 3.1.0 API definition with dart generator',
73+
() async {
74+
var annotatedFile =
75+
File('$parentFolder/github_issue_135_dart_test_config.dart');
76+
var annotatedFileContents = annotatedFile.readAsStringSync();
77+
var inputSpecFile = File('$parentFolder/github_issue_#135.json');
78+
79+
var generatedOutput = await generateForSource(annotatedFile.path,
80+
openapiSpecFilePath: inputSpecFile.path);
81+
82+
expect(generatedOutput,
83+
contains('Skipping source gen because generator does not need it.'),
84+
reason: generatedOutput);
85+
expect(generatedOutput, contains('Successfully formatted code.'),
86+
reason: generatedOutput);
87+
var analyzeResult = await Process.run(
88+
'dart',
89+
['analyze', '--no-fatal-warnings'],
90+
workingDirectory: workingDirectory,
91+
);
92+
expect(analyzeResult.exitCode, 0,
93+
reason: '${analyzeResult.stdout}\n\n${analyzeResult.stderr}');
94+
cleanup(workingDirectory);
95+
}, skip: true);
96+
test(
97+
'Test that code generation succeeds on OpenAPI 3.1.0 API definition with dio generator',
98+
() async {
99+
var annotatedFile =
100+
File('$parentFolder/github_issue_135_dio_test_config.dart');
101+
var annotatedFileContents = annotatedFile.readAsStringSync();
102+
var inputSpecFile = File('$parentFolder/github_issue_#135.json');
103+
104+
var generatedOutput = await generateForSource(annotatedFile.path,
105+
openapiSpecFilePath: inputSpecFile.path);
106+
107+
// expect(generatedOutput, contains('Skipping source gen because generator does not need it.'),reason:generatedOutput);
108+
expect(generatedOutput, contains('Successfully formatted code.'),
109+
reason: generatedOutput);
110+
var workingDirectory = path.join(parentFolder, 'output');
111+
var analyzeResult = await Process.run(
112+
'dart',
113+
['analyze', '--no-fatal-warnings'],
114+
workingDirectory: workingDirectory,
115+
);
116+
expect(analyzeResult.exitCode, 0,
117+
reason: '${analyzeResult.stdout}\n\n ${analyzeResult.stderr}');
118+
cleanup(workingDirectory);
119+
},
120+
);
121+
});
122+
});
123+
}
124+
125+
void cleanup(String path) async {
126+
final directory = Directory(path);
127+
128+
if (await directory.exists()) {
129+
await directory.delete(recursive: true);
130+
print('Folder deleted successfully.');
131+
} else {
132+
print('Folder does not exist.');
133+
}
134+
}

0 commit comments

Comments
 (0)