Browse Source

refactor(dynamite): cleanup _descriptionToDocs

Signed-off-by: Nikolas Rimikis <leptopoda@users.noreply.github.com>
pull/694/head
Nikolas Rimikis 1 year ago
parent
commit
75e9013a4f
No known key found for this signature in database
GPG Key ID: 85ED1DE9786A4FF2
  1. 9
      packages/dynamite/dynamite/lib/src/helpers/docs.dart
  2. 8
      packages/dynamite/dynamite/lib/src/models/open_api.dart
  3. 11
      packages/dynamite/dynamite/lib/src/models/operation.dart
  4. 3
      packages/dynamite/dynamite/lib/src/models/schema.dart
  5. 3
      packages/dynamite/dynamite/lib/src/models/tag.dart
  6. 29
      packages/dynamite/dynamite/lib/src/openapi_builder.dart

9
packages/dynamite/dynamite/lib/src/helpers/docs.dart

@ -0,0 +1,9 @@
const docsSeparator = '///';
Iterable<String> descriptionToDocs(final String? description) sync* {
if (description != null && description.isNotEmpty) {
for (final line in description.split('\n')) {
yield '$docsSeparator $line';
}
}
}

8
packages/dynamite/dynamite/lib/src/models/open_api.dart

@ -40,4 +40,12 @@ class OpenAPI {
final Components? components; final Components? components;
final Paths? paths; final Paths? paths;
Iterable<String> formattedTagsFor(final String? tag) sync* {
final matchedTags = tags?.where((final t) => t.name == tag);
if (matchedTags != null && matchedTags.isNotEmpty) {
yield* matchedTags.single.formattedDescription;
}
}
} }

11
packages/dynamite/dynamite/lib/src/models/operation.dart

@ -1,3 +1,4 @@
import 'package:dynamite/src/helpers/docs.dart';
import 'package:dynamite/src/models/parameter.dart'; import 'package:dynamite/src/models/parameter.dart';
import 'package:dynamite/src/models/request_body.dart'; import 'package:dynamite/src/models/request_body.dart';
import 'package:dynamite/src/models/response.dart'; import 'package:dynamite/src/models/response.dart';
@ -43,4 +44,14 @@ class Operation {
final Responses? responses; final Responses? responses;
final List<SecurityRequirement>? security; final List<SecurityRequirement>? security;
Iterable<String> get formattedDescription sync* {
yield* descriptionToDocs(summary);
if (summary != null && description != null) {
yield docsSeparator;
}
yield* descriptionToDocs(description);
}
} }

3
packages/dynamite/dynamite/lib/src/models/schema.dart

@ -1,3 +1,4 @@
import 'package:dynamite/src/helpers/docs.dart';
import 'package:dynamite/src/models/discriminator.dart'; import 'package:dynamite/src/models/discriminator.dart';
import 'package:json_annotation/json_annotation.dart'; import 'package:json_annotation/json_annotation.dart';
import 'package:meta/meta.dart'; import 'package:meta/meta.dart';
@ -85,6 +86,8 @@ class Schema {
final bool? nullable; final bool? nullable;
bool get isContentString => type == 'string' && (contentMediaType?.isNotEmpty ?? false) && contentSchema != null; bool get isContentString => type == 'string' && (contentMediaType?.isNotEmpty ?? false) && contentSchema != null;
Iterable<String> get formattedDescription => descriptionToDocs(description);
} }
class EmptySchema extends Schema { class EmptySchema extends Schema {

3
packages/dynamite/dynamite/lib/src/models/tag.dart

@ -1,3 +1,4 @@
import 'package:dynamite/src/helpers/docs.dart';
import 'package:json_annotation/json_annotation.dart'; import 'package:json_annotation/json_annotation.dart';
import 'package:meta/meta.dart'; import 'package:meta/meta.dart';
@ -17,4 +18,6 @@ class Tag {
final String name; final String name;
final String? description; final String? description;
Iterable<String> get formattedDescription => descriptionToDocs(description);
} }

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

@ -288,14 +288,9 @@ class OpenAPIBuilder implements Builder {
), ),
); );
} }
final matchedTags = spec.tags?.where((final t) => t.name == tag);
b b
..name = '$classPrefix${isRootClient ? 'Client' : _clientName(tag)}' ..name = '$classPrefix${isRootClient ? 'Client' : _clientName(tag)}'
..docs.addAll( ..docs.addAll(spec.formattedTagsFor(tag))
_descriptionToDocs(
matchedTags != null && matchedTags.isNotEmpty ? matchedTags.single.description : null,
),
)
..methods.addAll( ..methods.addAll(
[ [
for (final t in tags.whereType<String>().where( for (final t in tags.whereType<String>().where(
@ -324,13 +319,7 @@ class OpenAPIBuilder implements Builder {
b b
..name = _toDartName(_filterMethodName(operationId, tag ?? '')) ..name = _toDartName(_filterMethodName(operationId, tag ?? ''))
..modifier = MethodModifier.async ..modifier = MethodModifier.async
..docs.addAll([ ..docs.addAll(operation.formattedDescription);
..._descriptionToDocs(operation.summary),
if (operation.summary != null && operation.description != null) ...[
'///',
],
..._descriptionToDocs(operation.description),
]);
if (operation.deprecated ?? false) { if (operation.deprecated ?? false) {
b.annotations.add(refer('Deprecated').call([refer("''")])); b.annotations.add(refer('Deprecated').call([refer("''")]));
} }
@ -871,14 +860,6 @@ String _toCamelCase(final String name) {
return result; return result;
} }
List<String> _descriptionToDocs(final String? description) => [
if (description != null && description.isNotEmpty) ...[
for (final line in description.split('\n')) ...[
'/// $line',
],
],
];
String _filterMethodName(final String operationId, final String tag) { String _filterMethodName(final String operationId, final String tag) {
final expandedTag = tag.split('/').toList(); final expandedTag = tag.split('/').toList();
final parts = operationId.split('-'); final parts = operationId.split('-');
@ -918,7 +899,7 @@ TypeResult resolveObject(
(final b) { (final b) {
b b
..name = '${state.prefix}$identifier' ..name = '${state.prefix}$identifier'
..docs.addAll(_descriptionToDocs(schema.description)) ..docs.addAll(schema.formattedDescription)
..abstract = true ..abstract = true
..implements.add( ..implements.add(
refer( refer(
@ -988,7 +969,7 @@ TypeResult resolveObject(
..name = _toDartName(propertyName) ..name = _toDartName(propertyName)
..returns = refer(result.nullableName) ..returns = refer(result.nullableName)
..type = MethodType.getter ..type = MethodType.getter
..docs.addAll(_descriptionToDocs(propertySchema.description)); ..docs.addAll(propertySchema.formattedDescription);
if (_toDartName(propertyName) != propertyName) { if (_toDartName(propertyName) != propertyName) {
b.annotations.add( b.annotations.add(
@ -1302,7 +1283,7 @@ TypeResult resolveType(
..name = fields[result.name] ..name = fields[result.name]
..returns = refer(result.nullableName) ..returns = refer(result.nullableName)
..type = MethodType.getter ..type = MethodType.getter
..docs.addAll(_descriptionToDocs(s.description)); ..docs.addAll(s.formattedDescription);
}, },
), ),
], ],

Loading…
Cancel
Save