Browse Source

fix(dynamite): Fix generating part directive and serializers conditionally

Signed-off-by: jld3103 <jld3103yt@gmail.com>
pull/1020/head
jld3103 1 year ago
parent
commit
5b2cff8cd7
No known key found for this signature in database
GPG Key ID: 9062417B9E8EB7B3
  1. 5
      packages/dynamite/dynamite/lib/src/builder/imports.dart
  2. 8
      packages/dynamite/dynamite/lib/src/builder/serializer.dart
  3. 14
      packages/dynamite/dynamite/lib/src/openapi_builder.dart

5
packages/dynamite/dynamite/lib/src/builder/imports.dart

@ -1,8 +1,9 @@
import 'package:build/build.dart'; import 'package:build/build.dart';
import 'package:code_builder/code_builder.dart'; import 'package:code_builder/code_builder.dart';
import 'package:dynamite/src/builder/state.dart';
import 'package:path/path.dart' as p; import 'package:path/path.dart' as p;
List<Spec> generateImports(final AssetId outputId) => [ List<Spec> generateImports(final AssetId outputId, final State state) => [
const Code('// ignore_for_file: camel_case_types'), const Code('// ignore_for_file: camel_case_types'),
const Code('// ignore_for_file: discarded_futures'), const Code('// ignore_for_file: discarded_futures'),
const Code('// ignore_for_file: public_member_api_docs'), const Code('// ignore_for_file: public_member_api_docs'),
@ -23,6 +24,8 @@ List<Spec> generateImports(final AssetId outputId) => [
Directive.import('package:meta/meta.dart'), Directive.import('package:meta/meta.dart'),
Directive.import('package:universal_io/io.dart'), Directive.import('package:universal_io/io.dart'),
const Code(''), const Code(''),
if (state.resolvedTypes.isNotEmpty) ...[
Directive.part(p.basename(outputId.changeExtension('.g.dart').path)), Directive.part(p.basename(outputId.changeExtension('.g.dart').path)),
const Code(''), const Code(''),
],
]; ];

8
packages/dynamite/dynamite/lib/src/builder/serializer.dart

@ -1,9 +1,7 @@
import 'package:code_builder/code_builder.dart'; import 'package:code_builder/code_builder.dart';
import 'package:dynamite/src/builder/state.dart'; import 'package:dynamite/src/builder/state.dart';
List<Spec> buildSerializer(final State state) { List<Spec> buildSerializer(final State state) => [
if (state.resolvedTypes.isNotEmpty) {
return [
const Code('// coverage:ignore-start'), const Code('// coverage:ignore-start'),
const Code('final Serializers _serializers = (Serializers().toBuilder()'), const Code('final Serializers _serializers = (Serializers().toBuilder()'),
...state.resolvedTypes ...state.resolvedTypes
@ -18,7 +16,3 @@ List<Spec> buildSerializer(final State state) {
), ),
const Code('// coverage:ignore-end'), const Code('// coverage:ignore-end'),
]; ];
}
return [];
}

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

@ -51,9 +51,7 @@ class OpenAPIBuilder implements Builder {
final state = State(); final state = State();
final output = ListBuilder<Spec>() final output = ListBuilder<Spec>();
..addAll(generateImports(outputId))
..addAll(generateClients(spec, state));
if (spec.components?.schemas != null) { if (spec.components?.schemas != null) {
for (final schema in spec.components!.schemas!.entries) { for (final schema in spec.components!.schemas!.entries) {
@ -86,9 +84,17 @@ class OpenAPIBuilder implements Builder {
} }
} }
// Imports need to be generated after everything else so we know if we need the local part directive,
// but they need to be added to the beginning of the output.
final clients = generateClients(spec, state);
final serializer = buildSerializer(state);
final imports = generateImports(outputId, state);
output output
..addAll(imports)
..addAll(clients)
..addAll(state.output) ..addAll(state.output)
..addAll(buildSerializer(state)); ..addAll(serializer);
final patterns = [ final patterns = [
RegExp( RegExp(

Loading…
Cancel
Save