Skip to content

Commit

Permalink
Dart - update generator to support null safety (#10637)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
Co-authored-by: Artur Powroznik <[email protected]>
Co-authored-by: devjakobsen <[email protected]>
Co-authored-by: Noor Dawod <[email protected]>
  • Loading branch information
5 people committed Jan 2, 2022
1 parent 5f5a83a commit 6c3cdee
Show file tree
Hide file tree
Showing 191 changed files with 4,606 additions and 4,260 deletions.
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;
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

0 comments on commit 6c3cdee

Please sign in to comment.