Skip to content

Commit 6c3cdee

Browse files
agilobKate DöenArtur Powroznikdevjakobsennoordawod
authored
Dart - update generator to support null safety (#10637)
* fix: Make dart2 generated code compilable * Update dart2 client samples * Re-add deleted test files * Lower dart version to 2.12 * Make username and pass not null in http basic auth * Delete json_serializable * Make growable false by default * Make value not nullable * Remove redundant null check * Revert linter fix * Provide required username and pass * Revert initial abstractDartCodeGen changes * Revert removing dart pom module * Revert removing dart pom module * Lower minimum dart version to 2.12 * Disable dart2 tests generation * Disable petstore_client_lib * Disable samples/openapi3/client/petstore/dart2/petstore * Re-add dart2 tests * Add new tests * Delete empty directory * api_client.mustacheUpdate added optional HttpBearerAuth so you can add the token directly on the ApiClient * Update api_client.dart auto generated files for build * Update api_client.dart Autogenerated files for buiild * Make mapDateTime nullable and add ! after json mapping * Fix warning on Future<?> * Fix warning on Future<?> * Dont insert unused param to constructor * Modified Dart2 Mustache template. * Regenerated Petstore source code. * Remove extra code to sync with agilob's pr. * Regenerated Petstore source code. * Fix a couple of reported bugs. * Regenerated Petstore source code. * Make properties non-nullable. * Regenerated Petstore source code. * Do not trim user input before submitting. * Regenerate Petstore source code. * Regenerate Petstore source code. Co-authored-by: Kate Döen <kate@stack11.io> Co-authored-by: Artur Powroznik <arturp@backbase.com> Co-authored-by: devjakobsen <94956607+devjakobsen@users.noreply.github.com> Co-authored-by: Noor Dawod <noor@fine47.com>
1 parent 5f5a83a commit 6c3cdee

File tree

191 files changed

+4606
-4260
lines changed

Some content is hidden

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

191 files changed

+4606
-4260
lines changed

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public class CodegenModel implements IJsonSchemaValidationProperties {
6464
public String defaultValue;
6565
public String arrayModelType;
6666
public boolean isAlias; // Is this effectively an alias of another simple type
67-
public boolean isString, isInteger, isLong, isNumber, isNumeric, isFloat, isDouble, isDate, isDateTime, isShort, isUnboundedInteger, isBoolean;
67+
public boolean isString, isInteger, isLong, isNumber, isNumeric, isFloat, isDouble, isDate, isDateTime, isShort, isUnboundedInteger, isPrimitiveType, isBoolean;
6868
private boolean additionalPropertiesIsAnyType;
6969
public List<CodegenProperty> vars = new ArrayList<>(); // all properties (without parent's properties)
7070
public List<CodegenProperty> allVars = new ArrayList<>(); // all properties (with parent's properties)
@@ -637,6 +637,14 @@ public void setIsUnboundedInteger(boolean isUnboundedInteger) {
637637
this.isUnboundedInteger = isUnboundedInteger;
638638
}
639639

640+
@Override
641+
public boolean getIsPrimitiveType() { return isPrimitiveType; }
642+
643+
@Override
644+
public void setIsPrimitiveType(boolean isPrimitiveType) {
645+
this.isPrimitiveType = isPrimitiveType;
646+
}
647+
640648
@Override
641649
public CodegenProperty getAdditionalProperties() { return additionalProperties; }
642650

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,14 @@ public void setIsUnboundedInteger(boolean isUnboundedInteger) {
623623
this.isUnboundedInteger = isUnboundedInteger;
624624
}
625625

626+
@Override
627+
public boolean getIsPrimitiveType() { return isPrimitiveType; }
628+
629+
@Override
630+
public void setIsPrimitiveType(boolean isPrimitiveType) {
631+
this.isPrimitiveType = isPrimitiveType;
632+
}
633+
626634
@Override
627635
public CodegenProperty getAdditionalProperties() { return additionalProperties; }
628636

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,14 @@ public void setIsUnboundedInteger(boolean isUnboundedInteger) {
536536
this.isUnboundedInteger = isUnboundedInteger;
537537
}
538538

539+
@Override
540+
public boolean getIsPrimitiveType() { return isPrimitiveType; }
541+
542+
@Override
543+
public void setIsPrimitiveType(boolean isPrimitiveType) {
544+
this.isPrimitiveType = isPrimitiveType;
545+
}
546+
539547
public Map<String, Object> getVendorExtensions() {
540548
return vendorExtensions;
541549
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,14 @@ public void setIsUnboundedInteger(boolean isUnboundedInteger) {
371371
this.isUnboundedInteger = isUnboundedInteger;
372372
}
373373

374+
@Override
375+
public boolean getIsPrimitiveType() { return primitiveType; }
376+
377+
@Override
378+
public void setIsPrimitiveType(boolean isPrimitiveType) {
379+
this.primitiveType = isPrimitiveType;
380+
}
381+
374382
@Override
375383
public void setIsModel(boolean isModel) {
376384
this.isModel = isModel;

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,10 @@ public interface IJsonSchemaValidationProperties {
9595

9696
void setIsUnboundedInteger(boolean isUnboundedInteger);
9797

98+
boolean getIsPrimitiveType();
99+
100+
void setIsPrimitiveType(boolean isPrimitiveType);
101+
98102
CodegenProperty getAdditionalProperties();
99103

100104
void setAdditionalProperties(CodegenProperty additionalProperties);
@@ -213,4 +217,4 @@ default void setTypeProperties(Schema p) {
213217
setIsAnyType(true);
214218
}
215219
}
216-
}
220+
}

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

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.openapitools.codegen.languages;
22

3+
import com.google.common.collect.Lists;
4+
import com.google.common.collect.Sets;
35
import io.swagger.v3.oas.models.Operation;
46
import io.swagger.v3.oas.models.media.ArraySchema;
57
import io.swagger.v3.oas.models.media.Schema;
@@ -112,12 +114,13 @@ public AbstractDartCodegen() {
112114
setReservedWordsLowerCase(reservedWordsList);
113115

114116
// These types return isPrimitive=true in templates
115-
languageSpecificPrimitives = new HashSet<>(5);
116-
languageSpecificPrimitives.add("String");
117-
languageSpecificPrimitives.add("bool");
118-
languageSpecificPrimitives.add("int");
119-
languageSpecificPrimitives.add("num");
120-
languageSpecificPrimitives.add("double");
117+
languageSpecificPrimitives = Sets.newHashSet(
118+
"String",
119+
"bool",
120+
"int",
121+
"num",
122+
"double"
123+
);
121124

122125
typeMapping = new HashMap<>();
123126
typeMapping.put("Array", "List");
@@ -148,17 +151,18 @@ public AbstractDartCodegen() {
148151
typeMapping.put("AnyType", "Object");
149152

150153
// Data types of the above values which are automatically imported
151-
defaultIncludes = new HashSet<>();
152-
defaultIncludes.add("String");
153-
defaultIncludes.add("bool");
154-
defaultIncludes.add("int");
155-
defaultIncludes.add("num");
156-
defaultIncludes.add("double");
157-
defaultIncludes.add("List");
158-
defaultIncludes.add("Set");
159-
defaultIncludes.add("Map");
160-
defaultIncludes.add("DateTime");
161-
defaultIncludes.add("Object");
154+
defaultIncludes = Sets.newHashSet(
155+
"String",
156+
"bool",
157+
"int",
158+
"num",
159+
"double",
160+
"List",
161+
"Set",
162+
"Map",
163+
"DateTime",
164+
"Object"
165+
);
162166

163167
imports.put("String", "dart:core");
164168
imports.put("bool", "dart:core");

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public class DartClientCodegen extends AbstractDartCodegen {
3636

3737
public DartClientCodegen() {
3838
super();
39+
3940
final CliOption serializationLibrary = CliOption.newString(CodegenConstants.SERIALIZATION_LIBRARY,
4041
"Specify serialization library");
4142
serializationLibrary.setDefault(SERIALIZATION_LIBRARY_NATIVE);

modules/openapi-generator/src/main/resources/dart2/README.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ For more information, please visit [{{{infoUrl}}}]({{{infoUrl}}})
1919

2020
## Requirements
2121

22-
Dart 2.0 or later
22+
Dart 2.12 or later
2323

2424
## Installation & Usage
2525

modules/openapi-generator/src/main/resources/dart2/api.mustache

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
{{#operations}}
44

55
class {{{classname}}} {
6-
{{{classname}}}([ApiClient apiClient]) : apiClient = apiClient ?? defaultApiClient;
6+
{{{classname}}}([ApiClient? apiClient]) : apiClient = apiClient ?? defaultApiClient;
77

88
final ApiClient apiClient;
99
{{#operation}}
@@ -49,24 +49,13 @@ class {{{classname}}} {
4949
///
5050
{{/-last}}
5151
{{/allParams}}
52-
Future<Response> {{{nickname}}}WithHttpInfo({{#allParams}}{{#required}}{{{dataType}}} {{{paramName}}},{{^-last}} {{/-last}}{{/required}}{{/allParams}}{{#hasOptionalParams}}{ {{#allParams}}{{^required}}{{{dataType}}} {{{paramName}}},{{^-last}} {{/-last}}{{/required}}{{/allParams}} }{{/hasOptionalParams}}) async {
53-
{{#hasParams}}
54-
// Verify required params are set.
55-
{{#allParams}}
56-
{{#required}}
57-
if ({{{paramName}}} == null) {
58-
throw ApiException(HttpStatus.badRequest, 'Missing required param: {{{paramName}}}');
59-
}
60-
{{/required}}
61-
{{/allParams}}
62-
63-
{{/hasParams}}
52+
Future<Response> {{{nickname}}}WithHttpInfo({{#allParams}}{{#required}}{{{dataType}}} {{{paramName}}},{{^-last}} {{/-last}}{{/required}}{{/allParams}}{{#hasOptionalParams}}{ {{#allParams}}{{^required}}{{{dataType}}}? {{{paramName}}},{{^-last}} {{/-last}}{{/required}}{{/allParams}} }{{/hasOptionalParams}}) async {
6453
// ignore: prefer_const_declarations
6554
final path = r'{{{path}}}'{{#pathParams}}
6655
.replaceAll({{=<% %>=}}'{<% baseName %>}'<%={{ }}=%>, {{{paramName}}}{{^isString}}.toString(){{/isString}}){{/pathParams}};
6756

6857
// ignore: prefer_final_locals
69-
Object postBody{{#bodyParam}} = {{{paramName}}}{{/bodyParam}};
58+
Object? postBody{{#bodyParam}} = {{{paramName}}}{{/bodyParam}};
7059

7160
final queryParams = <QueryParam>[];
7261
final headerParams = <String, String>{};
@@ -77,7 +66,7 @@ class {{{classname}}} {
7766
{{^required}}
7867
if ({{{paramName}}} != null) {
7968
{{/required}}
80-
queryParams.addAll(_convertParametersForCollectionFormat('{{{collectionFormat}}}', '{{{baseName}}}', {{{paramName}}}));
69+
queryParams.addAll(_queryParams('{{{collectionFormat}}}', '{{{baseName}}}', {{{paramName}}}));
8170
{{^required}}
8271
}
8372
{{/required}}
@@ -139,7 +128,7 @@ class {{{classname}}} {
139128
postBody,
140129
headerParams,
141130
formParams,
142-
contentTypes.isEmpty ? null : contentTypes[0],
131+
contentTypes.isEmpty ? null : contentTypes.first,
143132
authNames,
144133
);
145134
}
@@ -174,7 +163,7 @@ class {{{classname}}} {
174163
///
175164
{{/-last}}
176165
{{/allParams}}
177-
Future<{{{returnType}}}{{^returnType}}void{{/returnType}}> {{{nickname}}}({{#allParams}}{{#required}}{{{dataType}}} {{{paramName}}},{{^-last}} {{/-last}}{{/required}}{{/allParams}}{{#hasOptionalParams}}{ {{#allParams}}{{^required}}{{{dataType}}} {{{paramName}}},{{^-last}} {{/-last}}{{/required}}{{/allParams}} }{{/hasOptionalParams}}) async {
166+
Future<{{#returnType}}{{{.}}}?{{/returnType}}{{^returnType}}void{{/returnType}}> {{{nickname}}}({{#allParams}}{{#required}}{{{dataType}}} {{{paramName}}},{{^-last}} {{/-last}}{{/required}}{{/allParams}}{{#hasOptionalParams}}{ {{#allParams}}{{^required}}{{{dataType}}}? {{{paramName}}},{{^-last}} {{/-last}}{{/required}}{{/allParams}} }{{/hasOptionalParams}}) async {
178167
final response = await {{{nickname}}}WithHttpInfo({{#allParams}}{{#required}}{{{paramName}}},{{^-last}} {{/-last}}{{/required}}{{/allParams}}{{#hasOptionalParams}} {{#allParams}}{{^required}}{{{paramName}}}: {{{paramName}}},{{^-last}} {{/-last}}{{/required}}{{/allParams}} {{/hasOptionalParams}});
179168
if (response.statusCode >= HttpStatus.badRequest) {
180169
throw ApiException(response.statusCode, await _decodeBodyBytes(response));
@@ -183,13 +172,13 @@ class {{{classname}}} {
183172
// When a remote server returns no body with a status of 204, we shall not decode it.
184173
// At the time of writing this, `dart:convert` will throw an "Unexpected end of input"
185174
// FormatException when trying to decode an empty string.
186-
if (response.body != null && response.statusCode != HttpStatus.noContent) {
175+
if (response.body.isNotEmpty && response.statusCode != HttpStatus.noContent) {
187176
{{#native_serialization}}
188177
{{#isArray}}
189178
final responseBody = await _decodeBodyBytes(response);
190179
return (await apiClient.deserializeAsync(responseBody, '{{{returnType}}}') as List)
191180
.cast<{{{returnBaseType}}}>()
192-
.{{#uniqueItems}}toSet(){{/uniqueItems}}{{^uniqueItems}}toList(growable: false){{/uniqueItems}};
181+
.{{#uniqueItems}}toSet(){{/uniqueItems}}{{^uniqueItems}}toList(){{/uniqueItems}};
193182
{{/isArray}}
194183
{{^isArray}}
195184
{{#isMap}}
@@ -199,7 +188,7 @@ class {{{classname}}} {
199188
return await apiClient.deserializeAsync(await _decodeBodyBytes(response), '{{{returnType}}}',) as {{{returnType}}};
200189
{{/isMap}}{{/isArray}}{{/native_serialization}}
201190
}
202-
return Future<{{{returnType}}}>.value();
191+
return null;
203192
{{/returnType}}
204193
}
205194
{{/operation}}

0 commit comments

Comments
 (0)