diff --git a/packages/dynamite/dynamite/lib/src/builder/imports.dart b/packages/dynamite/dynamite/lib/src/builder/imports.dart index c3a37d54..9e4f5e51 100644 --- a/packages/dynamite/dynamite/lib/src/builder/imports.dart +++ b/packages/dynamite/dynamite/lib/src/builder/imports.dart @@ -1,8 +1,9 @@ import 'package:build/build.dart'; import 'package:code_builder/code_builder.dart'; +import 'package:dynamite/src/builder/state.dart'; import 'package:path/path.dart' as p; -List generateImports(final AssetId outputId) => [ +List generateImports(final AssetId outputId, final State state) => [ const Code('// ignore_for_file: camel_case_types'), const Code('// ignore_for_file: discarded_futures'), const Code('// ignore_for_file: public_member_api_docs'), @@ -23,6 +24,8 @@ List generateImports(final AssetId outputId) => [ Directive.import('package:meta/meta.dart'), Directive.import('package:universal_io/io.dart'), const Code(''), - Directive.part(p.basename(outputId.changeExtension('.g.dart').path)), - const Code(''), + if (state.resolvedTypes.isNotEmpty) ...[ + Directive.part(p.basename(outputId.changeExtension('.g.dart').path)), + const Code(''), + ], ]; diff --git a/packages/dynamite/dynamite/lib/src/builder/serializer.dart b/packages/dynamite/dynamite/lib/src/builder/serializer.dart index 04674180..6118511c 100644 --- a/packages/dynamite/dynamite/lib/src/builder/serializer.dart +++ b/packages/dynamite/dynamite/lib/src/builder/serializer.dart @@ -1,9 +1,7 @@ import 'package:code_builder/code_builder.dart'; import 'package:dynamite/src/builder/state.dart'; -List buildSerializer(final State state) { - if (state.resolvedTypes.isNotEmpty) { - return [ +List buildSerializer(final State state) => [ const Code('// coverage:ignore-start'), const Code('final Serializers _serializers = (Serializers().toBuilder()'), ...state.resolvedTypes @@ -18,7 +16,3 @@ List buildSerializer(final State state) { ), const Code('// coverage:ignore-end'), ]; - } - - return []; -} diff --git a/packages/dynamite/dynamite/lib/src/openapi_builder.dart b/packages/dynamite/dynamite/lib/src/openapi_builder.dart index 1e46af25..5e42b4d7 100644 --- a/packages/dynamite/dynamite/lib/src/openapi_builder.dart +++ b/packages/dynamite/dynamite/lib/src/openapi_builder.dart @@ -51,9 +51,7 @@ class OpenAPIBuilder implements Builder { final state = State(); - final output = ListBuilder() - ..addAll(generateImports(outputId)) - ..addAll(generateClients(spec, state)); + final output = ListBuilder(); if (spec.components?.schemas != null) { 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 + ..addAll(imports) + ..addAll(clients) ..addAll(state.output) - ..addAll(buildSerializer(state)); + ..addAll(serializer); final patterns = [ RegExp(