Skip to content

Commit

Permalink
Merge pull request #1426 from nextcloud/refactor/dynamite/reduce_allO…
Browse files Browse the repository at this point in the history
…f_interfaces

refactor(dynamite)!: remove unecessary allOf interfaces
  • Loading branch information
Leptopoda authored Jan 9, 2024
2 parents 40ccec5 + 62fe418 commit 3b37ed7
Show file tree
Hide file tree
Showing 16 changed files with 1,141 additions and 1,517 deletions.
5 changes: 1 addition & 4 deletions packages/dynamite/dynamite/example/lib/petstore.openapi.dart
Original file line number Diff line number Diff line change
Expand Up @@ -332,13 +332,10 @@ abstract class NewPet implements $NewPetInterface, Built<NewPet, NewPetBuilder>
}

@BuiltValue(instantiable: false)
abstract interface class $Pet_1Interface {
abstract interface class $PetInterface implements $NewPetInterface {
int get id;
}

@BuiltValue(instantiable: false)
abstract interface class $PetInterface implements $NewPetInterface, $Pet_1Interface {}

abstract class Pet implements $PetInterface, Built<Pet, PetBuilder> {
/// Creates a new Pet object using the builder pattern.
factory Pet([void Function(PetBuilder)? b]) = _$Pet;
Expand Down
55 changes: 24 additions & 31 deletions packages/dynamite/dynamite/example/lib/petstore.openapi.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

56 changes: 0 additions & 56 deletions packages/dynamite/dynamite/lib/src/builder/resolve_interface.dart
Original file line number Diff line number Diff line change
@@ -1,65 +1,9 @@
import 'package:built_collection/built_collection.dart';
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/built_value.dart';
import 'package:dynamite/src/helpers/dart_helpers.dart';
import 'package:dynamite/src/helpers/dynamite.dart';
import 'package:dynamite/src/models/openapi.dart' as openapi;
import 'package:dynamite/src/models/type_result.dart';

TypeResultObject resolveInterface(
openapi.OpenAPI spec,
State state,
String identifier,
openapi.Schema schema,
) {
final properties = schema.properties?.entries;

if (properties == null || properties.isEmpty) {
throw StateError('The schema must have a non empty properties field.');
}

final result = TypeResultObject(
identifier,
);

if (state.resolvedInterfaces.add(result)) {
final $interface = buildInterface(
identifier,
methods: BuiltList.build((b) {
b.addAll(
properties.map((property) {
final propertyName = property.key;
final propertySchema = property.value;

final result = resolveType(
spec,
state,
'${identifier}_${toDartName(propertyName, uppercaseFirstCharacter: true)}',
propertySchema,
nullable: isDartParameterNullable(
schema.required.contains(propertyName),
propertySchema,
),
);

return generateProperty(
result,
propertyName,
propertySchema.formattedDescription,
);
}),
);
}),
);

state.output.add($interface);
}

return result;
}

Method generateProperty(
TypeResult type,
String propertyName,
Expand Down
32 changes: 26 additions & 6 deletions packages/dynamite/dynamite/lib/src/builder/resolve_ofs.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import 'package:dynamite/src/builder/resolve_interface.dart';
import 'package:dynamite/src/builder/resolve_type.dart';
import 'package:dynamite/src/builder/state.dart';
import 'package:dynamite/src/helpers/built_value.dart';
import 'package:dynamite/src/helpers/dart_helpers.dart';
import 'package:dynamite/src/helpers/dynamite.dart';
import 'package:dynamite/src/models/openapi.dart' as openapi;
import 'package:dynamite/src/models/type_result.dart';

Expand Down Expand Up @@ -76,13 +78,31 @@ TypeResult resolveAllOf(

for (final s in schema.allOf!) {
if (s.properties != null) {
final interfaceClass = resolveInterface(
spec,
state,
'${identifier}_${schema.allOf!.indexOf(s)}',
s,
final properties = s.properties!.entries;

methods.addAll(
properties.map((property) {
final propertyName = property.key;
final propertySchema = property.value;

final result = resolveType(
spec,
state,
'${identifier}_${toDartName(propertyName, uppercaseFirstCharacter: true)}',
propertySchema,
nullable: isDartParameterNullable(
s.required.contains(propertyName),
propertySchema,
),
);

return generateProperty(
result,
propertyName,
propertySchema.formattedDescription,
);
}),
);
interfaces.add(interfaceClass);
} else {
final object = resolveType(
spec,
Expand Down
24 changes: 5 additions & 19 deletions packages/dynamite/dynamite_end_to_end_test/lib/all_of.openapi.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,13 @@ import 'package:meta/meta.dart' as _i1;
part 'all_of.openapi.g.dart';

@BuiltValue(instantiable: false)
abstract interface class $ObjectAllOf_0Interface {
abstract interface class $ObjectAllOfInterface {
@BuiltValueField(wireName: 'attribute1-allOf')
String get attribute1AllOf;
}

@BuiltValue(instantiable: false)
abstract interface class $ObjectAllOf_1Interface {
@BuiltValueField(wireName: 'attribute2-allOf')
String get attribute2AllOf;
}

@BuiltValue(instantiable: false)
abstract interface class $ObjectAllOfInterface implements $ObjectAllOf_0Interface, $ObjectAllOf_1Interface {}

/// All of with objects only.
abstract class ObjectAllOf implements $ObjectAllOfInterface, Built<ObjectAllOf, ObjectAllOfBuilder> {
/// Creates a new ObjectAllOf object using the builder pattern.
Expand All @@ -52,14 +45,11 @@ abstract class ObjectAllOf implements $ObjectAllOfInterface, Built<ObjectAllOf,
}

@BuiltValue(instantiable: false)
abstract interface class $OneObjectAllOf_0Interface {
abstract interface class $OneObjectAllOfInterface {
@BuiltValueField(wireName: 'attribute-allOf')
String get attributeAllOf;
}

@BuiltValue(instantiable: false)
abstract interface class $OneObjectAllOfInterface implements $OneObjectAllOf_0Interface {}

/// All of with one object value.
abstract class OneObjectAllOf implements $OneObjectAllOfInterface, Built<OneObjectAllOf, OneObjectAllOfBuilder> {
/// Creates a new OneObjectAllOf object using the builder pattern.
Expand Down Expand Up @@ -111,15 +101,11 @@ abstract class PrimitiveAllOf implements $PrimitiveAllOfInterface, Built<Primiti
}

@BuiltValue(instantiable: false)
abstract interface class $MixedAllOf_1Interface {
@BuiltValueField(wireName: 'attribute-allOf')
String get attributeAllOf;
}

@BuiltValue(instantiable: false)
abstract interface class $MixedAllOfInterface implements $MixedAllOf_1Interface {
abstract interface class $MixedAllOfInterface {
@BuiltValueField(wireName: 'String')
String get string;
@BuiltValueField(wireName: 'attribute-allOf')
String get attributeAllOf;
}

/// All of with object and primitive value.
Expand Down
Loading

0 comments on commit 3b37ed7

Please sign in to comment.