Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a clone method to request builders to implement copyWith #9

Merged
merged 6 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/kiota_abstractions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ part 'src/api_client_builder.dart';
part 'src/base_request_builder.dart';
part 'src/date_only.dart';
part 'src/error_mappings.dart';
part 'src/extensions/base_request_builder_extensions.dart';
part 'src/extensions/date_only_extensions.dart';
part 'src/extensions/map_extensions.dart';
part 'src/extensions/request_information_extensions.dart';
Expand Down
5 changes: 4 additions & 1 deletion lib/src/base_request_builder.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
part of '../kiota_abstractions.dart';

/// Base class for all request builders.
abstract class BaseRequestBuilder {
abstract class BaseRequestBuilder<T extends BaseRequestBuilder<T>> {
BaseRequestBuilder(
this.requestAdapter,
this.urlTemplate,
Expand All @@ -16,4 +16,7 @@ abstract class BaseRequestBuilder {

/// Url template to use to build the URL for the current request builder.
String urlTemplate;

/// Clones the current request builder.
T clone();
}
12 changes: 12 additions & 0 deletions lib/src/extensions/base_request_builder_extensions.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
part of '../../kiota_abstractions.dart';

extension BaseRequestBuilderExtensions<T extends BaseRequestBuilder<T>>
on BaseRequestBuilder<T> {
/// Clones the current request builder using [clone] and sets the given
/// [rawUrl] as the url to use.
///
/// This utilizes the [RequestInformation.rawUrlKey] to store the raw url
/// in the [pathParameters].
T withUrl(String rawUrl) => clone()
..pathParameters.addOrReplace(RequestInformation.rawUrlKey, rawUrl);
}
58 changes: 58 additions & 0 deletions test/request_builder_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import 'package:kiota_abstractions/kiota_abstractions.dart';
import 'package:mockito/annotations.dart';
import 'package:test/test.dart';

import 'request_builder_test.mocks.dart';

class SampleRequestBuilder extends BaseRequestBuilder<SampleRequestBuilder> {
SampleRequestBuilder(
this.id,
super.requestAdapter,
super.urlTemplate,
super.pathParameters,
);

final int id;

@override
SampleRequestBuilder clone() {
return SampleRequestBuilder(
id,
requestAdapter,
urlTemplate,
{...pathParameters},
);
}
}

@GenerateMocks([RequestAdapter])
void main() {
test('BaseRequestBuilder.withUrl', () {
final mockRequestAdapter = MockRequestAdapter();

final requestBuilder = SampleRequestBuilder(
1,
mockRequestAdapter,
'https://graph.microsoft.com/v1.0/users/{id}',
{'id': '1'},
);

final newRequestBuilder =
requestBuilder.withUrl('https://graph.microsoft.com/v2.0/users/123');

expect(newRequestBuilder.id, equals(1));
expect(
newRequestBuilder.pathParameters,
equals({
'id': '1',
'request-raw-url': 'https://graph.microsoft.com/v2.0/users/123',
}),
);

// make sure the original requestBuilder is not modified
expect(
requestBuilder.pathParameters,
equals({'id': '1'}),
);
});
}
158 changes: 158 additions & 0 deletions test/request_builder_test.mocks.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
// Mocks generated by Mockito 5.4.4 from annotations
// in kiota_abstractions/test/request_builder_test.dart.
// Do not manually edit this file.

// ignore_for_file: no_leading_underscores_for_library_prefixes
import 'dart:async' as _i3;

import 'package:kiota_abstractions/kiota_abstractions.dart' as _i2;
import 'package:mockito/mockito.dart' as _i1;

// ignore_for_file: type=lint
// ignore_for_file: avoid_redundant_argument_values
// ignore_for_file: avoid_setters_without_getters
// ignore_for_file: comment_references
// ignore_for_file: deprecated_member_use
// ignore_for_file: deprecated_member_use_from_same_package
// ignore_for_file: implementation_imports
// ignore_for_file: invalid_use_of_visible_for_testing_member
// ignore_for_file: prefer_const_constructors
// ignore_for_file: unnecessary_parenthesis
// ignore_for_file: camel_case_types
// ignore_for_file: subtype_of_sealed_class

class _FakeSerializationWriterFactory_0 extends _i1.SmartFake
implements _i2.SerializationWriterFactory {
_FakeSerializationWriterFactory_0(
Object parent,
Invocation parentInvocation,
) : super(
parent,
parentInvocation,
);
}

/// A class which mocks [RequestAdapter].
///
/// See the documentation for Mockito's code generation for more information.
class MockRequestAdapter extends _i1.Mock implements _i2.RequestAdapter {
MockRequestAdapter() {
_i1.throwOnMissingStub(this);
}

@override
set baseUrl(String? _baseUrl) => super.noSuchMethod(
Invocation.setter(
#baseUrl,
_baseUrl,
),
returnValueForMissingStub: null,
);

@override
_i2.SerializationWriterFactory get serializationWriterFactory =>
(super.noSuchMethod(
Invocation.getter(#serializationWriterFactory),
returnValue: _FakeSerializationWriterFactory_0(
this,
Invocation.getter(#serializationWriterFactory),
),
) as _i2.SerializationWriterFactory);

@override
_i3.Future<ModelType?> send<ModelType extends _i2.Parsable>(
_i2.RequestInformation? requestInfo,
_i2.ParsableFactory<ModelType>? factory, [
Map<String, _i2.ParsableFactory<_i2.Parsable>>? errorMapping,
]) =>
(super.noSuchMethod(
Invocation.method(
#send,
[
requestInfo,
factory,
errorMapping,
],
),
returnValue: _i3.Future<ModelType?>.value(),
) as _i3.Future<ModelType?>);

@override
_i3.Future<Iterable<ModelType>?>
sendCollection<ModelType extends _i2.Parsable>(
_i2.RequestInformation? requestInfo,
_i2.ParsableFactory<ModelType>? factory, [
Map<String, _i2.ParsableFactory<_i2.Parsable>>? errorMapping,
]) =>
(super.noSuchMethod(
Invocation.method(
#sendCollection,
[
requestInfo,
factory,
errorMapping,
],
),
returnValue: _i3.Future<Iterable<ModelType>?>.value(),
) as _i3.Future<Iterable<ModelType>?>);

@override
_i3.Future<ModelType?> sendPrimitive<ModelType>(
_i2.RequestInformation? requestInfo, [
Map<String, _i2.ParsableFactory<_i2.Parsable>>? errorMapping,
]) =>
(super.noSuchMethod(
Invocation.method(
#sendPrimitive,
[
requestInfo,
errorMapping,
],
),
returnValue: _i3.Future<ModelType?>.value(),
) as _i3.Future<ModelType?>);

@override
_i3.Future<Iterable<ModelType>?> sendPrimitiveCollection<ModelType>(
_i2.RequestInformation? requestInfo, [
Map<String, _i2.ParsableFactory<_i2.Parsable>>? errorMapping,
]) =>
(super.noSuchMethod(
Invocation.method(
#sendPrimitiveCollection,
[
requestInfo,
errorMapping,
],
),
returnValue: _i3.Future<Iterable<ModelType>?>.value(),
) as _i3.Future<Iterable<ModelType>?>);

@override
_i3.Future<void> sendNoContent(
_i2.RequestInformation? requestInfo, [
Map<String, _i2.ParsableFactory<_i2.Parsable>>? errorMapping,
]) =>
(super.noSuchMethod(
Invocation.method(
#sendNoContent,
[
requestInfo,
errorMapping,
],
),
returnValue: _i3.Future<void>.value(),
returnValueForMissingStub: _i3.Future<void>.value(),
) as _i3.Future<void>);

@override
_i3.Future<T?> convertToNativeRequest<T>(
_i2.RequestInformation? requestInfo) =>
(super.noSuchMethod(
Invocation.method(
#convertToNativeRequest,
[requestInfo],
),
returnValue: _i3.Future<T?>.value(),
) as _i3.Future<T?>);
}