Skip to content

Commit f741548

Browse files
authored
fix(autowire): 🐛 Autowiring override arguments do not work when c… (#220)
1 parent a1f3a0a commit f741548

File tree

4 files changed

+145
-45
lines changed

4 files changed

+145
-45
lines changed

lib/Autowire.js

+1-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import json5 from 'json5'
44
import { parse } from '@typescript-eslint/typescript-estree'
55
import Definition from './Definition'
66
import Reference from './Reference'
7-
import ServiceFile from './ServiceFile'
87
import AutowireIdentifier from './AutowireIdentifier'
98
import ContainerDefaultDirMustBeSet from './Exception/ContainerDefaultDirMustBeSet'
109
import PassConfig from './PassConfig'
@@ -29,6 +28,7 @@ export default class Autowire {
2928
} catch (e) {
3029
this._tsConfigPaths = null
3130
}
31+
this._container.autowire = this
3232
}
3333

3434
_ensureContainerIsValidForAutowire (container) {
@@ -120,9 +120,6 @@ export default class Autowire {
120120
promises.push(this._executeFilePath(filePath))
121121
}
122122
await Promise.all(promises)
123-
if (this._serviceFile instanceof ServiceFile) {
124-
await this._serviceFile.generateFromContainer(this._container)
125-
}
126123
this._container.addCompilerPass(
127124
new AutowireOverridePass(),
128125
PassConfig.TYPE_BEFORE_OPTIMIZATION

lib/ContainerBuilder.js

+16
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import WrongDefinitionException from './Exception/WrongDefinitionException'
1212
import FrozenContainerException from './Exception/FrozenContainerException'
1313
import RootDirectoryNotFound from './Exception/RootDirectoryNotFound'
1414
import RootDirectoryMustBeAbsolute from './Exception/RootDirectoryMustBeAbsolute'
15+
import ServiceFile from './ServiceFile'
1516

1617
class ContainerBuilder {
1718
/**
@@ -32,6 +33,18 @@ class ContainerBuilder {
3233
this._instanceManager = undefined
3334
this._containerReferenceAsService = containerReferenceAsService
3435
this._defaultDir = defaultDir
36+
this._autowire = null
37+
}
38+
39+
/**
40+
* @param {Autowire} autowire
41+
*/
42+
set autowire (autowire) {
43+
this._autowire = autowire
44+
}
45+
46+
get autowire () {
47+
return this._autowire
3548
}
3649

3750
ensureDirectoryExists (directory) {
@@ -163,6 +176,9 @@ class ContainerBuilder {
163176

164177
async compile () {
165178
await new Compiler(this).run()
179+
if (this._autowire?.serviceFile instanceof ServiceFile) {
180+
await this._autowire.serviceFile.generateFromContainer(this)
181+
}
166182
}
167183

168184
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
services:
2+
App.FooBarAutowireOverride:
3+
class: './FooBarAutowireOverride'
4+
override_arguments:
5+
- '@CiAdapter'
6+
- '@SomeService'
7+
- '@AnotherService'
8+
9+
App.AnotherFooBarAutowireOverride:
10+
class: './AnotherFooBarAutowireOverride'
11+
override_arguments:
12+
- '@FooAdapter'
13+
- '@SomeService'
14+
- '@AnotherService'

test/node-dependency-injection/lib-ts/Autowire.spec.js

+114-41
Original file line numberDiff line numberDiff line change
@@ -38,51 +38,121 @@ describe('AutowireTS', () => {
3838
const excludedServiceMessage = 'The service ExcludedService is not registered'
3939
const inFolderExcludedMessage = 'The service InFolderExcludedService is not registered'
4040

41-
it("should not override single class with autowiring if not exists", async () => {
42-
const configFile = path.join(
43-
__dirname,
44-
'..',
45-
'..',
46-
resourcesTsFolder,
47-
'Autowire-Override',
48-
'config',
49-
'services-not-exists.yaml'
50-
)
51-
const cb = new ContainerBuilder()
52-
const loader = new YamlFileLoader(cb)
53-
await loader.load(configFile)
54-
await cb.compile()
41+
describe("Autowiring override arguments", () => {
42+
const folder = 'Autowire-Override';
43+
44+
it("should override arguments be visible after compile container and dump file", async () => {
45+
// Arrange.
46+
const configFile = path.join(
47+
__dirname,
48+
'..',
49+
'..',
50+
resourcesTsFolder,
51+
folder,
52+
'config',
53+
'services-noimports.yaml'
54+
)
55+
const dir = path.join(__dirname, '..', '..', resourcesTsFolder, folder, 'src')
56+
const container = new ContainerBuilder(false, dir)
57+
const loader = new YamlFileLoader(container)
58+
const autowire = new Autowire(container)
59+
const dumpPath = `${dumpServicesPath}-override.yaml`
60+
autowire.serviceFile = new ServiceFile(dumpPath)
61+
await loader.load(configFile)
62+
await autowire.process()
63+
await container.compile()
64+
65+
const containerFromDump = new ContainerBuilder(false, dir)
66+
const loaderFromDump = new YamlFileLoader(containerFromDump)
67+
68+
// Act.
69+
await loaderFromDump.load(dumpPath)
70+
71+
// Act.
72+
const actual = containerFromDump.get(FooBarAutowireOverride)
73+
const actualAnother = containerFromDump.get(AnotherFooBarAutowireOverride)
74+
75+
// Assert.
76+
assert.equal(actual.getString(), "ci")
77+
assert.equal(actualAnother.getString(), "foo")
78+
});
79+
80+
it("should override arguments be visible after compile container", async () => {
81+
// Arrange.
82+
const dir = path.join(__dirname, '..', '..', resourcesTsFolder, folder, 'src')
83+
const container = new ContainerBuilder(false, dir)
84+
const autowire = new Autowire(container)
85+
const loader = new YamlFileLoader(container);
86+
const configFile = path.join(
87+
__dirname,
88+
'..',
89+
'..',
90+
resourcesTsFolder,
91+
folder,
92+
'config',
93+
'services-noimports.yaml'
94+
)
95+
await loader.load(configFile)
96+
await autowire.process()
97+
await container.compile()
98+
99+
// Act.
100+
const actual = container.get(FooBarAutowireOverride)
101+
const actualAnother = container.get(AnotherFooBarAutowireOverride)
102+
103+
// Assert.
104+
assert.equal(actual.getString(), "ci")
105+
assert.equal(actualAnother.getString(), "foo")
106+
});
55107

56-
// Act.
57-
const actual = cb.get(FooBarAutowireOverride)
108+
it("should not override single class with autowiring if not exists", async () => {
109+
// Arrange.
110+
const configFile = path.join(
111+
__dirname,
112+
'..',
113+
'..',
114+
resourcesTsFolder,
115+
folder,
116+
'config',
117+
'services-not-exists.yaml'
118+
)
119+
const cb = new ContainerBuilder()
120+
const loader = new YamlFileLoader(cb)
121+
await loader.load(configFile)
122+
await cb.compile()
58123

59-
// Assert.
60-
assert.isUndefined(actual.adapter)
61-
});
62-
63-
it("should override single class with autowiring", async () => {
64-
const configFile = path.join(
65-
__dirname,
66-
'..',
67-
'..',
68-
resourcesTsFolder,
69-
'Autowire-Override',
70-
'config',
71-
'services.yaml'
72-
)
73-
const cb = new ContainerBuilder()
74-
const loader = new YamlFileLoader(cb)
75-
await loader.load(configFile)
76-
await cb.compile()
124+
// Act.
125+
const actual = cb.get(FooBarAutowireOverride)
126+
127+
// Assert.
128+
assert.isUndefined(actual.adapter)
129+
});
77130

78-
// Act.
79-
const actual = cb.get(FooBarAutowireOverride)
80-
const actualAnother = cb.get(AnotherFooBarAutowireOverride)
131+
it("should override single class with autowiring", async () => {
132+
// Arrange.
133+
const configFile = path.join(
134+
__dirname,
135+
'..',
136+
'..',
137+
resourcesTsFolder,
138+
folder,
139+
'config',
140+
'services.yaml'
141+
)
142+
const cb = new ContainerBuilder()
143+
const loader = new YamlFileLoader(cb)
144+
await loader.load(configFile)
145+
await cb.compile()
81146

82-
// Assert.
83-
assert.equal(actual.getString(), "ci")
84-
assert.equal(actualAnother.getString(), "bar")
85-
});
147+
// Act.
148+
const actual = cb.get(FooBarAutowireOverride)
149+
const actualAnother = cb.get(AnotherFooBarAutowireOverride)
150+
151+
// Assert.
152+
assert.equal(actual.getString(), "ci")
153+
assert.equal(actualAnother.getString(), "bar")
154+
});
155+
})
86156

87157
it('should get service file when was properly set', () => {
88158
// Arrange.
@@ -124,6 +194,7 @@ describe('AutowireTS', () => {
124194
const dumpPath = `${dumpServicesPath}.yaml`
125195
autowire.serviceFile = new ServiceFile(dumpPath, true)
126196
await autowire.process()
197+
await container.compile()
127198
const containerDump = new ContainerBuilder(false)
128199
const loader = new YamlFileLoader(containerDump)
129200

@@ -188,6 +259,7 @@ describe('AutowireTS', () => {
188259
await autowire.process()
189260
const containerDump = new ContainerBuilder(false, dir)
190261
const loader = new JsonFileLoader(containerDump)
262+
await container.compile()
191263

192264
// Act.
193265
await loader.load(dumpPath)
@@ -216,6 +288,7 @@ describe('AutowireTS', () => {
216288
await autowire.process()
217289
const containerDump = new ContainerBuilder(false, dir)
218290
const loader = new JsFileLoader(containerDump)
291+
await container.compile()
219292

220293
// Act.
221294
await loader.load(dumpPath)

0 commit comments

Comments
 (0)