Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dart - update generator to support null safety #10637

Merged
merged 52 commits into from
Jan 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
a1d65f4
fix: Make dart2 generated code compilable
Oct 6, 2021
7444a10
Update dart2 client samples
Oct 6, 2021
27887c6
Merge branch 'master' of github.com:OpenAPITools/openapi-generator in…
agilob Oct 12, 2021
14d3206
Re-add deleted test files
agilob Oct 12, 2021
8387b25
Lower dart version to 2.12
agilob Oct 12, 2021
f449f96
Make username and pass not null in http basic auth
agilob Oct 12, 2021
1326cff
Merge branch 'master' of github.com:OpenAPITools/openapi-generator in…
agilob Oct 19, 2021
932964b
Delete json_serializable
agilob Oct 19, 2021
a089fc4
Make growable false by default
agilob Oct 19, 2021
0b255fb
Make value not nullable
agilob Oct 19, 2021
86f8fbf
Remove redundant null check
agilob Oct 19, 2021
54c3b48
Revert linter fix
agilob Oct 19, 2021
3b375b5
Provide required username and pass
agilob Oct 19, 2021
81d5891
Revert initial abstractDartCodeGen changes
agilob Oct 19, 2021
28a9ac2
Revert removing dart pom module
agilob Oct 19, 2021
746085c
Revert removing dart pom module
agilob Oct 19, 2021
c97483b
Lower minimum dart version to 2.12
agilob Oct 19, 2021
b0d96ef
Disable dart2 tests generation
agilob Oct 27, 2021
bd6a0b7
Merge branch 'master' of github.com:OpenAPITools/openapi-generator in…
agilob Oct 27, 2021
9fccea2
Disable petstore_client_lib
agilob Oct 27, 2021
c2f341c
Disable samples/openapi3/client/petstore/dart2/petstore
agilob Oct 27, 2021
981157c
Re-add dart2 tests
agilob Oct 27, 2021
251faea
Add new tests
agilob Oct 27, 2021
90a937a
Delete empty directory
agilob Oct 27, 2021
9d689fb
Merge branch '6.0.x' of github.com:OpenAPITools/openapi-generator int…
agilob Oct 27, 2021
22c689f
Merge branch '6.0.x' of github.com:OpenAPITools/openapi-generator int…
Nov 2, 2021
77d25bb
api_client.mustacheUpdate
devjakobsen Nov 24, 2021
3a32039
Merge pull request #17 from devjakobsen/patch-1
agilob Nov 25, 2021
6aa6c0e
Merge branch '6.0.x' into dart-2.12
agilob Nov 25, 2021
df67b74
Update api_client.dart
devjakobsen Nov 26, 2021
c59fdb5
Update api_client.dart
devjakobsen Nov 26, 2021
65df747
Merge pull request #18 from devjakobsen/patch-3
agilob Nov 26, 2021
c8d6d85
Merge pull request #19 from devjakobsen/patch-2
agilob Nov 26, 2021
9518ee7
Make mapDateTime nullable and add ! after json mapping
agilob Dec 9, 2021
fdd1442
Fix warning on Future<?>
agilob Dec 9, 2021
1863a04
Fix warning on Future<?>
agilob Dec 9, 2021
3050869
Dont insert unused param to constructor
agilob Dec 9, 2021
c7258e3
Modified Dart2 Mustache template.
noordawod Dec 18, 2021
17b6c76
Regenerated Petstore source code.
noordawod Dec 18, 2021
9f1e6a7
Remove extra code to sync with agilob's pr.
noordawod Dec 18, 2021
ebd0fae
Regenerated Petstore source code.
noordawod Dec 18, 2021
eeb5ede
Merge pull request #23 from noordawod/techdebt/agilob-dart2-null-safety
agilob Dec 19, 2021
ceff059
Merge branch '6.0.x' of github.com:OpenAPITools/openapi-generator int…
agilob Dec 19, 2021
e6219c7
Fix a couple of reported bugs.
noordawod Dec 19, 2021
a989e61
Regenerated Petstore source code.
noordawod Dec 19, 2021
07d2f91
Make properties non-nullable.
noordawod Dec 20, 2021
4adb204
Regenerated Petstore source code.
noordawod Dec 20, 2021
5493205
Do not trim user input before submitting.
noordawod Dec 20, 2021
d647733
Regenerate Petstore source code.
noordawod Dec 20, 2021
2683665
Regenerate Petstore source code.
noordawod Dec 20, 2021
7252264
Merge branch '6.0.x' of github.com:OpenAPITools/openapi-generator int…
agilob Jan 2, 2022
a59eda0
Merge branch 'dart-2.12' of github.com:agilob/openapi-generator into …
agilob Jan 2, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public class CodegenModel implements IJsonSchemaValidationProperties {
public String defaultValue;
public String arrayModelType;
public boolean isAlias; // Is this effectively an alias of another simple type
public boolean isString, isInteger, isLong, isNumber, isNumeric, isFloat, isDouble, isDate, isDateTime, isShort, isUnboundedInteger, isBoolean;
public boolean isString, isInteger, isLong, isNumber, isNumeric, isFloat, isDouble, isDate, isDateTime, isShort, isUnboundedInteger, isPrimitiveType, isBoolean;
agilob marked this conversation as resolved.
Show resolved Hide resolved
private boolean additionalPropertiesIsAnyType;
public List<CodegenProperty> vars = new ArrayList<>(); // all properties (without parent's properties)
public List<CodegenProperty> allVars = new ArrayList<>(); // all properties (with parent's properties)
Expand Down Expand Up @@ -637,6 +637,14 @@ public void setIsUnboundedInteger(boolean isUnboundedInteger) {
this.isUnboundedInteger = isUnboundedInteger;
}

@Override
public boolean getIsPrimitiveType() { return isPrimitiveType; }

@Override
public void setIsPrimitiveType(boolean isPrimitiveType) {
this.isPrimitiveType = isPrimitiveType;
}

@Override
public CodegenProperty getAdditionalProperties() { return additionalProperties; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,14 @@ public void setIsUnboundedInteger(boolean isUnboundedInteger) {
this.isUnboundedInteger = isUnboundedInteger;
}

@Override
public boolean getIsPrimitiveType() { return isPrimitiveType; }

@Override
public void setIsPrimitiveType(boolean isPrimitiveType) {
this.isPrimitiveType = isPrimitiveType;
}

@Override
public CodegenProperty getAdditionalProperties() { return additionalProperties; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,14 @@ public void setIsUnboundedInteger(boolean isUnboundedInteger) {
this.isUnboundedInteger = isUnboundedInteger;
}

@Override
public boolean getIsPrimitiveType() { return isPrimitiveType; }

@Override
public void setIsPrimitiveType(boolean isPrimitiveType) {
this.isPrimitiveType = isPrimitiveType;
}

public Map<String, Object> getVendorExtensions() {
return vendorExtensions;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,14 @@ public void setIsUnboundedInteger(boolean isUnboundedInteger) {
this.isUnboundedInteger = isUnboundedInteger;
}

@Override
public boolean getIsPrimitiveType() { return primitiveType; }

@Override
public void setIsPrimitiveType(boolean isPrimitiveType) {
this.primitiveType = isPrimitiveType;
}

@Override
public void setIsModel(boolean isModel) {
this.isModel = isModel;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ public interface IJsonSchemaValidationProperties {

void setIsUnboundedInteger(boolean isUnboundedInteger);

boolean getIsPrimitiveType();

void setIsPrimitiveType(boolean isPrimitiveType);

CodegenProperty getAdditionalProperties();

void setAdditionalProperties(CodegenProperty additionalProperties);
Expand Down Expand Up @@ -213,4 +217,4 @@ default void setTypeProperties(Schema p) {
setIsAnyType(true);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.openapitools.codegen.languages;

import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import io.swagger.v3.oas.models.Operation;
import io.swagger.v3.oas.models.media.ArraySchema;
import io.swagger.v3.oas.models.media.Schema;
Expand Down Expand Up @@ -112,12 +114,13 @@ public AbstractDartCodegen() {
setReservedWordsLowerCase(reservedWordsList);

// These types return isPrimitive=true in templates
languageSpecificPrimitives = new HashSet<>(5);
languageSpecificPrimitives.add("String");
languageSpecificPrimitives.add("bool");
languageSpecificPrimitives.add("int");
languageSpecificPrimitives.add("num");
languageSpecificPrimitives.add("double");
languageSpecificPrimitives = Sets.newHashSet(
"String",
"bool",
"int",
"num",
"double"
);

typeMapping = new HashMap<>();
typeMapping.put("Array", "List");
Expand Down Expand Up @@ -148,17 +151,18 @@ public AbstractDartCodegen() {
typeMapping.put("AnyType", "Object");

// Data types of the above values which are automatically imported
defaultIncludes = new HashSet<>();
defaultIncludes.add("String");
defaultIncludes.add("bool");
defaultIncludes.add("int");
defaultIncludes.add("num");
defaultIncludes.add("double");
defaultIncludes.add("List");
defaultIncludes.add("Set");
defaultIncludes.add("Map");
defaultIncludes.add("DateTime");
defaultIncludes.add("Object");
defaultIncludes = Sets.newHashSet(
"String",
"bool",
"int",
"num",
"double",
"List",
"Set",
"Map",
"DateTime",
"Object"
);

imports.put("String", "dart:core");
imports.put("bool", "dart:core");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public class DartClientCodegen extends AbstractDartCodegen {

public DartClientCodegen() {
super();

final CliOption serializationLibrary = CliOption.newString(CodegenConstants.SERIALIZATION_LIBRARY,
"Specify serialization library");
serializationLibrary.setDefault(SERIALIZATION_LIBRARY_NATIVE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ For more information, please visit [{{{infoUrl}}}]({{{infoUrl}}})

## Requirements

Dart 2.0 or later
Dart 2.12 or later

## Installation & Usage

Expand Down
29 changes: 9 additions & 20 deletions modules/openapi-generator/src/main/resources/dart2/api.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{{#operations}}

class {{{classname}}} {
{{{classname}}}([ApiClient apiClient]) : apiClient = apiClient ?? defaultApiClient;
{{{classname}}}([ApiClient? apiClient]) : apiClient = apiClient ?? defaultApiClient;

final ApiClient apiClient;
{{#operation}}
Expand Down Expand Up @@ -49,24 +49,13 @@ class {{{classname}}} {
///
{{/-last}}
{{/allParams}}
Future<Response> {{{nickname}}}WithHttpInfo({{#allParams}}{{#required}}{{{dataType}}} {{{paramName}}},{{^-last}} {{/-last}}{{/required}}{{/allParams}}{{#hasOptionalParams}}{ {{#allParams}}{{^required}}{{{dataType}}} {{{paramName}}},{{^-last}} {{/-last}}{{/required}}{{/allParams}} }{{/hasOptionalParams}}) async {
{{#hasParams}}
// Verify required params are set.
{{#allParams}}
{{#required}}
if ({{{paramName}}} == null) {
throw ApiException(HttpStatus.badRequest, 'Missing required param: {{{paramName}}}');
}
{{/required}}
{{/allParams}}

{{/hasParams}}
Future<Response> {{{nickname}}}WithHttpInfo({{#allParams}}{{#required}}{{{dataType}}} {{{paramName}}},{{^-last}} {{/-last}}{{/required}}{{/allParams}}{{#hasOptionalParams}}{ {{#allParams}}{{^required}}{{{dataType}}}? {{{paramName}}},{{^-last}} {{/-last}}{{/required}}{{/allParams}} }{{/hasOptionalParams}}) async {
// ignore: prefer_const_declarations
final path = r'{{{path}}}'{{#pathParams}}
.replaceAll({{=<% %>=}}'{<% baseName %>}'<%={{ }}=%>, {{{paramName}}}{{^isString}}.toString(){{/isString}}){{/pathParams}};

// ignore: prefer_final_locals
Object postBody{{#bodyParam}} = {{{paramName}}}{{/bodyParam}};
Object? postBody{{#bodyParam}} = {{{paramName}}}{{/bodyParam}};

final queryParams = <QueryParam>[];
final headerParams = <String, String>{};
Expand All @@ -77,7 +66,7 @@ class {{{classname}}} {
{{^required}}
if ({{{paramName}}} != null) {
{{/required}}
queryParams.addAll(_convertParametersForCollectionFormat('{{{collectionFormat}}}', '{{{baseName}}}', {{{paramName}}}));
queryParams.addAll(_queryParams('{{{collectionFormat}}}', '{{{baseName}}}', {{{paramName}}}));
{{^required}}
}
{{/required}}
Expand Down Expand Up @@ -139,7 +128,7 @@ class {{{classname}}} {
postBody,
headerParams,
formParams,
contentTypes.isEmpty ? null : contentTypes[0],
contentTypes.isEmpty ? null : contentTypes.first,
authNames,
);
}
Expand Down Expand Up @@ -174,7 +163,7 @@ class {{{classname}}} {
///
{{/-last}}
{{/allParams}}
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 {
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 {
final response = await {{{nickname}}}WithHttpInfo({{#allParams}}{{#required}}{{{paramName}}},{{^-last}} {{/-last}}{{/required}}{{/allParams}}{{#hasOptionalParams}} {{#allParams}}{{^required}}{{{paramName}}}: {{{paramName}}},{{^-last}} {{/-last}}{{/required}}{{/allParams}} {{/hasOptionalParams}});
if (response.statusCode >= HttpStatus.badRequest) {
throw ApiException(response.statusCode, await _decodeBodyBytes(response));
Expand All @@ -183,13 +172,13 @@ class {{{classname}}} {
// When a remote server returns no body with a status of 204, we shall not decode it.
// At the time of writing this, `dart:convert` will throw an "Unexpected end of input"
// FormatException when trying to decode an empty string.
if (response.body != null && response.statusCode != HttpStatus.noContent) {
if (response.body.isNotEmpty && response.statusCode != HttpStatus.noContent) {
{{#native_serialization}}
{{#isArray}}
final responseBody = await _decodeBodyBytes(response);
return (await apiClient.deserializeAsync(responseBody, '{{{returnType}}}') as List)
.cast<{{{returnBaseType}}}>()
.{{#uniqueItems}}toSet(){{/uniqueItems}}{{^uniqueItems}}toList(growable: false){{/uniqueItems}};
.{{#uniqueItems}}toSet(){{/uniqueItems}}{{^uniqueItems}}toList(){{/uniqueItems}};
{{/isArray}}
{{^isArray}}
{{#isMap}}
Expand All @@ -199,7 +188,7 @@ class {{{classname}}} {
return await apiClient.deserializeAsync(await _decodeBodyBytes(response), '{{{returnType}}}',) as {{{returnType}}};
{{/isMap}}{{/isArray}}{{/native_serialization}}
}
return Future<{{{returnType}}}>.value();
return null;
{{/returnType}}
}
{{/operation}}
Expand Down
Loading