Skip to content

Commit

Permalink
fix(dynamite): fix header part_file generation
Browse files Browse the repository at this point in the history
Signed-off-by: Nikolas Rimikis <[email protected]>
  • Loading branch information
Leptopoda committed Oct 27, 2023
1 parent 79e2f5e commit 3b6cbd9
Show file tree
Hide file tree
Showing 9 changed files with 785 additions and 49 deletions.
40 changes: 40 additions & 0 deletions packages/dynamite/dynamite/lib/src/builder/generate_schemas.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import 'package:code_builder/code_builder.dart';
import 'package:dynamite/src/builder/resolve_type.dart';
import 'package:dynamite/src/builder/state.dart';
import 'package:dynamite/src/helpers/dart_helpers.dart';
import 'package:dynamite/src/models/openapi.dart' as openapi;
import 'package:dynamite/src/models/type_result.dart';

Iterable<Spec> generateSchemas(
final openapi.OpenAPI spec,
final State state,
) sync* {
if (spec.components?.schemas != null) {
for (final schema in spec.components!.schemas!.entries) {
final identifier = toDartName(schema.key, uppercaseFirstCharacter: true);
if (schema.value.type == null && schema.value.ref == null && schema.value.ofs == null) {
yield TypeDef(
(final b) => b
..name = identifier
..definition = refer('dynamic'),
);
} else {
final result = resolveType(
spec,
state,
identifier,
schema.value,
);
if (result is TypeResultBase) {
yield TypeDef(
(final b) => b
..name = identifier
..definition = refer(result.name),
);
}
}
}
}

yield* state.output;
}
2 changes: 1 addition & 1 deletion packages/dynamite/dynamite/lib/src/builder/imports.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ List<Spec> generateImports(final AssetId outputId, final State state) => [
Directive.import('package:meta/meta.dart'),
Directive.import('package:universal_io/io.dart'),
const Code(''),
if (state.resolvedTypes.isNotEmpty) ...[
if (state.hasResolvedBuiltTypes) ...[
Directive.part(p.basename(outputId.changeExtension('.g.dart').path)),
const Code(''),
],
Expand Down
7 changes: 7 additions & 0 deletions packages/dynamite/dynamite/lib/src/builder/state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,11 @@ class State {
final output = <Spec>[];
final resolvedTypes = <TypeResult>{};
final resolvedInterfaces = <TypeResult>{};

/// Wether the state contains resolved types that need the built_value generator.
bool get hasResolvedBuiltTypes => resolvedTypes
.where(
(final type) => type is TypeResultEnum || (type is TypeResultObject && type.className != 'ContentString'),
)
.isNotEmpty;
}
51 changes: 6 additions & 45 deletions packages/dynamite/dynamite/lib/src/openapi_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@ import 'package:checked_yaml/checked_yaml.dart';
import 'package:code_builder/code_builder.dart';
import 'package:dart_style/dart_style.dart';
import 'package:dynamite/src/builder/client.dart';
import 'package:dynamite/src/builder/generate_schemas.dart';
import 'package:dynamite/src/builder/imports.dart';
import 'package:dynamite/src/builder/resolve_type.dart';
import 'package:dynamite/src/builder/serializer.dart';
import 'package:dynamite/src/builder/state.dart';
import 'package:dynamite/src/helpers/dart_helpers.dart';
import 'package:dynamite/src/models/openapi.dart' as openapi;
import 'package:dynamite/src/models/type_result.dart';
import 'package:version/version.dart';

class OpenAPIBuilder implements Builder {
Expand Down Expand Up @@ -58,50 +56,13 @@ class OpenAPIBuilder implements Builder {

final state = State();

final output = ListBuilder<Spec>();

if (spec.components?.schemas != null) {
for (final schema in spec.components!.schemas!.entries) {
final identifier = toDartName(schema.key, uppercaseFirstCharacter: true);
if (schema.value.type == null && schema.value.ref == null && schema.value.ofs == null) {
output.add(
TypeDef(
(final b) => b
..name = identifier
..definition = refer('dynamic'),
),
);
} else {
final result = resolveType(
spec,
state,
identifier,
schema.value,
);
if (result is TypeResultBase) {
output.add(
TypeDef(
(final b) => b
..name = identifier
..definition = refer(result.name),
),
);
}
}
}
}

// 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(serializer);
final output = ListBuilder<Spec>()
..addAll(generateClients(spec, state))
..addAll(generateSchemas(spec, state))
..addAll(buildSerializer(state))
..insertAll(0, generateImports(outputId, state));

final patterns = [
RegExp(
Expand Down
Loading

0 comments on commit 3b6cbd9

Please sign in to comment.