Browse Source

Merge pull request #247 from provokateurin/feature/static-analysis-dynamite-output

Add dynamites output to static analysis to prevent mistakes
pull/249/head
Kate 2 years ago committed by GitHub
parent
commit
bb623818ff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 44
      packages/dynamite/lib/src/openapi_builder.dart
  2. 4
      packages/nextcloud/analysis_options.yaml
  3. 1986
      packages/nextcloud/lib/src/nextcloud.openapi.dart
  4. 58
      packages/nextcloud/lib/src/nextcloud.openapi.g.dart
  5. 7
      tool/generate-nextcloud.sh

44
packages/dynamite/lib/src/openapi_builder.dart

@ -59,6 +59,9 @@ class OpenAPIBuilder implements Builder {
final state = State(prefix);
final output = <String>[
'// ignore_for_file: avoid_dynamic_calls',
'// ignore_for_file: camel_case_types',
'// ignore_for_file: public_member_api_docs',
"import 'dart:convert';",
"import 'dart:io';",
"import 'dart:typed_data';",
@ -154,7 +157,7 @@ class OpenAPIBuilder implements Builder {
).accept(emitter).toString(),
Class(
(final b) => b
..name = '_Response'
..name = 'RawResponse'
..fields.addAll([
Field(
(final b) => b
@ -197,7 +200,7 @@ class OpenAPIBuilder implements Builder {
..annotations.add(refer('override'))
..lambda = true
..body = const Code(
r"'_Response(statusCode: $statusCode, headers: $headers, body: ${utf8.decode(body)})'",
r"'RawResponse(statusCode: $statusCode, headers: $headers, body: ${utf8.decode(body)})'",
),
),
),
@ -205,7 +208,7 @@ class OpenAPIBuilder implements Builder {
Class(
(final b) => b
..name = '${prefix}ApiException'
..extend = refer('_Response')
..extend = refer('RawResponse')
..implements.add(refer('Exception'))
..constructors.addAll(
[
@ -230,7 +233,7 @@ class OpenAPIBuilder implements Builder {
Parameter(
(final b) => b
..name = 'response'
..type = refer('_Response'),
..type = refer('RawResponse'),
),
)
..body = Code('${prefix}ApiException(response.statusCode, response.headers, response.body,)'),
@ -527,7 +530,7 @@ class OpenAPIBuilder implements Builder {
Method(
(final b) => b
..name = 'doRequest'
..returns = refer('Future<_Response>')
..returns = refer('Future<RawResponse>')
..modifier = MethodModifier.async
..requiredParameters.addAll([
Parameter(
@ -572,7 +575,7 @@ class OpenAPIBuilder implements Builder {
response.headers.forEach((final name, final values) {
responseHeaders[name] = values.last;
});
return _Response(
return RawResponse(
response.statusCode,
responseHeaders,
await response.bodyBytes,
@ -639,7 +642,12 @@ class OpenAPIBuilder implements Builder {
final parameters = <spec_parameter.Parameter>[
...pathParameters,
if (operation.parameters != null) ...operation.parameters!,
];
]..sort(
(final a, final b) => sortRequiredElements(
a.required ?? false,
b.required ?? false,
),
);
b
..name = _toDartName(_filterMethodName(operationId, tag ?? ''))
..modifier = MethodModifier.async
@ -1231,6 +1239,13 @@ TypeResult resolveObject(
state.output.add(
Class(
(final b) {
final sortedParameterKeys = schema.properties!.keys.toList()
..sort(
(final a, final b) => sortRequiredElements(
(schema.required ?? []).contains(a),
(schema.required ?? []).contains(b),
),
);
b
..name = '${state.prefix}$identifier'
..docs.addAll(_descriptionToDocs(schema.description))
@ -1254,7 +1269,7 @@ TypeResult resolveObject(
Constructor(
(final b) => b
..optionalParameters.addAll(
schema.properties!.keys.map(
sortedParameterKeys.map(
(final propertyName) => Parameter(
(final b) {
final propertySchema = schema.properties![propertyName]!;
@ -1862,3 +1877,16 @@ TypeResult resolveType(
throw Exception('Can not convert OpenAPI type "${schema.toJson()}" to a Dart type');
}
// ignore: avoid_positional_boolean_parameters
int sortRequiredElements(final bool a, final bool b) {
if (a != b) {
if (a && !b) {
return -1;
} else {
return 1;
}
}
return 0;
}

4
packages/nextcloud/analysis_options.yaml

@ -1,5 +1 @@
include: package:nit_picking/dart.yaml
analyzer:
exclude:
- 'lib/src/**.openapi.**'

1986
packages/nextcloud/lib/src/nextcloud.openapi.dart

File diff suppressed because it is too large Load Diff

58
packages/nextcloud/lib/src/nextcloud.openapi.g.dart

@ -1270,16 +1270,9 @@ NextcloudNewsArticle _$NextcloudNewsArticleFromJson(Map<String, dynamic> json) {
id: json['id'] as int,
guid: json['guid'] as String,
guidHash: json['guidHash'] as String,
url: json['url'] as String?,
title: json['title'] as String,
author: json['author'] as String?,
pubDate: json['pubDate'] as int,
updatedDate: json['updatedDate'] as int?,
body: json['body'] as String,
enclosureMime: json['enclosureMime'] as String?,
enclosureLink: json['enclosureLink'] as String?,
mediaThumbnail: json['mediaThumbnail'] as String?,
mediaDescription: json['mediaDescription'] as String?,
feedId: json['feedId'] as int,
unread: json['unread'] as bool,
starred: json['starred'] as bool,
@ -1287,6 +1280,13 @@ NextcloudNewsArticle _$NextcloudNewsArticleFromJson(Map<String, dynamic> json) {
rtl: json['rtl'] as bool,
fingerprint: json['fingerprint'] as String,
contentHash: json['contentHash'] as String,
url: json['url'] as String?,
author: json['author'] as String?,
updatedDate: json['updatedDate'] as int?,
enclosureMime: json['enclosureMime'] as String?,
enclosureLink: json['enclosureLink'] as String?,
mediaThumbnail: json['mediaThumbnail'] as String?,
mediaDescription: json['mediaDescription'] as String?,
);
}
@ -1336,17 +1336,17 @@ NextcloudNewsFeed _$NextcloudNewsFeedFromJson(Map<String, dynamic> json) {
id: json['id'] as int,
url: json['url'] as String,
title: json['title'] as String,
faviconLink: json['faviconLink'] as String?,
added: json['added'] as int,
folderId: json['folderId'] as int?,
unreadCount: json['unreadCount'] as int?,
ordering: json['ordering'] as int,
link: json['link'] as String?,
pinned: json['pinned'] as bool,
updateErrorCount: json['updateErrorCount'] as int,
lastUpdateError: json['lastUpdateError'] as String?,
items:
(json['items'] as List<dynamic>).map((e) => NextcloudNewsArticle.fromJson(e as Map<String, dynamic>)).toList(),
faviconLink: json['faviconLink'] as String?,
folderId: json['folderId'] as int?,
unreadCount: json['unreadCount'] as int?,
link: json['link'] as String?,
lastUpdateError: json['lastUpdateError'] as String?,
);
}
@ -1407,9 +1407,9 @@ NextcloudNewsListFeeds _$NextcloudNewsListFeedsFromJson(Map<String, dynamic> jso
allowedKeys: const ['starredCount', 'newestItemId', 'feeds'],
);
return NextcloudNewsListFeeds(
feeds: (json['feeds'] as List<dynamic>).map((e) => NextcloudNewsFeed.fromJson(e as Map<String, dynamic>)).toList(),
starredCount: json['starredCount'] as int?,
newestItemId: json['newestItemId'] as int?,
feeds: (json['feeds'] as List<dynamic>).map((e) => NextcloudNewsFeed.fromJson(e as Map<String, dynamic>)).toList(),
);
}
@ -1835,20 +1835,21 @@ NextcloudProvisioningApiUserDetails _$NextcloudProvisioningApiUserDetailsFromJso
],
);
return NextcloudProvisioningApiUserDetails(
enabled: json['enabled'] as bool?,
storageLocation: json['storageLocation'] as String?,
displaynameScope: json['displaynameScope'] as String,
backendCapabilities: NextcloudProvisioningApiUserDetails_BackendCapabilities.fromJson(
json['backendCapabilities'] as Map<String, dynamic>),
id: json['id'] as String,
lastLogin: json['lastLogin'] as int,
backend: json['backend'] as String,
subadmin: (json['subadmin'] as List<dynamic>).map((e) => e as String).toList(),
quota: NextcloudProvisioningApiUserDetails_Quota.fromJson(json['quota'] as Map<String, dynamic>),
avatarScope: json['avatarScope'] as String,
email: json['email'] as String?,
locale: json['locale'] as String,
emailScope: json['emailScope'] as String,
additionalMail: (json['additional_mail'] as List<dynamic>).map((e) => e as String).toList(),
additionalMailScope: (json['additional_mailScope'] as List<dynamic>).map((e) => e as String).toList(),
displayname: json['displayname'] as String?,
displaynameScope: json['displaynameScope'] as String,
language: json['language'] as String,
groups: (json['groups'] as List<dynamic>).map((e) => e as String).toList(),
phone: json['phone'] as String,
phoneScope: json['phoneScope'] as String,
address: json['address'] as String,
@ -1860,7 +1861,7 @@ NextcloudProvisioningApiUserDetails _$NextcloudProvisioningApiUserDetailsFromJso
organisation: json['organisation'] as String,
organisationScope: json['organisationScope'] as String,
role: json['role'] as String,
roleScope: json['roleScope'] as String,
fediverseScope: json['fediverseScope'] as String,
headline: json['headline'] as String,
headlineScope: json['headlineScope'] as String,
biography: json['biography'] as String,
@ -1868,13 +1869,12 @@ NextcloudProvisioningApiUserDetails _$NextcloudProvisioningApiUserDetailsFromJso
profileEnabled: json['profile_enabled'] as String,
profileEnabledScope: json['profile_enabledScope'] as String,
fediverse: json['fediverse'] as String,
fediverseScope: json['fediverseScope'] as String,
groups: (json['groups'] as List<dynamic>).map((e) => e as String).toList(),
language: json['language'] as String,
locale: json['locale'] as String,
roleScope: json['roleScope'] as String,
enabled: json['enabled'] as bool?,
displayname: json['displayname'] as String?,
email: json['email'] as String?,
notifyEmail: json['notify_email'] as String?,
backendCapabilities: NextcloudProvisioningApiUserDetails_BackendCapabilities.fromJson(
json['backendCapabilities'] as Map<String, dynamic>),
storageLocation: json['storageLocation'] as String?,
displayName: json['display-name'] as String?,
);
}
@ -2211,10 +2211,10 @@ NextcloudUserStatusPublicStatus _$NextcloudUserStatusPublicStatusFromJson(Map<St
);
return NextcloudUserStatusPublicStatus(
userId: json['userId'] as String,
status: $enumDecode(_$NextcloudUserStatusTypeEnumMap, json['status']),
message: json['message'] as String?,
icon: json['icon'] as String?,
clearAt: json['clearAt'] == null ? null : NextcloudUserStatusPublicStatus_ClearAt.fromJson(json['clearAt']),
status: $enumDecode(_$NextcloudUserStatusTypeEnumMap, json['status']),
);
}
@ -2318,13 +2318,13 @@ NextcloudUserStatusStatus _$NextcloudUserStatusStatusFromJson(Map<String, dynami
);
return NextcloudUserStatusStatus(
userId: json['userId'] as String,
messageIsPredefined: json['messageIsPredefined'] as bool,
status: $enumDecode(_$NextcloudUserStatusTypeEnumMap, json['status']),
statusIsUserDefined: json['statusIsUserDefined'] as bool,
message: json['message'] as String?,
messageId: json['messageId'] as String?,
messageIsPredefined: json['messageIsPredefined'] as bool,
icon: json['icon'] as String?,
clearAt: json['clearAt'] == null ? null : NextcloudUserStatusStatus_ClearAt.fromJson(json['clearAt']),
status: $enumDecode(_$NextcloudUserStatusTypeEnumMap, json['status']),
statusIsUserDefined: json['statusIsUserDefined'] as bool,
);
}

7
tool/generate-nextcloud.sh

@ -63,6 +63,9 @@ jq \
cd packages/nextcloud
rm -rf .dart_tool/build
fvm dart pub run build_runner build --delete-conflicting-outputs
# For some reason we need to fix and format twice, otherwise not everything gets fixed
fvm dart fix --apply lib/src/nextcloud.openapi.dart
../../tool/format.sh
fvm dart fix --apply lib/src/nextcloud.openapi.dart
../../tool/format.sh
)
./tool/format.sh

Loading…
Cancel
Save