Skip to content

Commit 1232ea0

Browse files
committed
docs: updated known issues doc
1 parent 09f6039 commit 1232ea0

File tree

15 files changed

+153
-87
lines changed

15 files changed

+153
-87
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,9 @@ New:
134134
class Example {}
135135
```
136136

137+
## Known Issues
138+
139+
Check out the known issues article here [Known Issues](openapi-generator-annotations/README.md#known-issues)
137140

138141
## Contributing
139142

example/lib/main.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Openapi Generator last run: : 2024-01-16T01:49:21.940229
1+
// Openapi Generator last run: : 2024-01-16T03:54:39.346434
22
import 'package:flutter/material.dart';
33
import 'package:openapi_generator_annotations/openapi_generator_annotations.dart';
44

openapi-generator-annotations/README.md

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -53,24 +53,8 @@ Give a thumbs up if you like this library
5353

5454

5555
## Known Issues
56-
### Dependency issues/conflicts
57-
This is not an issue with this library but with flutter/dart in general. If you are having issues with dependencies, what
58-
you can do is make use of dependency overrides. This is added to the pubspec.yaml of the generated package and then the pubspec
59-
must be added to the .openapi-generator-ignore of the generated package.
60-
For example, let's assume you want to override the analyzer package for the generated source
6156

62-
in generatedsource/pubspec.yaml add the following
63-
```yaml
64-
dependency_overrides:
65-
analyzer: 1.0.0
66-
```
67-
Then in generatedsources/.openapi-generator-ignore, add the below so that the pubspec is not overwritten next time you run source gen
68-
```.gitignore
69-
pubspec.yaml
70-
```
71-
72-
The above steps are usefull when you have issues with dependency conflicts, clashes. You can even use it to upgrade the
73-
library packages in the generated source.
57+
Check out the known issues article here [Known Issues](../openapi-generator-annotations/README.md#known-issues)
7458

7559
## Features and bugs
7660

openapi-generator-cli/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ pubspec.yaml
5151
The above steps are usefull when you have issues with dependency conflicts, clashes. You can even use it to upgrade the
5252
library packages in the generated source.
5353

54+
## Known Issues
55+
56+
Check out the known issues article here [Known Issues](../openapi-generator-annotations/README.md#known-issues)
57+
5458
## Contributing
5559

5660
Please read our [Contributing guidelines](../CONTRIBUTING.md) before submitting a Pull Request.

openapi-generator/README.md

Lines changed: 73 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,40 @@ flutter pub run build_runner build --delete-conflicting-outputs
4545

4646
The api sdk will be generated in the folder specified in the annotation. See examples for more details
4747

48+
## Next Generation
49+
50+
As of version 5.0 of this library, there is some new functionality that has been added to the generator. This version
51+
will have the ability to:
52+
53+
- cache changes in the openapi spec
54+
- Rerun when there ares difference in the cached copy and current copy
55+
- Pull from a remote source and cache that.
56+
- **Note**: This means that your cache could be potentially stale. But in that case this flow will still pull the
57+
latest and run.
58+
- While this is a possible usage, if you are actively developing your spec it is preferred you provide a local copy.
59+
- Skip generation based off:
60+
- Flags
61+
- No difference between the cache and local
62+
- And all the functionality provided previously.
63+
64+
Your original workflow stay the same but there is a slight difference in the annotations.
65+
66+
New:
67+
68+
```dart
69+
@Openapi(
70+
additionalProperties:
71+
DioProperties(pubName: 'petstore_api', pubAuthor: 'Johnny dep..'),
72+
inputSpec:
73+
RemoteSpec(path: 'https://petstore3.swagger.io/api/v3/openapi.json'),
74+
typeMappings: {'Pet': 'ExamplePet'},
75+
generatorName: Generator.dio,
76+
runSourceGenOnOutput: true,
77+
outputDirectory: 'api/petstore_api',
78+
)
79+
class Example {}
80+
```
81+
4882
## Known Issues
4983

5084
### Dependency issues/conflicts
@@ -73,14 +107,48 @@ pubspec.yaml
73107
The above steps are useful when you have issues with dependency conflicts, clashes. You can even use it to upgrade the
74108
library packages in the generated source.
75109

76-
## FAQ
110+
### Resolving Issues with Generated Code
111+
112+
This library is a wrapper around [openapi generator](https://github.com/OpenAPITools/openapi-generator) to enable
113+
seamless (kind of) integration into dart's build system.
114+
The underlying generator itself is not 100% perfect mainly because it is multipurpose built and sometimes generates bad
115+
code due to various reasons including incorrect openapi doc.
116+
If you encounter issues with the code generated by the OpenAPI generator, there are two main ways to resolve them:
77117

78-
Q: I run source gen (`flutter pub run build_runner build --delete-conflicting-outputs`), The api generator does not run.
118+
#### 1. Correct the OpenAPI Documentation
79119

80-
A: The source generator of flutter only runs when there are changes to the file that has the annotation. If this ever
81-
happens, just go to the file that has the `@openapi` annotation and edit something in the file.
120+
The issue might be due to incorrect or conflicting OpenAPI documentation. For instance, if a model name conflicts with
121+
Dart's reserved names, you should edit the OpenAPI documentation to fix the issue.
122+
123+
#### 2. Manually Fix the Generated Code
124+
125+
If correcting the OpenAPI documentation is not possible or you don't have access to it, you can manually fix the
126+
generated code.
127+
128+
Here are the steps to do this:
129+
130+
1. Identify the files with the bad code and manually correct them.
131+
2. Add the manually edited files to the `.openapi-generator-ignore` file. This ensures that your changes are not
132+
overridden when you run the generator again.
133+
below is a sample of the `.openapi-generator-ignore` file. This file uses the `.gitignore` syntax and also has
134+
documentation in it.
135+
136+
```gitignore
137+
# You can also negate patterns with an exclamation (!).
138+
# For example, you can ignore all files in a docs folder with the file extension .md:
139+
docs/*.md
140+
# Then explicitly reverse the ignore rule for a single file:
141+
!docs/README.md
142+
path/to/corrected.dart
143+
```
144+
145+
Remember to commit the `.openapi-generator-ignore` file to your git repository to preserve these changes.
146+
147+
By following these steps, you should be able to resolve issues with the generated code.
148+
149+
## FAQ
82150

83-
Q: How do I prevent files from being generated e.g tests
151+
#### Q: How do I prevent files from being generated e.g tests
84152

85153
A: To prevent any files from being generated, you need to add it to ```.openapi-generator-ignore```. This file is in the
86154
root of the generated code. For example, to prevent generating tests, add ```test/*``` to the file.
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import 'package:build/build.dart';
22
import 'package:openapi_generator/src/openapi_generator_runner.dart';
3+
import 'package:openapi_generator/src/process_runner.dart';
34
import 'package:source_gen/source_gen.dart';
45

56
Builder openApiClientSdk(BuilderOptions options) =>
6-
LibraryBuilder(OpenapiGenerator(),
7+
LibraryBuilder(OpenapiGenerator(ProcessRunner()),
78
generatedExtension: '.openapi_generator');

openapi-generator/lib/src/models/generator_arguments.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ class GeneratorArguments {
155155
FutureOr<List<String>> get jarArgs async => [
156156
'generate',
157157
if (outputDirectory?.isNotEmpty ?? false) '-o=$outputDirectory',
158-
'-i=${await inputFileOrFetch}',
158+
'-i=$inputFileOrFetch',
159159
if (templateDirectory?.isNotEmpty ?? false) '-t=$templateDirectory',
160160
'-g=$generatorName',
161161
if (skipValidation) '--skip-validate-spec',

openapi-generator/lib/src/openapi_generator_runner.dart

Lines changed: 33 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import 'package:logging/logging.dart';
99
import 'package:openapi_generator/src/determine_flutter_project_status.dart';
1010
import 'package:openapi_generator/src/gen_on_spec_changes.dart';
1111
import 'package:openapi_generator/src/models/output_message.dart';
12+
import 'package:openapi_generator/src/process_runner.dart';
1213
import 'package:openapi_generator/src/utils.dart';
1314
import 'package:openapi_generator_annotations/openapi_generator_annotations.dart'
1415
as annots;
@@ -18,10 +19,9 @@ import 'models/command.dart';
1819
import 'models/generator_arguments.dart';
1920

2021
class OpenapiGenerator extends GeneratorForAnnotation<annots.Openapi> {
21-
@Deprecated('To be removed in next major version')
22-
final bool testMode;
22+
final ProcessRunner _processRunner;
2323

24-
OpenapiGenerator({this.testMode = false});
24+
OpenapiGenerator(this._processRunner);
2525

2626
@override
2727
FutureOr<String> generateForAnnotatedElement(
@@ -98,21 +98,17 @@ class OpenapiGenerator extends GeneratorForAnnotation<annots.Openapi> {
9898
var javaOpts = Platform.environment['JAVA_OPTS'] ?? '';
9999

100100
ProcessResult result;
101-
if (!testMode) {
102-
result = await Process.run(
103-
'java',
104-
[
105-
if (javaOpts.isNotEmpty) javaOpts,
106-
'-jar',
107-
binPath,
108-
...args,
109-
],
110-
workingDirectory: Directory.current.path,
111-
runInShell: Platform.isWindows,
112-
);
113-
} else {
114-
result = ProcessResult(999999, 0, null, null);
115-
}
101+
result = await _processRunner.run(
102+
'java',
103+
[
104+
if (javaOpts.isNotEmpty) javaOpts,
105+
'-jar',
106+
binPath,
107+
...args,
108+
],
109+
workingDirectory: Directory.current.path,
110+
runInShell: Platform.isWindows,
111+
);
116112

117113
if (result.exitCode != 0) {
118114
return Future.error(
@@ -342,16 +338,12 @@ class OpenapiGenerator extends GeneratorForAnnotation<annots.Openapi> {
342338
);
343339

344340
ProcessResult results;
345-
if (!testMode) {
346-
results = await Process.run(
347-
command.executable,
348-
command.arguments,
349-
runInShell: Platform.isWindows,
350-
workingDirectory: args.outputDirectory,
351-
);
352-
} else {
353-
results = ProcessResult(99999, 0, null, null);
354-
}
341+
results = await _processRunner.run(
342+
command.executable,
343+
command.arguments,
344+
runInShell: Platform.isWindows,
345+
workingDirectory: args.outputDirectory,
346+
);
355347

356348
if (results.exitCode != 0) {
357349
return Future.error(
@@ -398,16 +390,12 @@ class OpenapiGenerator extends GeneratorForAnnotation<annots.Openapi> {
398390
);
399391

400392
ProcessResult results;
401-
if (!testMode) {
402-
results = await Process.run(
403-
command.executable,
404-
command.arguments,
405-
runInShell: Platform.isWindows,
406-
workingDirectory: args.outputDirectory,
407-
);
408-
} else {
409-
results = ProcessResult(999999, 0, null, null);
410-
}
393+
results = await _processRunner.run(
394+
command.executable,
395+
command.arguments,
396+
runInShell: Platform.isWindows,
397+
workingDirectory: args.outputDirectory,
398+
);
411399

412400
if (results.exitCode != 0) {
413401
return Future.error(
@@ -482,16 +470,13 @@ class OpenapiGenerator extends GeneratorForAnnotation<annots.Openapi> {
482470
Future<void> formatCode({required GeneratorArguments args}) async {
483471
final command = Command(executable: 'dart', arguments: ['format', './']);
484472
ProcessResult result;
485-
if (!testMode) {
486-
result = await Process.run(
487-
command.executable,
488-
command.arguments,
489-
workingDirectory: args.outputDirectory,
490-
runInShell: Platform.isWindows,
491-
);
492-
} else {
493-
result = ProcessResult(99999, 0, null, null);
494-
}
473+
474+
result = await _processRunner.run(
475+
command.executable,
476+
command.arguments,
477+
workingDirectory: args.outputDirectory,
478+
runInShell: Platform.isWindows,
479+
);
495480

496481
if (result.exitCode != 0) {
497482
return Future.error(
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import 'dart:io';
2+
3+
class ProcessRunner {
4+
Future<ProcessResult> run(String executable, List<String> arguments,
5+
{Map<String, String>? environment,
6+
String? workingDirectory,
7+
bool runInShell = false}) {
8+
return Process.run(executable, arguments,
9+
environment: environment,
10+
workingDirectory: workingDirectory,
11+
runInShell: runInShell);
12+
}
13+
}

openapi-generator/pubspec.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ dependencies:
2020

2121
dev_dependencies:
2222
test: ^1.24.2
23+
mockito: ^5.4.4
24+
test_process:
2325
build_test: '>=1.2.0 <3.0.0'
2426
source_gen_test:
2527
pedantic:

0 commit comments

Comments
 (0)