From 0c16f9e4a5aad8117e0b7a625397216bfbc0f0e2 Mon Sep 17 00:00:00 2001 From: leonsenft Date: Tue, 20 Nov 2018 15:12:50 -0800 Subject: [PATCH] fix(Core): correctly treat optional pipe dependencies as optional Previously the `@Optional()` annotation used to mark injected dependencies as optional was ignored on pipes, meaning a "No provider found ..." error would be thrown when attempting to instantiate a pipe with a missing optional dependency. Fixes https://github.com/dart-lang/angular/issues/1665. PiperOrigin-RevId: 222314430 --- .../1665_optional_pipe_dependency_test.dart | 33 +++++++++++++++++++ angular/CHANGELOG.md | 6 ++++ .../compiler/view_compiler/compile_view.dart | 2 +- 3 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 _tests/test/regression/1665_optional_pipe_dependency_test.dart diff --git a/_tests/test/regression/1665_optional_pipe_dependency_test.dart b/_tests/test/regression/1665_optional_pipe_dependency_test.dart new file mode 100644 index 0000000000..e9be54a9d6 --- /dev/null +++ b/_tests/test/regression/1665_optional_pipe_dependency_test.dart @@ -0,0 +1,33 @@ +@TestOn('browser') +import 'package:test/test.dart'; +import 'package:angular/angular.dart'; +import 'package:angular_test/angular_test.dart'; + +import '1665_optional_pipe_dependency_test.template.dart' as ng; + +void main() { + tearDown(disposeAnyRunningTest); + + test('a missing @Optional() pipe dependency does not throw', () async { + var testBed = NgTestBed.forComponent(ng.TestComponentNgFactory); + expect(testBed.create(), completes); + }); +} + +abstract class TestService {} + +@Pipe('test') +class TestPipe { + TestPipe(@Optional() TestService _); + + dynamic transform(dynamic value) => value; +} + +@Component( + selector: 'test', + template: '{{value | test}}', + pipes: [TestPipe], +) +class TestComponent { + var value = 'Hello world!'; +} diff --git a/angular/CHANGELOG.md b/angular/CHANGELOG.md index 48450a676d..4d3205e709 100644 --- a/angular/CHANGELOG.md +++ b/angular/CHANGELOG.md @@ -84,7 +84,13 @@ Previously, this would build successfully but emit code that instantiated the directive with `dynamic` in place of the private type. +* [#1665][]: `@Optional()` dependencies of pipes are now correctly treated as + optional. Previously the annotation was ignored, and attempting to + instantiate the pipe with a missing optional dependency would throw an error + for the missing dependency. + [#1653]: https://github.com/dart-lang/angular/issues/1653 +[#1665]: https://github.com/dart-lang/angular/issues/1665 ## 5.1.0 diff --git a/angular/lib/src/compiler/view_compiler/compile_view.dart b/angular/lib/src/compiler/view_compiler/compile_view.dart index 7c3ae96e7b..ef030e5433 100755 --- a/angular/lib/src/compiler/view_compiler/compile_view.dart +++ b/angular/lib/src/compiler/view_compiler/compile_view.dart @@ -1158,7 +1158,7 @@ class CompileView implements AppViewBuilder { return o.ReadClassMemberExpr('ref'); } usesInjectorGet = true; - return injectFromViewParentInjector(this, diDep.token, false); + return injectFromViewParentInjector(this, diDep.token, diDep.isOptional); }).toList(); final pipeInstance = storage.allocate( name,