loopbackio / loopback-next

LoopBack makes it easy to build modern API applications that require complex integrations.
https://loopback.io
Other
4.96k stars 1.07k forks source link

Error: Definition of model property is missing `type` when circularly referenced with @property() #7482

Open joschne opened 3 years ago

joschne commented 3 years ago

Steps to reproduce

Create new lb4 app, then:

Create model Foo

@model()
export class Foo {

  @property()
  name?: string;

  @property({type: Bar})
  bar?: Bar;

  constructor(data?: Partial<Foo>) {
  }
}

Create model Bar

@model()
export class Bar {

  @property()
  name?: string;

  @property({type: Foo})
  foo?: Foo;

  constructor(data?: Partial<Bar>) {
  }
}

Start the application.

Current Behavior

Cannot start the application. Error: The definition of model property Foo.bar is missing type field and TypeScript did not provide any design-time type. Learn more at https://loopback.io/doc/en/lb4/Error-codes.html#cannot_infer_property_type

Expected Behavior

Should start, as it does when having self referenced model, like e.g.:

@model()
export class Baz {

  @property()
  name?: string;

  @property({type: Baz})
  baz?: Baz;

  constructor(data?: Partial<Baz>) {
  }
}

Link to reproduction sandbox

https://github.com/joschne/loopback-next/tree/my-issue-1

Additional information

darwin x64 12.8.1 @loopback/example-todo@3.11.0 /Users/admin/Code/Playground/bugs/loopback-next/examples/todo ├── UNMET DEPENDENCY @loopback/boot@^3.4.0 ├── UNMET DEPENDENCY @loopback/core@^2.16.0 ├── UNMET DEPENDENCY @loopback/repository@^3.6.0 ├── UNMET DEPENDENCY @loopback/rest@^9.3.0 ├── UNMET DEPENDENCY @loopback/rest-explorer@^3.3.0 ├── UNMET DEPENDENCY @loopback/service-proxy@^3.2.0 ├── loopback-connector-rest@4.0.1 npm ERR! missing: @loopback/boot@^3.4.0, required by @loopback/example-todo@3.11.0 npm ERR! missing: @loopback/core@^2.16.0, required by @loopback/example-todo@3.11.0 npm ERR! missing: @loopback/repository@^3.6.0, required by @loopback/example-todo@3.11.0 npm ERR! missing: @loopback/rest@^9.3.0, required by @loopback/example-todo@3.11.0 npm ERR! missing: @loopback/rest-explorer@^3.3.0, required by @loopback/example-todo@3.11.0 npm ERR! missing: @loopback/service-proxy@^3.2.0, required by @loopback/example-todo@3.11.0

Related Issues

joschne commented 3 years ago

After further investigation, I found a workaround.

// use:
  @hasOne(() => Bar)
  bar?: Bar;

// instead of
  @property(Bar)
  bar?: Bar;

// and expose the Model with relations on Controller endpoint so:
  @get('/bar')
  @response(200)
  bar(
    @requestBody({
      content: {
        'application/json': {
          schema: getModelSchemaRef(Bar, {includeRelations: true}),
        },
      },
    })
    bar: Bar,
  ): string {
    return 'ok';
  }

See: https://github.com/joschne/loopback-next/tree/my-issue-1-workaround