loopbackio / loopbackio/loopback-next

@service(GeocoderProvider) Fails with "No Binding Found" in LoopBack Component

Aperta
#11,062 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

bug
Lingua principale
TypeScript
Stelle
5.1k
Fork
1.1k
Merge medio
2g 21h
PR unite (30g)
27

Descrizione

Describe the bug

Problem Description
@service(GeocoderProvider) Fails with "No Binding Found" in LoopBack Component after overriding the same class via Service booter in application.

Problem Statement
You created a custom LoopBack Component named DummyComponent, which declares and registers models, repositories, controllers, and a service (GeocoderProvider):

export class DummyComponent implements Component {
  ...
  services?: ServiceOrProviderClass[];
  ...
  constructor(
    @inject(CoreBindings.APPLICATION_INSTANCE)
    private application: Application,
  ) {
    this.services = [GeocoderProvider]; // <-- Intended to register the service
  }
}

and used in a controller as

export class TodoController {
  constructor(
    @repository(TodoRepository)
    public todoRepository: TodoRepository,
    
    @service(GeocoderProvider) // <-- This fails
    protected geoService: Geocoder,
  ) {}
}

i added DummyComponent in application.ts as

this.component(DummyComponent);

this works fine. but when i override the GeocoderProvider via Service Booter.
this gives me error

500 Error: No binding found for GeocoderProvider. 
Make sure a service binding is created in context RequestContext-xxxx with serviceInterface (GeocoderProvider).

Steps to reproduce:

Reason for this error->
@service decorator filters the binding based on a functions

export function filterByServiceInterface(
  serviceInterface: ServiceInterface,
): BindingFilter {
  return binding =>
    binding.valueConstructor === serviceInterface ||
    binding.tagMap[CoreTags.SERVICE_INTERFACE] === serviceInterface;
}

and if we explicitly does not provide interface then it uses class itself as interface and used it for comparison. and we end up with two different services that causes comparison to fail and returning no binding found.
As we don't have any options to provide this interface(serviceOptions) via service booter. either we need to Manually bind services after the boot process (defeating the purpose of convention-based discovery)

Solution:
I have created a decorator @serviceOptions decorator that allows you to annotate service classes with configuration options metadata. It uses the factory pattern to create decorators with a specific options metadata key.
Then createServiceBinding function is the core registration mechanism that:

  • Extracts metadata: Retrieves decorator-defined options from the class
  • Merges configurations: Combines decorator metadata with manually passed options (manual options take precedence)
  • and rest will be same
Logs

Additional information

No response

Reproduction

https://github.com/Tyagi-Sunny/loopback-next/tree/bug-no-binding-found

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia nell’application.ts dell’esempio todo e segui la registrazione di Service Booter fino a @service e filterByServiceInterface. Leggi il percorso createServiceBinding e il comportamento proposto dei metadati @serviceOptions, quindi riproduci il fallimento con il ramo bug-no-binding-found e GET /todo. Il lavoro è completato quando il GeocoderProvider sovrascritto viene risolto tramite @service senza un errore No binding found.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
typescript
Ambito
backend
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.