ApiModelProperty decorator on a ManyToOne relationship looses type on one side
See original GitHub issueI’m submitting a…
[ ] Regression
[x] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.
Current behavior
When defining a @ManyToOne
with @ApiModelProperty
the relation type gets lost in swagger definition and is set as type: ''
UserEntity:
...
app:
type: ''
...
Expected behavior
Swagger definition contains right type on both ends of relation
UserEntity:
...
app:
type: object
$ref: '#/definitions/AppEntity'
...
Minimal reproduction of the problem with instructions
@Entity()
export class AppEntity {
@ApiModelPropertyOptional({ type: String })
@PrimaryGeneratedColumn('uuid')
id: string
@ApiModelProperty()
@Column({ length: 256, nullable: true, default: null })
name: string
@ApiModelProperty({ type: UserEntity, isArray: true })
@OneToMany(type => UserEntity, user => user.app)
users: UserEntity[]
}
@Entity()
export class UserEntity {
@ApiModelPropertyOptional({ type: String })
@PrimaryGeneratedColumn('uuid')
id: string
@ApiModelProperty()
@Column()
name: string
@ApiModelProperty({ type: AppEntity })
@ManyToOne(type => AppEntity, app => app.users)
app: AppEntity
}
ends up with swagger definition
UserEntity:
type: object
properties:
id:
type: string
name:
type: string
app:
type: ''
required:
- name
- app
AppEntity:
type: object
properties:
id:
type: string
name:
type: string
users:
type: array
items:
$ref: '#/definitions/UserEntity'
required:
- name
- users
What is the motivation / use case for changing the behavior?
@ManyToOne
/ @OneToMany
should create correct definition
Environment
"@nestjs/common": "^4.6.5",
"@nestjs/core": "^4.6.5",
"@nestjs/microservices": "^4.6.5",
"@nestjs/swagger": "^1.1.4",
"@nestjs/testing": "^4.6.1",
"@nestjs/typeorm": "^2.0.0",
"@nestjs/websockets": "^4.6.5",
"typeorm": "^0.1.16",
"typescript": "^2.7.2"
Issue Analytics
- State:
- Created 6 years ago
- Reactions:7
- Comments:9 (4 by maintainers)
Top Results From Across the Web
How to add the ApiModelProperty decorator to a ManyToOne ...
I'm using Nest.js with the TypeORM and Swagger modules. So far documenting the API endpoints with Swagger works like a charm, but I'd...
Read more >NestJS Basic Auth and Sessions - Just Another Typescript Blog
Custom Decorator – We will create a custom route decorator to access our User object from the session. Basic Auth – User authentication...
Read more >03 - TypeORM one-to-many/many-to-one relationship - YouTube
repo:https://github.com/Rowadz/typeorm_ytDOCS: many to one :https://typeorm.io/#/ many-to-one - one -to-many-relations00:00 Introduction00:40 ...
Read more >Table of Contents - DOKUMEN.PUB
Nest.js making use of Express, you have access to each and every one of these ... the @ManyToOne() decorators is used to specify...
Read more >Untitled
The srs, Manthous campur sari gunung kidul part 1, Petervale family planning ... Lettore vinile prezzo, Different types of execution, England australia ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
This happens because when you have two models that depend on each other, e.g. AccesToken -> User, User -> AccesToken, one of imports will be undefined when the file first runs. In order the resolve this you need to use a function in the same way the
@OneToMany
/@ManyToOne
decorator does it.This illustrates the problem:
A proposed fix is to wrap the related Model in a callback, the same way that
@OneToMany
does this. E.g.:I already requested a merge on this pull request: https://github.com/nestjs/swagger/pull/179 (https://github.com/labibramadhan/swagger/pull/1)
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.