Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,12 @@ String _toClientRequest(
..write(' $finalResponseType ')
..write('${request.name}(');

if (request.parameters.isNotEmpty ||
final hasParameters = request.parameters.isNotEmpty ||
addExtrasParameter ||
addDioOptionsParameter) {
addDioOptionsParameter ||
request.configExtension.isCancelable;

if (hasParameters) {
sb.write('{\n');
}

Expand All @@ -193,10 +196,11 @@ String _toClientRequest(
if (addDioOptionsParameter) {
sb.write(_addDioOptionsParameter());
}
if (request.configExtension.isCancelable) {
sb.write(_addDioCancelToken());
}

if (request.parameters.isNotEmpty ||
addExtrasParameter ||
addDioOptionsParameter) {
if (hasParameters) {
sb.write(' });\n');
} else {
sb.write(');\n');
Expand Down Expand Up @@ -262,6 +266,9 @@ String _quoteJson(String value) =>
String _addDioOptionsParameter() =>
' @DioOptions() RequestOptions? options,\n';

String _addDioCancelToken() =>
' @CancelRequest() CancelToken? cancelToken,\n';

String _toParameter(UniversalRequestType parameter, bool useMultipartFile) {
var parameterType = parameter.type.toSuitableType(
ProgrammingLanguage.dart,
Expand Down
60 changes: 60 additions & 0 deletions swagger_parser/lib/src/parser/model/universal_request.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ final class UniversalRequest {
this.contentType = 'application/json',
this.description,
this.isDeprecated = false,
this.configExtension = const UniversalRequestConfigExtension(),
});

/// Request name
Expand Down Expand Up @@ -61,6 +62,8 @@ final class UniversalRequest {
/// Value indicating whether this request is deprecated
final bool isDeprecated;

final UniversalRequestConfigExtension configExtension;

@override
bool operator ==(Object other) =>
identical(this, other) ||
Expand Down Expand Up @@ -139,3 +142,60 @@ enum HttpRequestType {
static HttpRequestType? fromString(String type) =>
HttpRequestType.values.firstWhereOrNull((e) => e.name == type);
}

@immutable
final class UniversalRequestConfigExtension {
const UniversalRequestConfigExtension({
bool? isCancelable,
}) : isCancelable = isCancelable ?? false;

factory UniversalRequestConfigExtension.parse(
Map<String, dynamic> requestConfig,
) {
final xDart = requestConfig[_xDartSection] as Map<String, dynamic>?;
return UniversalRequestConfigExtension(
isCancelable: bool.tryParse(xDart?[_isCancelableKey]?.toString() ?? ''),
);
}

static const _xDartSection = 'x-dart';

/// Whether or not the request should be cancelable.
///
/// True if the `cancelable` flag in the `x-dart` section has been set to
/// `true` for the specific request.
///
/// ## Example
///
/// ```yaml
/// paths:
/// /path/variant4:
/// get:
/// x-dart:
/// cancelable: true
/// tags:
/// - sse
/// responses:
/// 200:
/// content:
/// application/octect-stream:
/// schema:
/// type: integer
/// format: binary
/// ```
final bool isCancelable;
static const _isCancelableKey = 'cancelable';

@override
int get hashCode => isCancelable.hashCode;

@override
bool operator ==(Object other) =>
other is UniversalRequestConfigExtension &&
other.isCancelable == isCancelable;

@override
String toString() => 'UniversalRequestConfigExtension('
'isCancelable: $isCancelable'
')';
}
1 change: 1 addition & 0 deletions swagger_parser/lib/src/parser/parser/open_api_parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -709,6 +709,7 @@ class OpenApiParser {
parameters: parameters,
isDeprecated:
requestPath[_deprecatedConst].toString().toBool() ?? false,
configExtension: UniversalRequestConfigExtension.parse(requestPath),
);
// we are converting the tag to the snake case
// later tag is used to determine the file name
Expand Down
Loading