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

Added dio options parameter #258

Merged
merged 1 commit into from
Sep 6, 2024
Merged
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
5 changes: 5 additions & 0 deletions swagger_parser/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ swagger_parser:
# If the value is 'true', then the annotation will be added to all requests.
extras_parameter_by_default: false

# Optional (dart only).
# Support @DioOptions annotation for interceptors.
# If the value is 'true', then the annotation will be added to all requests.
dio_options_parameter_by_default: false

# Optional (dart only). Set 'true' to generate root client
# with interface and all clients instances.
root_client: true
Expand Down
23 changes: 23 additions & 0 deletions swagger_parser/lib/src/config/swp_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class SWPConfig {
this.replacementRules = const [],
this.defaultContentType = 'application/json',
this.extrasParameterByDefault = false,
this.dioOptionsParameterByDefault = false,
this.pathMethodName = false,
this.mergeClients = false,
this.enumsParentPrefix = true,
Expand All @@ -54,6 +55,7 @@ class SWPConfig {
required this.replacementRules,
required this.defaultContentType,
required this.extrasParameterByDefault,
required this.dioOptionsParameterByDefault,
required this.pathMethodName,
required this.mergeClients,
required this.enumsParentPrefix,
Expand Down Expand Up @@ -124,6 +126,14 @@ class SWPConfig {
);
}

final dioOptionsParameterByDefault =
yamlMap['dio_options_parameter_by_default'];
if (dioOptionsParameterByDefault is! bool?) {
throw const ConfigException(
"Config parameter 'dio_options_parameter_by_default' must be bool.",
);
}

final pathMethodName = yamlMap['path_method_name'];
if (pathMethodName is! bool?) {
throw const ConfigException(
Expand Down Expand Up @@ -285,6 +295,8 @@ class SWPConfig {
defaultContentType: defaultContentType ?? dc.defaultContentType,
extrasParameterByDefault:
extrasParameterByDefault ?? dc.extrasParameterByDefault,
dioOptionsParameterByDefault:
dioOptionsParameterByDefault ?? dc.dioOptionsParameterByDefault,
mergeClients: mergeClients ?? dc.mergeClients,
enumsParentPrefix: enumsParentPrefix ?? dc.enumsParentPrefix,
skippedParameters: skippedParameters ?? dc.skippedParameters,
Expand Down Expand Up @@ -380,6 +392,16 @@ class SWPConfig {
/// ```
final bool extrasParameterByDefault;

/// DART ONLY
/// Add dio options parameter to all requests.
///
/// If value is 'true', then the annotation will be added to all requests.
/// ```dart
/// @POST('/path/')
/// Future<String> myMethod({@DioOptions() RequestOptions? options});
/// ```
final bool dioOptionsParameterByDefault;

/// If `true`, use the endpoint path for the method name.
/// if `false`, use `operationId`.
final bool pathMethodName;
Expand All @@ -402,6 +424,7 @@ class SWPConfig {
jsonSerializer: jsonSerializer,
defaultContentType: defaultContentType,
extrasParameterByDefault: extrasParameterByDefault,
dioOptionsParameterByDefault: dioOptionsParameterByDefault,
rootClient: rootClient,
rootClientName: rootClientName,
clientPostfix: clientPostfix,
Expand Down
11 changes: 11 additions & 0 deletions swagger_parser/lib/src/generator/config/generator_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class GeneratorConfig {
this.defaultContentType = 'application/json',
this.rootClient = true,
this.extrasParameterByDefault = false,
this.dioOptionsParameterByDefault = false,
this.rootClientName = 'RestClient',
this.clientPostfix,
this.exportFile = true,
Expand Down Expand Up @@ -91,6 +92,16 @@ class GeneratorConfig {
/// ```
final bool extrasParameterByDefault;

/// DART ONLY
/// Add dio options parameter to all requests.
///
/// If value is 'true', then the annotation will be added to all requests.
/// ```dart
/// @POST('/path/')
/// Future<String> myMethod({@DioOptions() RequestOptions? options});
/// ```
final bool dioOptionsParameterByDefault;

/// Optional. Set regex replacement rules for the names of the generated classes/enums.
/// All rules are applied in order.
final List<ReplacementRule> replacementRules;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ final class FillController {
markFilesAsGenerated: config.markFilesAsGenerated,
defaultContentType: config.defaultContentType,
extrasParameterByDefault: config.extrasParameterByDefault,
dioOptionsParameterByDefault: config.dioOptionsParameterByDefault,
originalHttpResponse: config.originalHttpResponse,
),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ enum ProgrammingLanguage {
required bool markFilesAsGenerated,
required String defaultContentType,
bool extrasParameterByDefault = false,
bool dioOptionsParameterByDefault = false,
bool originalHttpResponse = false,
}) =>
switch (this) {
Expand All @@ -119,6 +120,7 @@ enum ProgrammingLanguage {
markFileAsGenerated: markFilesAsGenerated,
defaultContentType: defaultContentType,
extrasParameterByDefault: extrasParameterByDefault,
dioOptionsParameterByDefault: dioOptionsParameterByDefault,
originalHttpResponse: originalHttpResponse,
),
kotlin => kotlinRetrofitClientTemplate(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ String dartRetrofitClientTemplate({
required bool markFileAsGenerated,
required String defaultContentType,
bool extrasParameterByDefault = false,
bool dioOptionsParameterByDefault = false,
bool originalHttpResponse = false,
}) {
final sb = StringBuffer(
Expand All @@ -36,6 +37,7 @@ abstract class $name {
defaultContentType,
originalHttpResponse: originalHttpResponse,
extrasParameterByDefault: extrasParameterByDefault,
dioOptionsParameterByDefault: dioOptionsParameterByDefault,
),
);
}
Expand All @@ -48,6 +50,7 @@ String _toClientRequest(
String defaultContentType, {
required bool originalHttpResponse,
required bool extrasParameterByDefault,
required bool dioOptionsParameterByDefault,
}) {
final responseType = request.returnType == null
? 'void'
Expand All @@ -58,7 +61,9 @@ String _toClientRequest(
${descriptionComment(request.description, tabForFirstLine: false, tab: ' ', end: ' ')}${request.isDeprecated ? "@Deprecated('This method is marked as deprecated')\n " : ''}${_contentTypeHeader(request, defaultContentType)}@${request.requestType.name.toUpperCase()}('${request.route}')
Future<${originalHttpResponse ? 'HttpResponse<$responseType>' : responseType}> ${request.name}(''',
);
if (request.parameters.isNotEmpty || extrasParameterByDefault) {
if (request.parameters.isNotEmpty ||
extrasParameterByDefault ||
dioOptionsParameterByDefault) {
sb.write('{\n');
}
final sortedByRequired = List<UniversalRequestType>.from(
Expand All @@ -70,7 +75,12 @@ String _toClientRequest(
if (extrasParameterByDefault) {
sb.write(_addExtraParameter());
}
if (request.parameters.isNotEmpty || extrasParameterByDefault) {
if (dioOptionsParameterByDefault) {
sb.write(_addDioOptionsParameter());
}
if (request.parameters.isNotEmpty ||
extrasParameterByDefault ||
dioOptionsParameterByDefault) {
sb.write(' });\n');
} else {
sb.write(');\n');
Expand All @@ -97,6 +107,9 @@ String _fileImport(UniversalRestClient restClient) => restClient.requests.any(

String _addExtraParameter() => ' @Extras() Map<String, dynamic>? extras,\n';

String _addDioOptionsParameter() =>
' @DioOptions() RequestOptions? options,\n';

String _toParameter(UniversalRequestType parameter) {
var parameterType = parameter.type.toSuitableType(ProgrammingLanguage.dart);
// https://github.com/trevorwang/retrofit.dart/issues/631
Expand Down