Skip to content

[Question] Can Swagger generate custom generic types? #8194

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

Open
MelnikovIG opened this issue May 16, 2018 · 30 comments
Open

[Question] Can Swagger generate custom generic types? #8194

MelnikovIG opened this issue May 16, 2018 · 30 comments

Comments

@MelnikovIG
Copy link

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 MelnikovIG changed the title Can Swagger generate custom generic types? [Question] Can Swagger generate custom generic types? May 16, 2018
@Pangamma
Copy link

Pangamma commented Aug 3, 2018

Bump

@HugoMario
Copy link
Contributor

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

@mauropanzeri
Copy link

mauropanzeri commented Aug 31, 2018

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
Copy link

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.

@Pangamma
Copy link

Pangamma commented Oct 8, 2018

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.

@robbertvanhove
Copy link

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

@bluoxy
Copy link

bluoxy commented Mar 31, 2020

I have a same problem.

@Savrov
Copy link

Savrov commented Apr 13, 2020

I wish to use this feature too

@fendorio
Copy link

Same

@Bernix01
Copy link

Any update on this?

@Pangamma
Copy link

Pangamma commented Apr 30, 2021

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

@rhopiCoder
Copy link

+1

5 similar comments
@Thomas-Lederer
Copy link

+1

@damien7792
Copy link

+1

@ShuuMakengi
Copy link

+1

@joshuafc
Copy link

joshuafc commented Sep 1, 2021

+1

@fadi-shahoud
Copy link

+1

@barta-home
Copy link

Same here

@joshuafc
Copy link

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

@duaneking
Copy link

Bumping for fix

@m17kea
Copy link

m17kea commented Sep 15, 2022

Would love to see this implemented

@josemariabaena
Copy link

i have the same problem . is is available?

@davethieben
Copy link

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

@Chgocn
Copy link

Chgocn commented Mar 11, 2023

I wish to use this feature too

@ullfindsmit
Copy link

+1

@GaffaRay
Copy link

Great feature to implement.

@sabine19
Copy link

+1

@Junad-Park
Copy link

still open...

@Aweptimum
Copy link

Aweptimum commented Apr 13, 2024

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

@Alamonall
Copy link

Hello! Any progress?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests