Skip to content

Commit 3d038b7

Browse files
authored
[dart][dart-dio] Next-gen dart-dio generator (#8869)
* [dart-dio] Remove old Dart 1.x templates * [dart-dio] Initial version of a next-gen Dart Dio generator * Fix a couple readme links and examples * Fix import in testcase * Add integration tests to master POM * Run initial dart format and improve some base formatting * Generate docs * Use integer matchers in test for content-length * Separate more built_value specific files * Fix mustache partial path * Update pubspec template * Fix tests after mock lib updates * Generate with new built_value pre-release This solves the problem of not being able to serialize null. * Update built_value dependency * Regenerate after merge * Regenerate tests * Fix missing byte response cast * Update dio to 4.0.0-prev1 * Run format * Ignore unused imports in inheritance classes * Update mock test library * Update docs * Make a couple variables final * Update and freeze dio dependency * Update generator doc * Add a new option to post processes all dart output at once This is much faster than individual files. The committed samples should be formatted since Dart is very opinionated and it makes diffs in PRs much easier to read. Due to this, we also need to format in CI, otherwise there is a git diff. * Test some CI stuff
1 parent 88fa5d7 commit 3d038b7

280 files changed

Lines changed: 15378 additions & 1155 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.circleci/config.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ jobs:
8282
- ~/.bundle
8383
- ~/.go_workspace
8484
- ~/.gradle
85+
- ~/.pub-cache
8586
- ~/.cache/bower
8687
- ".git"
8788
- ~/.stack

CI/circle_parallel.sh

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,18 @@ function cleanup {
1414

1515
trap cleanup EXIT
1616

17+
function installDart {
18+
# install dart2
19+
sudo apt-get update
20+
sudo apt-get install apt-transport-https
21+
sudo sh -c 'wget -qO- https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add -'
22+
sudo sh -c 'wget -qO- https://storage.googleapis.com/download.dartlang.org/linux/debian/dart_stable.list > /etc/apt/sources.list.d/dart_stable.list'
23+
sudo apt-get update
24+
sudo apt-get install dart
25+
export PATH="$PATH:/usr/lib/dart/bin"
26+
export DART_POST_PROCESS="dart format"
27+
}
28+
1729
if [ "$NODE_INDEX" = "1" ]; then
1830
echo "Running node $NODE_INDEX to test 'samples.circleci' defined in pom.xml ..."
1931
java -version
@@ -24,6 +36,8 @@ if [ "$NODE_INDEX" = "1" ]; then
2436
ls -l /home/circleci/.ivy2/cache
2537

2638
elif [ "$NODE_INDEX" = "2" ]; then
39+
installDart
40+
2741
# run ensure-up-to-date sample script on SNAPSHOT version only
2842
project_version=`mvn org.apache.maven.plugins:maven-help-plugin:3.1.0:evaluate -Dexpression=project.version -q -DforceStdout`
2943
if [[ $project_version == *"-SNAPSHOT" ]]; then
@@ -67,14 +81,7 @@ else
6781
export PATH="/usr/local/go1.14/go/bin:$PATH"
6882
go version
6983

70-
# install dart2
71-
sudo apt-get update
72-
sudo apt-get install apt-transport-https
73-
sudo sh -c 'wget -qO- https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add -'
74-
sudo sh -c 'wget -qO- https://storage.googleapis.com/download.dartlang.org/linux/debian/dart_stable.list > /etc/apt/sources.list.d/dart_stable.list'
75-
sudo apt-get update
76-
sudo apt-get install dart
77-
export PATH="$PATH:/usr/lib/dart/bin"
84+
installDart
7885

7986
mvn --no-snapshot-updates --quiet verify -Psamples.circleci.others -Dorg.slf4j.simpleLogger.defaultLogLevel=error
8087
mvn --no-snapshot-updates --quiet javadoc:javadoc -Psamples.circleci -Dorg.slf4j.simpleLogger.defaultLogLevel=error
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
generatorName: dart-dio-next
2+
outputDir: samples/openapi3/client/petstore/dart-dio-next/petstore_client_lib_fake
3+
inputSpec: modules/openapi-generator/src/test/resources/3_0/petstore-with-fake-endpoints-models-for-testing.yaml
4+
templateDir: modules/openapi-generator/src/main/resources/dart/libraries/dio
5+
enablePostProcessFile: "true"
6+
additionalProperties:
7+
hideGenerationTimestamp: "true"

docs/file-post-processing.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ The following environment variables are supported by their respective generators
2222
* `CPP_POST_PROCESS_FILE`
2323
* `CSHARP_POST_PROCESS_FILE`
2424
* `C_POST_PROCESS_FILE`
25+
* `DART_POST_PROCESS`
2526
* `DART_POST_PROCESS_FILE`
2627
* `FSHARP_POST_PROCESS_FILE`
2728
* `GO_POST_PROCESS_FILE`

docs/generators.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ The following generators are available:
2222
* [csharp-netcore](generators/csharp-netcore.md)
2323
* [dart](generators/dart.md)
2424
* [dart-dio](generators/dart-dio.md)
25+
* [dart-dio-next (experimental)](generators/dart-dio-next.md)
2526
* [dart-jaguar](generators/dart-jaguar.md)
2627
* [eiffel](generators/eiffel.md)
2728
* [elixir](generators/elixir.md)

docs/generators/dart-dio-next.md

Lines changed: 233 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,233 @@
1+
---
2+
title: Config Options for dart-dio-next
3+
sidebar_label: dart-dio-next
4+
---
5+
6+
These options may be applied as additional-properties (cli) or configOptions (plugins). Refer to [configuration docs](https://openapi-generator.tech/docs/configuration) for more details.
7+
8+
| Option | Description | Values | Default |
9+
| ------ | ----------- | ------ | ------- |
10+
|allowUnicodeIdentifiers|boolean, toggles whether unicode identifiers are allowed in names or not, default is false| |false|
11+
|dateLibrary|Specify Date library|<dl><dt>**core**</dt><dd>[DEFAULT] Dart core library (DateTime)</dd><dt>**timemachine**</dt><dd>Time Machine is date and time library for Flutter, Web, and Server with support for timezones, calendars, cultures, formatting and parsing.</dd></dl>|core|
12+
|disallowAdditionalPropertiesIfNotPresent|If false, the 'additionalProperties' implementation (set to true by default) is compliant with the OAS and JSON schema specifications. If true (default), keep the old (incorrect) behaviour that 'additionalProperties' is set to false by default.|<dl><dt>**false**</dt><dd>The 'additionalProperties' implementation is compliant with the OAS and JSON schema specifications.</dd><dt>**true**</dt><dd>Keep the old (incorrect) behaviour that 'additionalProperties' is set to false by default.</dd></dl>|true|
13+
|ensureUniqueParams|Whether to ensure parameter names are unique in an operation (rename parameters that are not).| |true|
14+
|legacyDiscriminatorBehavior|Set to false for generators with better support for discriminators. (Python, Java, Go, PowerShell, C#have this enabled by default).|<dl><dt>**true**</dt><dd>The mapping in the discriminator includes descendent schemas that allOf inherit from self and the discriminator mapping schemas in the OAS document.</dd><dt>**false**</dt><dd>The mapping in the discriminator includes any descendent schemas that allOf inherit from self, any oneOf schemas, any anyOf schemas, any x-discriminator-values, and the discriminator mapping schemas in the OAS document AND Codegen validates that oneOf and anyOf schemas contain the required discriminator and throws an error if the discriminator is missing.</dd></dl>|true|
15+
|prependFormOrBodyParameters|Add form or body parameters to the beginning of the parameter list.| |false|
16+
|pubAuthor|Author name in generated pubspec| |null|
17+
|pubAuthorEmail|Email address of the author in generated pubspec| |null|
18+
|pubDescription|Description in generated pubspec| |null|
19+
|pubHomepage|Homepage in generated pubspec| |null|
20+
|pubLibrary|Library name in generated code| |null|
21+
|pubName|Name in generated pubspec| |null|
22+
|pubVersion|Version in generated pubspec| |null|
23+
|serializationLibrary|Specify serialization library|<dl><dt>**built_value**</dt><dd>[DEFAULT] built_value</dd></dl>|built_value|
24+
|sortModelPropertiesByRequiredFlag|Sort model properties to place required parameters before optional parameters.| |true|
25+
|sortParamsByRequiredFlag|Sort method arguments to place required parameters before optional parameters.| |true|
26+
|sourceFolder|Source folder for generated code| |null|
27+
|useEnumExtension|Allow the 'x-enum-values' extension for enums| |null|
28+
29+
## IMPORT MAPPING
30+
31+
| Type/Alias | Imports |
32+
| ---------- | ------- |
33+
34+
35+
## INSTANTIATION TYPES
36+
37+
| Type/Alias | Instantiated By |
38+
| ---------- | --------------- |
39+
40+
41+
## LANGUAGE PRIMITIVES
42+
43+
<ul class="column-ul">
44+
<li>String</li>
45+
<li>bool</li>
46+
<li>double</li>
47+
<li>dynamic</li>
48+
<li>int</li>
49+
<li>num</li>
50+
</ul>
51+
52+
## RESERVED WORDS
53+
54+
<ul class="column-ul">
55+
<li>abstract</li>
56+
<li>as</li>
57+
<li>assert</li>
58+
<li>async</li>
59+
<li>await</li>
60+
<li>break</li>
61+
<li>case</li>
62+
<li>catch</li>
63+
<li>class</li>
64+
<li>const</li>
65+
<li>continue</li>
66+
<li>covariant</li>
67+
<li>default</li>
68+
<li>deferred</li>
69+
<li>do</li>
70+
<li>dynamic</li>
71+
<li>else</li>
72+
<li>enum</li>
73+
<li>export</li>
74+
<li>extends</li>
75+
<li>extension</li>
76+
<li>external</li>
77+
<li>factory</li>
78+
<li>false</li>
79+
<li>final</li>
80+
<li>finally</li>
81+
<li>for</li>
82+
<li>function</li>
83+
<li>get</li>
84+
<li>hide</li>
85+
<li>if</li>
86+
<li>implements</li>
87+
<li>import</li>
88+
<li>in</li>
89+
<li>inout</li>
90+
<li>interface</li>
91+
<li>is</li>
92+
<li>late</li>
93+
<li>library</li>
94+
<li>mixin</li>
95+
<li>native</li>
96+
<li>new</li>
97+
<li>null</li>
98+
<li>of</li>
99+
<li>on</li>
100+
<li>operator</li>
101+
<li>out</li>
102+
<li>part</li>
103+
<li>patch</li>
104+
<li>required</li>
105+
<li>rethrow</li>
106+
<li>return</li>
107+
<li>set</li>
108+
<li>show</li>
109+
<li>source</li>
110+
<li>static</li>
111+
<li>super</li>
112+
<li>switch</li>
113+
<li>sync</li>
114+
<li>this</li>
115+
<li>throw</li>
116+
<li>true</li>
117+
<li>try</li>
118+
<li>typedef</li>
119+
<li>var</li>
120+
<li>void</li>
121+
<li>while</li>
122+
<li>with</li>
123+
<li>yield</li>
124+
</ul>
125+
126+
## FEATURE SET
127+
128+
129+
### Client Modification Feature
130+
| Name | Supported | Defined By |
131+
| ---- | --------- | ---------- |
132+
|BasePath|✗|ToolingExtension
133+
|Authorizations|✗|ToolingExtension
134+
|UserAgent|✗|ToolingExtension
135+
|MockServer|✗|ToolingExtension
136+
137+
### Data Type Feature
138+
| Name | Supported | Defined By |
139+
| ---- | --------- | ---------- |
140+
|Custom|✗|OAS2,OAS3
141+
|Int32|✗|OAS2,OAS3
142+
|Int64|✗|OAS2,OAS3
143+
|Float|✗|OAS2,OAS3
144+
|Double|✗|OAS2,OAS3
145+
|Decimal|✗|ToolingExtension
146+
|String|✗|OAS2,OAS3
147+
|Byte|✗|OAS2,OAS3
148+
|Binary|✗|OAS2,OAS3
149+
|Boolean|✗|OAS2,OAS3
150+
|Date|✗|OAS2,OAS3
151+
|DateTime|✗|OAS2,OAS3
152+
|Password|✗|OAS2,OAS3
153+
|File|✗|OAS2
154+
|Array|✗|OAS2,OAS3
155+
|Maps|✗|ToolingExtension
156+
|CollectionFormat|✗|OAS2
157+
|CollectionFormatMulti|✗|OAS2
158+
|Enum|✗|OAS2,OAS3
159+
|ArrayOfEnum|✗|ToolingExtension
160+
|ArrayOfModel|✗|ToolingExtension
161+
|ArrayOfCollectionOfPrimitives|✗|ToolingExtension
162+
|ArrayOfCollectionOfModel|✗|ToolingExtension
163+
|ArrayOfCollectionOfEnum|✗|ToolingExtension
164+
|MapOfEnum|✗|ToolingExtension
165+
|MapOfModel|✗|ToolingExtension
166+
|MapOfCollectionOfPrimitives|✗|ToolingExtension
167+
|MapOfCollectionOfModel|✗|ToolingExtension
168+
|MapOfCollectionOfEnum|✗|ToolingExtension
169+
170+
### Documentation Feature
171+
| Name | Supported | Defined By |
172+
| ---- | --------- | ---------- |
173+
|Readme|✗|ToolingExtension
174+
|Model|✗|ToolingExtension
175+
|Api|✗|ToolingExtension
176+
177+
### Global Feature
178+
| Name | Supported | Defined By |
179+
| ---- | --------- | ---------- |
180+
|Host|✗|OAS2,OAS3
181+
|BasePath|✗|OAS2,OAS3
182+
|Info|✗|OAS2,OAS3
183+
|Schemes|✗|OAS2,OAS3
184+
|PartialSchemes|✗|OAS2,OAS3
185+
|Consumes|✗|OAS2
186+
|Produces|✗|OAS2
187+
|ExternalDocumentation|✗|OAS2,OAS3
188+
|Examples|✗|OAS2,OAS3
189+
|XMLStructureDefinitions|✗|OAS2,OAS3
190+
|MultiServer|✗|OAS3
191+
|ParameterizedServer|✗|OAS3
192+
|ParameterStyling|✗|OAS3
193+
|Callbacks|✗|OAS3
194+
|LinkObjects|✗|OAS3
195+
196+
### Parameter Feature
197+
| Name | Supported | Defined By |
198+
| ---- | --------- | ---------- |
199+
|Path|✗|OAS2,OAS3
200+
|Query|✗|OAS2,OAS3
201+
|Header|✗|OAS2,OAS3
202+
|Body|✗|OAS2
203+
|FormUnencoded|✗|OAS2
204+
|FormMultipart|✗|OAS2
205+
|Cookie|✗|OAS3
206+
207+
### Schema Support Feature
208+
| Name | Supported | Defined By |
209+
| ---- | --------- | ---------- |
210+
|Simple|✗|OAS2,OAS3
211+
|Composite|✗|OAS2,OAS3
212+
|Polymorphism|✗|OAS2,OAS3
213+
|Union|✗|OAS3
214+
215+
### Security Feature
216+
| Name | Supported | Defined By |
217+
| ---- | --------- | ---------- |
218+
|BasicAuth|✗|OAS2,OAS3
219+
|ApiKey|✗|OAS2,OAS3
220+
|OpenIDConnect|✗|OAS3
221+
|BearerToken|✗|OAS3
222+
|OAuth2_Implicit|✗|OAS2,OAS3
223+
|OAuth2_Password|✗|OAS2,OAS3
224+
|OAuth2_ClientCredentials|✗|OAS2,OAS3
225+
|OAuth2_AuthorizationCode|✗|OAS2,OAS3
226+
227+
### Wire Format Feature
228+
| Name | Supported | Defined By |
229+
| ---- | --------- | ---------- |
230+
|JSON|✗|OAS2,OAS3
231+
|XML|✗|OAS2,OAS3
232+
|PROTOBUF|✗|ToolingExtension
233+
|Custom|✗|OAS2,OAS3

modules/openapi-generator/src/main/java/org/openapitools/codegen/CodegenOperation.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,15 @@ public boolean getHasFormParams() {
114114
return nonempty(formParams);
115115
}
116116

117+
/**
118+
* Check if there's at least one body parameter or at least one form parameter
119+
*
120+
* @return true if body or form parameter exists, false otherwise
121+
*/
122+
public boolean getHasBodyOrFormParams() {
123+
return getHasBodyParam() || getHasFormParams();
124+
}
125+
117126
/**
118127
* Check if there's at least one form parameter
119128
*

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractDartCodegen.java

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,16 @@
1212
import org.openapitools.codegen.*;
1313
import org.openapitools.codegen.meta.features.*;
1414
import org.openapitools.codegen.utils.ModelUtils;
15+
import org.slf4j.Logger;
16+
import org.slf4j.LoggerFactory;
1517

1618
import java.io.BufferedReader;
1719
import java.io.File;
1820
import java.io.InputStreamReader;
1921
import java.nio.charset.StandardCharsets;
2022
import java.util.*;
21-
import org.slf4j.Logger;
22-
import org.slf4j.LoggerFactory;
2323

2424
import static org.openapitools.codegen.utils.StringUtils.*;
25-
import static org.openapitools.codegen.utils.StringUtils.camelize;
2625

2726
public abstract class AbstractDartCodegen extends DefaultCodegen {
2827

@@ -663,6 +662,7 @@ public String escapeUnsafeCharacters(String input) {
663662

664663
@Override
665664
public void postProcessFile(File file, String fileType) {
665+
super.postProcessFile(file, fileType);
666666
if (file == null) {
667667
return;
668668
}
@@ -674,7 +674,7 @@ public void postProcessFile(File file, String fileType) {
674674

675675
// process all files with dart extension
676676
if ("dart".equals(FilenameUtils.getExtension(file.toString()))) {
677-
// currently only support "dartfmt -w yourcode.dart"
677+
// currently supported is "dartfmt -w" and "dart format"
678678
String command = dartPostProcessFile + " " + file.toString();
679679
try {
680680
Process p = Runtime.getRuntime().exec(command);
@@ -689,4 +689,31 @@ public void postProcessFile(File file, String fileType) {
689689
}
690690
}
691691
}
692+
693+
@Override
694+
public void postProcess() {
695+
if (isEnablePostProcessFile()) {
696+
// Using the condition here to have way to still disable this
697+
// for older Dart generators in CI by default.
698+
699+
// Post processing the whole dart output is much faster then individual files.
700+
// Setting this variable to "dart format" is the suggested way of doing this.
701+
final String dartPostProcess = System.getenv("DART_POST_PROCESS");
702+
if (!StringUtils.isEmpty(dartPostProcess)) {
703+
final String command = dartPostProcess + " " + getOutputDir();
704+
try {
705+
Process p = Runtime.getRuntime().exec(command);
706+
int exitValue = p.waitFor();
707+
if (exitValue != 0) {
708+
LOGGER.error("Error running the command ({}). Exit code: {}", command, exitValue);
709+
} else {
710+
LOGGER.info("Successfully executed: {}", command);
711+
}
712+
} catch (Exception e) {
713+
LOGGER.error("Error running the command ({}). Exception: {}", command, e.getMessage());
714+
}
715+
}
716+
}
717+
super.postProcess();
718+
}
692719
}

0 commit comments

Comments
 (0)