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

Use default class property for @optional injected properties #1467

Merged
merged 6 commits into from
Oct 17, 2023

Conversation

haaijman-imagem
Copy link
Contributor

@haaijman-imagem haaijman-imagem commented Jun 23, 2022

Description

Currently the @optional tag overrides class properties defined with typescript-style default values:

@injectable()
class Ninja {
    @inject(Weapon) @optional() weapon: Weapon = new Katana();

    ....
}

this currently sets the weapon property to undefined if no Weapon is present in the container. My proposed change allows does not override the default and results in the weapon property being set to new Katana().

(Default values in the constructor already work, but I would prefer not to add boilerplate constructors if this solution is possible.)

Related Issue

@optional property injection overrides default

Motivation and Context

How Has This Been Tested?

By copying the test case for optional constructor injection but moving the injection to property injection and then running npm test

Types of changes

  • Updated docs / Refactor code / Added a tests case (non-breaking change)
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.
  • I have updated the changelog.

Currently the @optional tag overrides class properties defined with typescript-style default values:
```typescript
@Injectable()
class Ninja {
    @Inject(Weapon) @optional() weapon: Weapon = new Katana();

    ....
}
```
this currently sets the weapon property to `undefined` if no Weapon is present in the container. My proposed change allows does not override the default and results in the weapon property being set to new Katana().

(Default values in the constructor already work, but I would prefer not to add boilerplate constructors if this solution is possible.)
@codecov-commenter
Copy link

codecov-commenter commented Aug 13, 2022

Codecov Report

Merging #1467 (56055bf) into master (758a6bd) will increase coverage by 0.05%.
The diff coverage is 100.00%.

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

@@            Coverage Diff             @@
##           master    #1467      +/-   ##
==========================================
+ Coverage   99.56%   99.61%   +0.05%     
==========================================
  Files          53       53              
  Lines        1364     1568     +204     
  Branches      196      206      +10     
==========================================
+ Hits         1358     1562     +204     
  Partials        6        6              
Files Coverage Δ
src/resolution/instantiation.ts 96.72% <100.00%> (+0.05%) ⬆️

... and 22 files with indirect coverage changes

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@PodaruDragos
Copy link
Contributor

LGTM.
can I have your opinion here as well @jakehamtexas ?

@haaijman-imagem
Copy link
Contributor Author

LGTM. can I have your opinion here as well @jakehamtexas ?

Any updates on this?

Copy link
Member

@notaphplover notaphplover left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm!

@notaphplover
Copy link
Member

@haaijman-imagem can you update the branch? After that I would say we can merge it

@PodaruDragos PodaruDragos merged commit eec4159 into inversify:master Oct 17, 2023
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants