Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ApiReponse with type is an Advanced Types #41

Closed
workfel opened this issue Jan 24, 2018 · 5 comments
Closed

ApiReponse with type is an Advanced Types #41

workfel opened this issue Jan 24, 2018 · 5 comments

Comments

@workfel
Copy link

workfel commented Jan 24, 2018

Hi,

I have an generic class who is return on all my request

import { ApiModelProperty } from '@nestjs/swagger';

export class ServerApiResponse<T> {
  
  @ApiModelProperty()
  value: T;
  
  @ApiModelProperty({ type: Number })
  pages: number;


  @ApiModelProperty({ type: Number })
  page: number;

  @ApiModelProperty({ type: Number })
  maxByPage: number;


  @ApiModelProperty({ type: Number })
  count: number;

  @ApiModelProperty({ type: Object })
  error: any;
}

And on my endpoint i have add

@ApiResponse({
    status: 200,
    description: '',
    type: ServerApiResponse<PatientCaretrackPopulatedOutput[]>
  })

But i have this error Error TS2348: Value of type 'ServerApiResponse' is not callable.

So i tried this

@ApiResponse({
    status: 200,
    description: '',
    type:  new ServerApiResponse<PatientCaretrackPopulatedOutput[]>()
  })

But when i launch swagger i have no description of response returned.

It's possible to do this ?

Thx

@alisherks
Copy link
Contributor

It's not possible for the same reason as Array or T[] (look at the issue microsoft/TypeScript/issues/10576).
You could try the following:

export abstract class ServiceApiResponse<T> {
  abstract get value(): T;
  
  @ApiModelProperty({ type: Number })
  pages: number;


  @ApiModelProperty({ type: Number })
  page: number;

  @ApiModelProperty({ type: Number })
  maxByPage: number;


  @ApiModelProperty({ type: Number })
  count: number;

  @ApiModelProperty({ type: Object })
  error: any;
}

export class PatientCaretrackPopulatedOutputResponse extends ServiceApiResponse<PatientCaretrackPopulatedResponse[]> {
  @ApiModelProperty({ type: PatientCaretrackPopulatedResponse, isArray: true })
  value: PatientCaretrackPopulatedResponse[];
}

@workfel
Copy link
Author

workfel commented Jan 24, 2018

Hi @alisherks, this works fine.
Thanks

@omar
Copy link

omar commented Mar 5, 2018

Cross posting this, does that mean a generic controller wouldn't be able to expose the proper endpoints? For example, something like this (taken from nestjs/nest#228):

export abstract class EntityController<T> {
    constructor(protected readonly service: EntityService<T>) { }

    @Get()
    async findAll() {
        return this.service.findAll();
    }

    @Get(':id')
    async getById(@Param() params) {
        return this.service.getById(params.id);
    }

    @Post()
    async create(@Body() data: Partial<T>) {
        return this.service.create(data);
    }
}

@QuentinLeCaignec
Copy link

Running into the same issue, having a generic CRUD controller being inherited is incredibly useful but for PUT and POST methods SwaggerUI is not implicitly picking up the T model.

I've tried specifying the model type with
@ImplicitApiBody({ name: 'data', type: thid.entityType })
where this.entityType is the actual Model declared in the child controller.

I've tried various ways of passing the model constructor to the generic parent controller and have @ImplicitApiBody work, to no avail.

Is there a way for Swagger to find the @Body type "T" in a generic parent controller like CrudController ?

@lock
Copy link

lock bot commented Apr 25, 2020

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.

@lock lock bot locked as resolved and limited conversation to collaborators Apr 25, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants