nestjsx / crud

NestJs CRUD for RESTful APIs
https://github.com/nestjsx/crud/wiki
MIT License
4.04k stars 533 forks source link

Not create swagger reference without `getOneBase` route #730

Open Kolobok12309 opened 3 years ago

Kolobok12309 commented 3 years ago

I have 2 entities with specific routes, they haven't getOneBase routes, only 'getManyBase', 'createOneBase', 'updateOneBase', 'deleteOneBase' For one of this entities i have this crud options

@Crud({
  model: {
    type: BreedDto,
  },
  dto: {
    create: CreateBreedDto,
    update: UpdateBreedDto,
  },
  routes: {
    only: ['getManyBase', 'createOneBase', 'updateOneBase', 'deleteOneBase'],
  },
})

And i get error in swagger

Resolver error at paths./breed.get.responses.200.content.application/json.schema.oneOf.0.$ref
Could not resolve reference: #/components/schemas/GetManyBreedDtoResponseDto
Resolver error at paths./breed.get.responses.200.content.application/json.schema.oneOf.1.items.$ref
Could not resolve reference: #/components/schemas/BreedDto

But if i add getOneBase to routes array, all worked fine. I think it's not normal. @ApiExtraModels(BreedDto) fix it too And if i replace CreateBreedDto or UpdateBreedDto or mb add BreedDto to some dto properties, all normally. Try add serialize.get and serialize.getMany, but it useless

Version

"@nestjsx/crud": "5.0.0-alpha.3",
"@nestjsx/crud-typeorm": "5.0.0-alpha.3",
"@nestjs/swagger": "4.8.2",
pashkash commented 2 years ago

same here, @Kolobok12309 did you handle this?

Kolobok12309 commented 2 years ago

@pashkash At the moment I don't remember why, but this my code now

@Crud({
  model: {
    type: BreedDto,
  },
  dto: {
    create: CreateBreedDto,
    update: UpdateBreedDto,
  },
  routes: {
    only: ['getManyBase', 'createOneBase', 'updateOneBase', 'deleteOneBase'],
  },
  ...
})
// TODO Remove it after crud decorator fix `getOneBase` bug
@ApiExtraModels(BreedDto)
@ApiTags('Breed')
@Controller('breed')
export class BreedController implements CrudController<BreedDto> {
  constructor(public service: BreedService) {}
}
pashkash commented 2 years ago

@Kolobok12309 thank you for reply; this decorator @ApiExtraModels(...) added schemas for entities for me; which is good, but all they are empty in openapi json file, e.g. "BreedDto": { "type": "object", "properties": {} },

pashkash commented 2 years ago

ok, i just need to add @ApiProperty(), ApiModelProperty, etc to properties of entities; which you dont need to add if you are not using crud; solved;

dont forget to use type: (type) => Contact in yours @ApiModelProperty({ type: (type) => Contact, isArray: false }) for entities relations to let swagger to render relations schemas