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

fix: non-reconstructable services are erased when imported #1144

Open
freshgum-bubbles opened this issue Jul 15, 2023 · 1 comment
Open

fix: non-reconstructable services are erased when imported #1144

freshgum-bubbles opened this issue Jul 15, 2023 · 1 comment
Labels
status: needs triage Issues which needs to be reproduced to be verified report. type: fix Issues describing a broken feature.

Comments

@freshgum-bubbles
Copy link

freshgum-bubbles commented Jul 15, 2023

Description

I found a bug in my fork of TypeDI,
and I thought I'd be a good neighbour by also posting it here.

With the current inheritance code, when a value is imported from
the default container, its value is always set to EMPTY_VALUE.
This breaks non-reconstructable services, which lack either a type
or a factory which allows for re-constructing the service.

Minimal code-snippet showcasing the problem

import 'reflect-metadata';
import { Container, ContainerInstance, Token } from 'typedi';

const myContainer = new ContainerInstance('my-container');

// Set a value on the default container:
const name = new Token<string>();

// Set it with a string value.
// This makes it non-reconstructable, as when it's imported and
// its value is erased, we have no way to reconstruct it (via, e.g.
// a factory or a constructor)
Container.set({ id: name, value: 'Joanna' });

// This breaks:
myContainer.get(name);

StackBlitz Demo

Full error:

Cannot instantiate the requested value for the "Token<UNSET_NAME>" identifier. The related metadata doesn't contain a factory or a type to instantiate.

Expected behavior

If a service can't be reconstructed from a type or a factory, it shouldn't
be erased when imported from the default container.

Actual behavior

The value is always erased.

The problem is caused by this line:

clonedService.value = EMPTY_VALUE;

@freshgum-bubbles freshgum-bubbles added status: needs triage Issues which needs to be reproduced to be verified report. type: fix Issues describing a broken feature. labels Jul 15, 2023
@freshgum-bubbles
Copy link
Author

freshgum-bubbles commented Jul 15, 2023

I fixed this in my fork by doing the following:

https://github.com/freshgum-bubbles/typedi/blob/d69e6f45afda817378adc2a35aad9d490c418b2a/src/container-instance.class.ts#L346-L359

(gah, it won't embed)

I'd be more than happy to open a PR for this, but I'd like
the go-ahead from yourselves (as this project seems to be abandoned).

Man, you guys work slowly :P

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: needs triage Issues which needs to be reproduced to be verified report. type: fix Issues describing a broken feature.
Development

No branches or pull requests

1 participant