Skip to content

Commit

Permalink
Support dart retrofit 4.1.0 @Extras parameter option (#209)
Browse files Browse the repository at this point in the history
  • Loading branch information
dfdgsdfg authored Mar 5, 2024
1 parent 04202c1 commit 6562083
Show file tree
Hide file tree
Showing 11 changed files with 285 additions and 4 deletions.
4 changes: 4 additions & 0 deletions swagger_parser/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 1.17.0

- Add new config parameter `extras_parameter_by_default` from [retrofit 4.1.0](https://pub.dev/packages/retrofit/changelog#410) for ([#208][https://github.com/Carapacik/swagger_parser/issues/208])

## 1.16.4

- Fixed errors with `required_by_default`
Expand Down
5 changes: 5 additions & 0 deletions swagger_parser/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ swagger_parser:
# Optional. Set default content-type for all requests.
default_content_type: "application/json"
# Optional (dart only).
# Support @Extras annotation for interceptors.
# If the value is 'true', then the annotation will be added to all requests.
extras_parameter_by_default: false
# Optional (dart only).
# It is used if the value does not have the annotations 'required' and 'nullable'.
# If the value is 'true', then value be 'required', if the value is 'false', then 'nullable'.
Expand Down
5 changes: 5 additions & 0 deletions swagger_parser/example/swagger_parser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ swagger_parser:
# Optional. Set default content-type for all requests.
default_content_type: "application/json"

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

# Optional (dart only).
# It is used if the value does not have the annotations 'required' and 'nullable'.
# If the value is 'true', then value be 'required', if the value is 'false', then 'nullable'.
Expand Down
20 changes: 20 additions & 0 deletions swagger_parser/lib/src/config/swp_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class SWPConfig {
this.originalHttpResponse = false,
this.replacementRules = const [],
this.defaultContentType = 'application/json',
this.extrasParameterByDefault = false,
this.pathMethodName = false,
this.requiredByDefault = true,
this.mergeClients = false,
Expand All @@ -53,6 +54,7 @@ class SWPConfig {
required this.originalHttpResponse,
required this.replacementRules,
required this.defaultContentType,
required this.extrasParameterByDefault,
required this.pathMethodName,
required this.requiredByDefault,
required this.mergeClients,
Expand Down Expand Up @@ -117,6 +119,13 @@ class SWPConfig {
);
}

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

final pathMethodName = yamlMap['path_method_name'];
if (pathMethodName is! bool?) {
throw const ConfigException(
Expand Down Expand Up @@ -276,6 +285,8 @@ class SWPConfig {
name: name,
pathMethodName: pathMethodName ?? dc.pathMethodName,
defaultContentType: defaultContentType ?? dc.defaultContentType,
extrasParameterByDefault:
extrasParameterByDefault ?? dc.extrasParameterByDefault,
requiredByDefault: requiredByDefault ?? dc.requiredByDefault,
mergeClients: mergeClients ?? dc.mergeClients,
enumsParentPrefix: enumsParentPrefix ?? dc.enumsParentPrefix,
Expand Down Expand Up @@ -362,6 +373,14 @@ class SWPConfig {
/// @Headers(<String, String>{'Content-Type': 'PARSED CONTENT TYPE'})
final String defaultContentType;

/// DART ONLY
/// Add extra parameter to all requests. Supported after retrofit 4.1.0.
///
///
/// @POST('/path/')
/// Future<String> myMethod({@Extras() Map<String, dynamic>? extras});
final bool extrasParameterByDefault;

/// DART ONLY
/// It is used if the value does not have the annotations `required` and `nullable`.
/// If the value is `true`, then value be `required`.
Expand Down Expand Up @@ -389,6 +408,7 @@ class SWPConfig {
language: language,
jsonSerializer: jsonSerializer,
defaultContentType: defaultContentType,
extrasParameterByDefault: extrasParameterByDefault,
rootClient: rootClient,
rootClientName: rootClientName,
clientPostfix: clientPostfix,
Expand Down
9 changes: 9 additions & 0 deletions swagger_parser/lib/src/generator/config/generator_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class GeneratorConfig {
this.jsonSerializer = JsonSerializer.jsonSerializable,
this.defaultContentType = 'application/json',
this.rootClient = true,
this.extrasParameterByDefault = false,
this.rootClientName = 'RestClient',
this.clientPostfix,
this.exportFile = true,
Expand Down Expand Up @@ -80,6 +81,14 @@ class GeneratorConfig {
/// @Headers(<String, String>{'Content-Type': 'PARSED CONTENT TYPE'})
final String defaultContentType;

/// DART ONLY
/// Add extras parameter to all requests. Supported after retrofit 4.1.0.
///
///
/// @POST('/path/')
/// Future<String> myMethod({@Extras() Map<String, dynamic>? extras});
final bool extrasParameterByDefault;

/// 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 @@ -48,6 +48,7 @@ final class FillController {
restClient.name.toPascal + postfix.toPascal,
markFilesAsGenerated: config.markFilesAsGenerated,
defaultContentType: config.defaultContentType,
extrasParameterByDefault: config.extrasParameterByDefault,
originalHttpResponse: config.originalHttpResponse,
),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ enum ProgrammingLanguage {
String name, {
required bool markFilesAsGenerated,
required String defaultContentType,
bool extrasParameterByDefault = false,
bool originalHttpResponse = false,
}) =>
switch (this) {
Expand All @@ -117,6 +118,7 @@ enum ProgrammingLanguage {
name: name,
markFileAsGenerated: markFilesAsGenerated,
defaultContentType: defaultContentType,
extrasParameterByDefault: extrasParameterByDefault,
originalHttpResponse: originalHttpResponse,
),
kotlin => kotlinRetrofitClientTemplate(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ String dartRetrofitClientTemplate({
required String name,
required bool markFileAsGenerated,
required String defaultContentType,
bool extrasParameterByDefault = false,
bool originalHttpResponse = false,
}) {
final sb = StringBuffer(
Expand All @@ -32,6 +33,7 @@ abstract class $name {
request,
defaultContentType,
originalHttpResponse: originalHttpResponse,
extrasParameterByDefault: extrasParameterByDefault,
),
);
}
Expand All @@ -43,6 +45,7 @@ String _toClientRequest(
UniversalRequest request,
String defaultContentType, {
required bool originalHttpResponse,
required bool extrasParameterByDefault,
}) {
final responseType = request.returnType == null
? 'void'
Expand All @@ -53,7 +56,7 @@ 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) {
if (request.parameters.isNotEmpty || extrasParameterByDefault) {
sb.write('{\n');
}
final sortedByRequired = List<UniversalRequestType>.from(
Expand All @@ -62,7 +65,10 @@ String _toClientRequest(
for (final parameter in sortedByRequired) {
sb.write('${_toParameter(parameter)}\n');
}
if (request.parameters.isNotEmpty) {
if (extrasParameterByDefault) {
sb.write(_addExtraParameter());
}
if (request.parameters.isNotEmpty || extrasParameterByDefault) {
sb.write(' });\n');
} else {
sb.write(');\n');
Expand All @@ -87,6 +93,8 @@ String _fileImport(UniversalRestClient restClient) => restClient.requests.any(
? "import 'dart:io';\n\n"
: '';

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

String _toParameter(UniversalRequestType parameter) {
var parameterType = parameter.type.toSuitableType(ProgrammingLanguage.dart);
// https://github.com/trevorwang/retrofit.dart/issues/631
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ enum HttpParameterType {
/// `@Body`
body('Body'),

/// `@Extras`
extras('Extras'),

/// `@Query`
query('Query'),

Expand Down
2 changes: 1 addition & 1 deletion swagger_parser/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: swagger_parser
description: Package that generates REST clients and data classes from OpenApi definition file
version: 1.16.4
version: 1.17.0
repository: https://github.com/Carapacik/swagger_parser/tree/main/swagger_parser
homepage: https://omega-r.com
topics:
Expand Down
Loading

0 comments on commit 6562083

Please sign in to comment.