loopback-next
loopback-next copied to clipboard
Error: Definition of model property is missing `type` when circularly referenced with @property()
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/[email protected] /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 ├── [email protected] npm ERR! missing: @loopback/boot@^3.4.0, required by @loopback/[email protected] npm ERR! missing: @loopback/core@^2.16.0, required by @loopback/[email protected] npm ERR! missing: @loopback/repository@^3.6.0, required by @loopback/[email protected] npm ERR! missing: @loopback/rest@^9.3.0, required by @loopback/[email protected] npm ERR! missing: @loopback/rest-explorer@^3.3.0, required by @loopback/[email protected] npm ERR! missing: @loopback/service-proxy@^3.2.0, required by @loopback/[email protected]
Related Issues
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