Skip to content

Commit

Permalink
Add new config parameter original_http_response(#116)
Browse files Browse the repository at this point in the history
Co-authored-by: Roman Laptev <[email protected]>
  • Loading branch information
theoolee and Carapacik authored Oct 20, 2023
1 parent 2af3e70 commit e2c82ee
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 2 deletions.
3 changes: 3 additions & 0 deletions swagger_parser/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 1.10.3
- Add new config parameter `original_http_response`(only for dart) ([#115](https://github.com/Carapacik/swagger_parser/issues/115))

## 1.10.2
- Fix error in `body` with name in dart template

Expand Down
3 changes: 3 additions & 0 deletions swagger_parser/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ swagger_parser:
# Optional. Set 'false' to not put a comment at the beginning of the generated files.
mark_files_as_generated: true
# Optional (dart only). Set 'true' to wrap all request return types with HttpResponse.
original_http_response: false
# Optional. Set regex replacement rules for the names of the generated classes/enums.
# All rules are applied in order.
replacement_rules:
Expand Down
11 changes: 11 additions & 0 deletions swagger_parser/lib/src/config/yaml_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ final class YamlConfig {
this.enumsToJson,
this.enumsPrefix,
this.markFilesAsGenerated,
this.originalHttpResponse,
this.replacementRules = const [],
});

Expand Down Expand Up @@ -209,6 +210,13 @@ final class YamlConfig {
);
}

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

final rawReplacementRules = yamlConfig['replacement_rules'];
if (rawReplacementRules is! YamlList?) {
throw const ConfigException(
Expand Down Expand Up @@ -254,6 +262,8 @@ final class YamlConfig {
putClientsInFolder: putClientsInFolder ?? rootConfig?.putClientsInFolder,
squashClients: squashClients ?? rootConfig?.squashClients,
pathMethodName: pathMethodName ?? rootConfig?.pathMethodName,
originalHttpResponse:
originalHttpResponse ?? rootConfig?.originalHttpResponse,
enumsToJson: enumsToJson ?? rootConfig?.enumsToJson,
enumsPrefix: enumsPrefix ?? rootConfig?.enumsPrefix,
markFilesAsGenerated:
Expand Down Expand Up @@ -352,5 +362,6 @@ final class YamlConfig {
final bool? enumsToJson;
final bool? enumsPrefix;
final bool? markFilesAsGenerated;
final bool? originalHttpResponse;
final List<ReplacementRule> replacementRules;
}
7 changes: 7 additions & 0 deletions swagger_parser/lib/src/generator/generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ final class Generator {
String? rootClientName,
bool? putClientsInFolder,
bool? squashClients,
bool? originalHttpResponse,
bool? pathMethodName,
bool? putInFolder,
bool? enumsToJson,
Expand All @@ -61,6 +62,7 @@ final class Generator {
_clientPostfix = clientPostfix ?? 'Client',
_putClientsInFolder = putClientsInFolder ?? false,
_squashClients = squashClients ?? false,
_originalHttpResponse = originalHttpResponse ?? false,
_pathMethodName = pathMethodName ?? false,
_putInFolder = putInFolder ?? false,
_enumsToJson = enumsToJson ?? false,
Expand All @@ -84,6 +86,7 @@ final class Generator {
clientPostfix: yamlConfig.clientPostfix,
putClientsInFolder: yamlConfig.putClientsInFolder,
squashClients: yamlConfig.squashClients,
originalHttpResponse: yamlConfig.originalHttpResponse,
pathMethodName: yamlConfig.pathMethodName,
putInFolder: yamlConfig.putInFolder,
enumsToJson: yamlConfig.enumsToJson,
Expand Down Expand Up @@ -138,6 +141,9 @@ final class Generator {
/// Squash all clients in one client.
final bool _squashClients;

/// Generate request methods with HttpResponse<Entity>
final bool _originalHttpResponse;

/// If true, use the endpoint path for the method name, if false, use operationId
final bool _pathMethodName;

Expand Down Expand Up @@ -257,6 +263,7 @@ final class Generator {
name: _name,
squashClients: _squashClients,
replacementRules: _replacementRules,
originalHttpResponse: _originalHttpResponse,
);
_openApiInfo = parser.parseOpenApiInfo();
_restClients = parser.parseRestClients();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ final class UniversalRequest {
this.isMultiPart = false,
this.isFormUrlEncoded = false,
this.isDeprecated = false,
this.isOriginalHttpResponse = false,
});

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

/// Wrap request return type with HttpResponse
final bool isOriginalHttpResponse;

@override
bool operator ==(Object other) =>
identical(this, other) ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,14 @@ abstract class $name {
}

String _toClientRequest(UniversalRequest request) {
final responseType = request.returnType == null
? 'void'
: request.returnType!.toSuitableType(ProgrammingLanguage.dart);
final sb = StringBuffer(
'''
${descriptionComment(request.description, tabForFirstLine: false, tab: ' ', end: ' ')}${request.isDeprecated ? "@Deprecated('This method is marked as deprecated')\n " : ''}${request.isMultiPart ? '@MultiPart()\n ' : ''}${request.isFormUrlEncoded ? '@FormUrlEncoded()\n ' : ''}@${request.requestType.name.toUpperCase()}('${request.route}')
Future<${request.returnType == null ? 'void' : request.returnType!.toSuitableType(ProgrammingLanguage.dart)}> ${request.name}(''',
Future<${request.isOriginalHttpResponse ? 'HttpResponse<$responseType>' : responseType}> ${request.name}(''',
);
if (request.parameters.isNotEmpty) {
sb.write('{\n');
Expand Down
4 changes: 4 additions & 0 deletions swagger_parser/lib/src/parser/parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@ class OpenApiParser {
bool enumsPrefix = false,
bool pathMethodName = false,
bool squashClients = false,
bool originalHttpResponse = false,
List<ReplacementRule> replacementRules = const <ReplacementRule>[],
}) : _name = name,
_pathMethodName = pathMethodName,
_enumsPrefix = enumsPrefix,
_squashClients = squashClients,
_originalHttpResponse = originalHttpResponse,
_replacementRules = replacementRules {
_definitionFileContent = isYaml
? (loadYaml(fileContent) as YamlMap).toMap()
Expand Down Expand Up @@ -62,6 +64,7 @@ class OpenApiParser {
final bool _enumsPrefix;
final String? _name;
final bool _squashClients;
final bool _originalHttpResponse;
final List<ReplacementRule> _replacementRules;
late final Map<String, dynamic> _definitionFileContent;
late final OAS _version;
Expand Down Expand Up @@ -472,6 +475,7 @@ class OpenApiParser {
route: path,
isMultiPart: isMultiPart,
isFormUrlEncoded: isFormUrlEncoded,
isOriginalHttpResponse: _originalHttpResponse,
returnType: returnType,
parameters: parameters,
isDeprecated: requestPath[_deprecatedConst].toString().toBool(),
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.10.2
version: 1.10.3
repository: https://github.com/Carapacik/swagger_parser/tree/main/swagger_parser
homepage: https://omega-r.com
topics:
Expand Down

0 comments on commit e2c82ee

Please sign in to comment.