Skip to content

Commit

Permalink
Merge pull request #992 from nextcloud/refactor/dynamite
Browse files Browse the repository at this point in the history
refactor(dynamite): add default values as described by openapi
  • Loading branch information
Leptopoda authored Oct 19, 2023
2 parents 6245160 + 7bae1ab commit a160696
Show file tree
Hide file tree
Showing 17 changed files with 140 additions and 96 deletions.
2 changes: 1 addition & 1 deletion packages/dynamite/dynamite/lib/src/builder/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ Iterable<Method> buildTags(
).forEach(code.writeln);

final operationParameters = ListBuilder<Parameter>();
final annotations = operation.deprecated ?? false ? refer('Deprecated').call([refer("''")]) : null;
final annotations = operation.deprecated ? refer('Deprecated').call([refer("''")]) : null;
var returnDataType = 'void';
var returnHeadersType = 'void';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ Iterable<String> deserializeProperty(
state,
'${identifier}_${toDartName(propertyName, uppercaseFirstCharacter: true)}',
propertySchema,
nullable: isDartParameterNullable(schema.required?.contains(propertyName), propertySchema),
nullable: isDartParameterNullable(schema.required.contains(propertyName), propertySchema),
);

yield "case '$propertyName':";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ TypeResultObject resolveInterface(
'${identifier}_${toDartName(propertyName, uppercaseFirstCharacter: true)}',
propertySchema,
nullable: isDartParameterNullable(
schema.required?.contains(propertyName),
schema.required.contains(propertyName),
propertySchema,
),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ Iterable<String> resolveMimeTypeEncode(
case 'application/x-www-form-urlencoded':
final dartParameterRequired = isRequired(
operation.requestBody!.required,
mediaType.schema?.$default,
mediaType.schema,
);
b.add(
Parameter(
Expand All @@ -100,7 +100,7 @@ Iterable<String> resolveMimeTypeEncode(
case 'application/octet-stream':
final dartParameterRequired = isRequired(
operation.requestBody!.required,
mediaType.schema?.$default,
mediaType.schema,
);
b.add(
Parameter(
Expand Down
10 changes: 5 additions & 5 deletions packages/dynamite/dynamite/lib/src/helpers/dynamite.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@ String filterMethodName(final String operationId, final String tag) {
String clientName(final String tag) => '${toDartName(tag, uppercaseFirstCharacter: true)}Client';

bool isDartParameterNullable(
final bool? required,
final bool required,
final openapi.Schema? schema,
) =>
(!(required ?? false) && schema?.$default == null) || (schema?.nullable ?? false);
(!required && schema?.$default == null) || (schema?.nullable ?? false);

bool isRequired(
final bool? required,
final dynamic default_,
final bool required,
final openapi.Schema? schema,
) =>
(required ?? false) && default_ == null;
required && schema?.$default == null;

int sortRequiredParameters(final openapi.Parameter a, final openapi.Parameter b) {
if (a.isDartRequired != b.isDartRequired) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ abstract class Header implements Built<Header, HeaderBuilder> {
@BuiltValueField(compare: false)
String? get description;

bool? get required;
bool get required;

Schema? get schema;

@BuiltValueHook(finalizeBuilder: true)
static void _defaults(final HeaderBuilder b) {
b.required ??= false;
}
}
26 changes: 15 additions & 11 deletions packages/dynamite/dynamite/lib/src/models/openapi/header.g.dart

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

13 changes: 12 additions & 1 deletion packages/dynamite/dynamite/lib/src/models/openapi/open_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ abstract class OpenAPI implements Built<OpenAPI, OpenAPIBuilder> {

Info get info;

BuiltList<Server>? get servers;
BuiltList<Server> get servers;

BuiltList<BuiltMap<String, BuiltList<String>>>? get security;

Expand All @@ -33,6 +33,17 @@ abstract class OpenAPI implements Built<OpenAPI, OpenAPIBuilder> {

bool get hasAnySecurity => components?.securitySchemes?.isNotEmpty ?? false;

@BuiltValueHook(finalizeBuilder: true)
static void _defaults(final OpenAPIBuilder b) {
if (b.servers.isEmpty) {
b.servers.add(
Server(
(final b) => b.url = '/',
),
);
}
}

Iterable<String> formattedTagsFor(final String? tag) sync* {
final matchedTags = tags?.where((final t) => t.name == tag);

Expand Down
26 changes: 15 additions & 11 deletions packages/dynamite/dynamite/lib/src/models/openapi/open_api.g.dart

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

Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ abstract class Operation implements Built<Operation, OperationBuilder> {
@BuiltValueField(compare: false)
String? get description;

bool? get deprecated;
bool get deprecated;

BuiltList<String>? get tags;

Expand All @@ -35,6 +35,11 @@ abstract class Operation implements Built<Operation, OperationBuilder> {

BuiltList<BuiltMap<String, BuiltList<String>>>? get security;

@BuiltValueHook(finalizeBuilder: true)
static void _defaults(final OperationBuilder b) {
b.deprecated ??= false;
}

Iterable<String> formattedDescription(
final String methodName, {
final bool isRawRequest = false,
Expand Down
24 changes: 12 additions & 12 deletions packages/dynamite/dynamite/lib/src/models/openapi/operation.g.dart

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

Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,16 @@ abstract class Parameter implements Built<Parameter, ParameterBuilder> {
@BuiltValueField(compare: false)
String? get description;

bool? get required;
bool get required;

Schema? get schema;

bool get isDartRequired => isRequired(required, schema?.$default);
@BuiltValueHook(finalizeBuilder: true)
static void _defaults(final ParameterBuilder b) {
b.required ??= false;
}

bool get isDartRequired => isRequired(required, schema);

String get formattedDescription {
final name = toDartName(this.name);
Expand Down
Loading

0 comments on commit a160696

Please sign in to comment.