swagger-codegen icon indicating copy to clipboard operation
swagger-codegen copied to clipboard

[Question] Can Swagger generate custom generic types?

Open MelnikovIG opened this issue 6 years ago • 29 comments

Assume we have API return model in C#

public class ApiResult<T>
{
  public T Result;
  public bool Success;
}

and returning ApiResult<string> object instance to client

so we have swagger generated model

ApiResult[String] {
  result (string, optional),
  success (boolean, optional)
}

which is incorrectly converted to typescript class using https://swagger.io/swagger-codegen/

'use strict';
import * as models from './models';
export interface ApiResultString {
    result?: string;
    success?: boolean;
}

Is it possible to generate output models with generics same as in input models?

MelnikovIG avatar May 16 '18 13:05 MelnikovIG

Bump

Pangamma avatar Aug 03 '18 20:08 Pangamma

i'm not so familiar with typescript, but if you guys conclude that this is the best practice, we can implement it.

HugoMario avatar Aug 03 '18 23:08 HugoMario

I agree with the opening post. Generally ApiResult is a generic wrapper from the real payload from the rest backend.

At this moment if you have n APIs that all wraps n different objects, you end having ApiResultObjectApi1... ApiResultObjectApi2... ApiResultObjectApiN in the generated code. Several useless repetition that serves a "difficult" to use api.service.ts

Since typescript supports generics, it would be ideal to generate the typescript interfaces as generics as well. The code that use those could be more generic and the models would be DRY and smaller.

mauropanzeri avatar Aug 31 '18 07:08 mauropanzeri

A little more thinking into this and i just realized that there is an indecision case.

Suppose a c# mode like this

public class ApiResult<T>
{
  public T Result;
  public bool Success;
  public string message;
}

and an api returning ApiResult<string> object instance to client

so we would have swagger generated model

ApiResult[String] {
  result (string, optional),
  success (boolean, optional)
  message (string, optional)
}

how the generator's template should behave? It doesn't know which of the strings are from the generics.

Maybe a typescript-angular generate's option like enableGenerics (default: false) should be better. so you can enable it only iff you're sure of the api's model.

mauropanzeri avatar Aug 31 '18 09:08 mauropanzeri

Yes, the spec needs to be updated to handle this case. Otherwise, a generator would need a minimum of two types being used in a generic result before it could decide which properties are generic and which properties are stationary.

Pangamma avatar Oct 08 '18 18:10 Pangamma

Hi, is there any progress on supporting generics in TypeScript yet? Thanx!

robbertvanhove avatar Mar 22 '20 20:03 robbertvanhove

I have a same problem.

bluoxy avatar Mar 31 '20 01:03 bluoxy

I wish to use this feature too

Savrov avatar Apr 13 '20 21:04 Savrov

Same

fendorio avatar Oct 26 '20 21:10 fendorio

Any update on this?

Bernix01 avatar Apr 23 '21 19:04 Bernix01

2021 says hello. I still need and want this feature. (Also seems to be my third time finding this issue via Google. )

Pangamma avatar Apr 30 '21 20:04 Pangamma

+1

rhopiCoder avatar May 04 '21 14:05 rhopiCoder

+1

Thomas-Lederer avatar May 04 '21 14:05 Thomas-Lederer

+1

damien7792 avatar May 18 '21 16:05 damien7792

+1

ShuuMakengi avatar Jul 23 '21 14:07 ShuuMakengi

+1

joshuafc avatar Sep 01 '21 09:09 joshuafc

+1

fadi-shahoud avatar Dec 05 '21 22:12 fadi-shahoud

Same here

barta-home avatar Feb 24 '22 20:02 barta-home

2022 says hello. I still need and want this feature.

joshuafc avatar Feb 25 '22 01:02 joshuafc

Bumping for fix

duaneking avatar May 15 '22 14:05 duaneking

Would love to see this implemented

m17kea avatar Sep 15 '22 09:09 m17kea

i have the same problem . is is available?

josemariabaena avatar Oct 31 '22 10:10 josemariabaena

2023 says, siighhh.... still open...

davethieben avatar Mar 06 '23 00:03 davethieben

I wish to use this feature too

Chgocn avatar Mar 11 '23 09:03 Chgocn

+1

ullfindsmit avatar Jun 04 '23 15:06 ullfindsmit

Great feature to implement.

GaffaRay avatar Oct 13 '23 13:10 GaffaRay

+1

sabine19 avatar Feb 19 '24 14:02 sabine19

still open...

Junad-Park avatar Apr 01 '24 14:04 Junad-Park

It's possible to document generics in a spec through dynamic references (henry andrews approved), but I don't think swagger-codegen supports them

Aweptimum avatar Apr 13 '24 18:04 Aweptimum