Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions docs/generators/dart-dio.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
|sortModelPropertiesByRequiredFlag|Sort model properties to place required parameters before optional parameters.| |true|
|sortParamsByRequiredFlag|Sort method arguments to place required parameters before optional parameters.| |true|
|sourceFolder|source folder for generated code| |src|
|supportDart2|Use dependencies compatible with Dart 2.| |false|
|useEnumExtension|Allow the 'x-enum-values' extension for enums| |false|

## IMPORT MAPPING
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ public class DartDioClientCodegen extends AbstractDartCodegen {
public static final String FINAL_PROPERTIES = "finalProperties";
public static final String FINAL_PROPERTIES_DEFAULT_VALUE = "true";

public static final String SUPPORT_DART2 = "supportDart2";
public static final String SUPPORT_DART2_DEFAULT_VALUE = "false";

private static final String CLIENT_NAME = "clientName";

@Getter @Setter
Expand Down Expand Up @@ -138,6 +141,11 @@ public DartDioClientCodegen() {
final CliOption finalProperties = CliOption.newBoolean(FINAL_PROPERTIES, "Whether properties are marked as final when using Json Serializable for serialization");
finalProperties.setDefault("true");
cliOptions.add(finalProperties);

// Support Dart 2 Option
final CliOption supportDart2 = CliOption.newBoolean(SUPPORT_DART2, "Use dependencies compatible with Dart 2.");
supportDart2.setDefault("false");
cliOptions.add(supportDart2);
}

@Override
Expand Down Expand Up @@ -183,6 +191,14 @@ public void processOpts() {
additionalProperties.put(FINAL_PROPERTIES, Boolean.parseBoolean(additionalProperties.get(FINAL_PROPERTIES).toString()));
}

if (!additionalProperties.containsKey(SUPPORT_DART2)) {
additionalProperties.put(SUPPORT_DART2, Boolean.parseBoolean(SUPPORT_DART2_DEFAULT_VALUE));
LOGGER.debug("supportDart2 not set, using default {}", SUPPORT_DART2_DEFAULT_VALUE);
}
else {
additionalProperties.put(SUPPORT_DART2, Boolean.parseBoolean(additionalProperties.get(SUPPORT_DART2).toString()));
}

if (!additionalProperties.containsKey(CLIENT_NAME)) {
final String name = org.openapitools.codegen.utils.StringUtils.camelize(pubName);
additionalProperties.put(CLIENT_NAME, name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ For more information, please visit [{{{infoUrl}}}]({{{infoUrl}}})

## Requirements

{{#supportDart2}}
* Dart 2.15.0+ or Flutter 2.8.0+
{{/supportDart2}}
{{^supportDart2}}
* Dart 3.0.0+ or Flutter 2.8.0+
{{/supportDart2}}
* Dio 5.0.0+ (https://pub.dev/packages/dio)
{{#useJsonSerializable}}
* JSON Serializable 6.1.5+ (https://pub.dev/packages/json_serializable)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ publish_to: {{.}}


environment:
sdk: '>={{^useJsonSerializable}}2.18.0{{/useJsonSerializable}}{{#useJsonSerializable}}3.5.0{{/useJsonSerializable}} <4.0.0'
sdk: '>={{#useJsonSerializable}}{{#supportDart2}}2.17.0{{/supportDart2}}{{^supportDart2}}3.0.0{{/supportDart2}}{{/useJsonSerializable}}{{^useJsonSerializable}}2.18.0{{/useJsonSerializable}} <4.0.0'

dependencies:
dio: '^5.7.0'
Expand All @@ -38,6 +38,11 @@ dev_dependencies:
{{/useBuiltValue}}
{{#useJsonSerializable}}
build_runner: any
json_serializable: '^6.9.3'
{{#supportDart2}}
json_serializable: '>=6.1.5 <6.9.0'
{{/supportDart2}}
{{^supportDart2}}
json_serializable: '^6.9.0'
{{/supportDart2}}
{{/useJsonSerializable}}
test: '^1.16.0'
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public Map<String, String> createOptions() {
.put(CodegenConstants.SERIALIZATION_LIBRARY, DartDioClientCodegen.SERIALIZATION_LIBRARY_DEFAULT)
.put(DartDioClientCodegen.DATE_LIBRARY, DartDioClientCodegen.DATE_LIBRARY_DEFAULT)
.put(DartDioClientCodegen.FINAL_PROPERTIES, DartDioClientCodegen.FINAL_PROPERTIES_DEFAULT_VALUE)
.put(DartDioClientCodegen.SUPPORT_DART2, DartDioClientCodegen.SUPPORT_DART2_DEFAULT_VALUE)
.put(DartDioClientCodegen.EQUALITY_CHECK_METHOD, DartDioClientCodegen.EQUALITY_CHECK_METHOD_DEFAULT)
.put(CodegenConstants.SOURCE_FOLDER, SOURCE_FOLDER_VALUE)
.put(DartDioClientCodegen.USE_ENUM_EXTENSION, USE_ENUM_EXTENSION)
Expand Down
2 changes: 1 addition & 1 deletion samples/openapi3/client/petstore/dart-dio/oneof/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ This Dart package is automatically generated by the [OpenAPI Generator](https://

## Requirements

* Dart 2.15.0+ or Flutter 2.8.0+
* Dart 3.0.0+ or Flutter 2.8.0+
* Dio 5.0.0+ (https://pub.dev/packages/dio)

## Installation & Usage
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ This Dart package is automatically generated by the [OpenAPI Generator](https://

## Requirements

* Dart 2.15.0+ or Flutter 2.8.0+
* Dart 3.0.0+ or Flutter 2.8.0+
* Dio 5.0.0+ (https://pub.dev/packages/dio)

## Installation & Usage
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ This Dart package is automatically generated by the [OpenAPI Generator](https://

## Requirements

* Dart 2.15.0+ or Flutter 2.8.0+
* Dart 3.0.0+ or Flutter 2.8.0+
* Dio 5.0.0+ (https://pub.dev/packages/dio)

## Installation & Usage
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ This Dart package is automatically generated by the [OpenAPI Generator](https://

## Requirements

* Dart 2.15.0+ or Flutter 2.8.0+
* Dart 3.0.0+ or Flutter 2.8.0+
* Dio 5.0.0+ (https://pub.dev/packages/dio)
* JSON Serializable 6.1.5+ (https://pub.dev/packages/json_serializable)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ homepage: homepage


environment:
sdk: '>=3.5.0 <4.0.0'
sdk: '>=3.0.0 <4.0.0'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for the PR

does it mean the output (dart API client) no longer work with Dart 2.x?

Copy link
Author

@al-kimmel-serj al-kimmel-serj Jan 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. I use json_serializable as serializationLibrary and I see error when I try to generate code via dart run build_runner build with json_serializable:6.9.0. Here is example of error:

line 18, column 30 of .: Expected to find ')'.
   ╷
18 │         if (instance.message case final value?) 'message': value,
   │                              ^^^^
   ╵

json_serializable generates code with case clauses in if statements since 6.9.0.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about adding an option supportDart2 (default to false) for backward compatibility?

we can't ship a change that breaks all Dart 2.x users.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. I will add the option to PR.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wing328, I've pushed code for supporting Dart 2.x.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks. i'll take another look


dependencies:
dio: '^5.7.0'
json_annotation: '^4.9.0'

dev_dependencies:
build_runner: any
json_serializable: '^6.9.3'
json_serializable: '^6.9.0'
test: '^1.16.0'
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ This Dart package is automatically generated by the [OpenAPI Generator](https://

## Requirements

* Dart 2.15.0+ or Flutter 2.8.0+
* Dart 3.0.0+ or Flutter 2.8.0+
* Dio 5.0.0+ (https://pub.dev/packages/dio)

## Installation & Usage
Expand Down