Skip to content

Commit f2d6c04

Browse files
committed
fix(annotations): added doc for --name-mappings and moved some things around
fixes #114
1 parent 68382b9 commit f2d6c04

File tree

7 files changed

+166
-5
lines changed

7 files changed

+166
-5
lines changed

melos.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ scripts:
2424
test:
2525
run: |
2626
melos exec -c 6 --fail-fast -- \
27-
"dart run test --coverage='coverage'"
27+
"dart run test --chain-stack-traces --coverage='coverage'"
2828
description: Run `flutter test` for a specific package.
2929
packageFilters:
3030
dirExists:

openapi-generator-annotations/lib/src/openapi_generator_annotations_base.dart

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,11 @@ class Openapi {
8484
/// --type-mappings
8585
final Map<String, String>? typeMappings;
8686

87-
/// sets mappings between OpenAPI spec properties name and generated code
88-
/// var/param/model in the format of OpenAPIName=generatedName.
89-
/// For example: update=updatable,_=underscore.
87+
/// a map to store the mapping between property name and the name provided by the user
88+
///
89+
/// specifies mappings between the property name and the new name in the format
90+
/// of property_a=firstProperty,property_b=secondProperty.
91+
/// [HTTP](https://openapi-generator.tech/docs/customization/#name-mapping)
9092
9193
/// --name-mappings
9294
final Map<String, String>? nameMappings;

openapi-generator/test/github_issues_test.dart

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,83 @@ void main() {
118118
},
119119
);
120120
});
121+
122+
group('#114', () {
123+
var issueNumber = '114';
124+
var parentFolder = path.join(testSpecPath, 'issue', issueNumber);
125+
var workingDirectory = path.join(parentFolder, 'output');
126+
setUpAll(
127+
() {
128+
var workingDirectory = path.join(parentFolder, 'output');
129+
cleanup(workingDirectory);
130+
},
131+
);
132+
test(
133+
'[dart] Test that trailing underscore does not get removed while parsing',
134+
() async {
135+
var annotatedFile = File(
136+
'$parentFolder/github_issue_${issueNumber}_dart_test_config.dart');
137+
// var annotatedFileContents = annotatedFile.readAsStringSync();
138+
var inputSpecFile =
139+
File('$parentFolder/github_issue_#$issueNumber.json');
140+
141+
var generatedOutput = await generateForSource(
142+
annotatedFile.path,
143+
openapiSpecFilePath: inputSpecFile.path,
144+
preProcessor: (annotatedFileContent) =>
145+
annotatedFileContent.replaceAll('{{issueNumber}}', issueNumber),
146+
);
147+
148+
expect(generatedOutput,
149+
contains('Skipping source gen because generator does not need it.'),
150+
reason: generatedOutput);
151+
expect(generatedOutput, contains('Successfully formatted code.'),
152+
reason: generatedOutput);
153+
var analyzeResult = await Process.run(
154+
'dart',
155+
['analyze', '--no-fatal-warnings'],
156+
workingDirectory: workingDirectory,
157+
);
158+
expect(analyzeResult.exitCode, 0,
159+
reason: '${analyzeResult.stdout}\n\n${analyzeResult.stderr}');
160+
cleanup(workingDirectory);
161+
});
162+
163+
test(
164+
'[dio] Test that trailing underscore does not get removed while parsing',
165+
() async {
166+
var annotatedFile = File(
167+
'$parentFolder/github_issue_${issueNumber}_dio_test_config.dart');
168+
// var annotatedFileContents = annotatedFile.readAsStringSync();
169+
var inputSpecFile =
170+
File('$parentFolder/github_issue_#$issueNumber.json');
171+
172+
var generatedOutput = await generateForSource(
173+
annotatedFile.path,
174+
openapiSpecFilePath: inputSpecFile.path,
175+
preProcessor: (annotatedFileContent) =>
176+
annotatedFileContent.replaceAll('{{issueNumber}}', issueNumber),
177+
);
178+
179+
expect(
180+
generatedOutput,
181+
contains(
182+
'pub run build_runner build --delete-conflicting-outputs'),
183+
reason: generatedOutput);
184+
expect(generatedOutput, contains('Successfully formatted code.'),
185+
reason: generatedOutput);
186+
var workingDirectory = path.join(parentFolder, 'output');
187+
var analyzeResult = await Process.run(
188+
'dart',
189+
['analyze', '--no-fatal-warnings'],
190+
workingDirectory: workingDirectory,
191+
);
192+
expect(analyzeResult.exitCode, 0,
193+
reason: '${analyzeResult.stdout}\n\n ${analyzeResult.stderr}');
194+
cleanup(workingDirectory);
195+
},
196+
);
197+
});
121198
});
122199
}
123200

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
{
2+
"openapi": "3.0.1",
3+
"info": {
4+
"title": "my-service",
5+
"version": "1.0"
6+
},
7+
"servers": [
8+
{
9+
"url": "/my-service"
10+
}
11+
],
12+
"security": [
13+
{
14+
"Authorization": []
15+
}
16+
],
17+
"paths": {
18+
"/api/v2/subscription/cancel": {
19+
"delete": {
20+
"tags": [
21+
"subscription-v-2-controller"
22+
],
23+
"operationId": "cancelSubscription_2",
24+
"responses": {
25+
"200": {
26+
"description": "OK"
27+
}
28+
}
29+
}
30+
}
31+
},
32+
"components": {
33+
"schemas": {
34+
"WechatPayRedirectToAndroidApp": {
35+
"type": "object",
36+
"properties": {
37+
"timestamp": {
38+
"type": "string"
39+
},
40+
"package_": {
41+
"type": "string"
42+
},
43+
"package": {
44+
"type": "string"
45+
}
46+
}
47+
}
48+
}
49+
}
50+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import 'package:openapi_generator_annotations/openapi_generator_annotations.dart';
2+
3+
@Openapi(
4+
inputSpec: InputSpec(
5+
path: './test/specs/issue/114/github_issue_#{{issueNumber}}.json',
6+
),
7+
updateAnnotatedFile: false,
8+
nameMappings: {'package_': 'otherPackage'},
9+
additionalProperties:
10+
AdditionalProperties(pubName: 'salad_api_client', pubAuthor: 'Google'),
11+
generatorName: Generator.dart,
12+
cleanSubOutputDirectory: ['./test/specs/issue/{{issueNumber}}/output'],
13+
cachePath: './test/specs/issue/{{issueNumber}}/output/cache.json',
14+
outputDirectory: './test/specs/issue/{{issueNumber}}/output')
15+
class GithubIssue135 {}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import 'package:openapi_generator_annotations/openapi_generator_annotations.dart';
2+
3+
@Openapi(
4+
inputSpec: InputSpec(
5+
path: './test/specs/issue/114/github_issue_#{{issueNumber}}.json',
6+
),
7+
updateAnnotatedFile: false,
8+
nameMappings: {'package_': 'otherPackage'},
9+
additionalProperties:
10+
AdditionalProperties(pubName: 'salad_api_client', pubAuthor: 'Google'),
11+
generatorName: Generator.dio,
12+
cleanSubOutputDirectory: ['./test/specs/issue/{{issueNumber}}/output'],
13+
cachePath: './test/specs/issue/{{issueNumber}}/output/cache.json',
14+
outputDirectory: './test/specs/issue/{{issueNumber}}/output')
15+
class GithubIssue135 {}

openapi-generator/test/utils.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,14 @@ final testSpecPath = path.join(Directory.current.path, 'test', 'specs/');
2525
Future<String> generateForSource(String annotatedFilePath,
2626
{String path = 'lib/myapp.dart',
2727
String? openapiSpecFilePath,
28+
String Function(String annotatedFileContent)? preProcessor,
2829
Map<String, String>? additionalSources}) async {
2930
final spec = File(openapiSpecFilePath ?? '${testSpecPath}openapi.test.yaml')
3031
.readAsStringSync();
3132
final annotatedContent = File(annotatedFilePath).readAsStringSync();
3233
var srcs = <String, String>{
33-
'openapi_generator|$path': annotatedContent,
34+
'openapi_generator|$path':
35+
preProcessor?.call(annotatedContent) ?? annotatedContent,
3436
'openapi_generator|openapi-spec.yaml': spec,
3537
if (additionalSources?.isNotEmpty == true) ...additionalSources!,
3638
};

0 commit comments

Comments
 (0)