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

Parameterized type propagates type parameters to customTypeMapping #358

Closed
bjorsa opened this issue Jun 4, 2019 · 8 comments
Closed

Parameterized type propagates type parameters to customTypeMapping #358

bjorsa opened this issue Jun 4, 2019 · 8 comments

Comments

@bjorsa
Copy link

bjorsa commented Jun 4, 2019

Given the following hierarchy of classes

class MyEntityRepresentation {
  IdRepresentation<MyEntityRepresentation> id;
}

class IdRepresentation<T> {
  String id;
}

And the following customTypeMapping

<customTypeMapping>IdRepresentation:string</customTypeMapping>

The generator will yield the following result.

export interface MyEntityRepresentation {
  id: string<MyEntityRepresentation>;
}

(Sorry for the pseudocode, I will provide a real example if needed).

Which of course won't compile as typescript.

This problem was introduced with version 2.10.466 as part of the rewrite concerning the handling of type parameters for customTypeMappings.

@vojtechhabarta
Copy link
Owner

Thanks for precise description of the problem.

I think it is not possible to detect if target type (in this case string) is generic because it can be anything (build-in type, generated type, type from some library).

I see some possibilities how to solve this issue but non of them is ideal.

  • In typescript-generator: Introduce some new syntax for customTypeMapping parameter which would specify how generic type variables should be handled. For example <customTypeMapping>IdRepresentation<T>:string</customTypeMapping> which would mean that <T> should just be ignored.

  • On your side: Write type alias export type IdRepresentation<T> = string; and use it in <customTypeMapping>IdRepresentation:IdRepresentation</customTypeMapping>.

What do you think? Do you know what I mean by these solutions?

@bjorsa
Copy link
Author

bjorsa commented Jun 5, 2019

Thank you for your quick response.

Yes I do know what you mean.

I did briefly examine the possibility of creating a PR for something similar to the first of the possibilities that you list, and ultimately decided not to since I did recognize that it would affect the customTypeMapping syntax in ways that you would probably want to have a say in anyways.

Both of your suggestions are possible to employ in our project, that beeing said though, I would prefer the first one since we do have a number of types that are being affected. The second solution would leave us with quite a number of interfaces that are defined using alias types for string and making code somewhat harder to read and understand.

@vojtechhabarta
Copy link
Owner

Currently customTypeMapping requires that source and target types have the same number of generic parameters which is limiting and I will try to improve it.

Workaround using type alias is little bit complicated but I think it's still feasible. Here is example how it can be done:

Create file id.d.ts:

export type Id<T> = string;

Import Id type from it:

<importDeclarations>
    <import>import { Id } from './id'</import>
</importDeclarations>

And change mappings:

<customTypeMappings>
    <mapping>IdRepresentation1:Id</mapping>
    <mapping>IdRepresentation2:Id</mapping>
</customTypeMappings>

There is just one Id type alias. Having this alias instead of just string can be harder to read but it can also be advantage because it says that the property is ID and not arbitrary string.

If you also have number IDs you could have two aliases StringId and NumberId.

@vojtechhabarta
Copy link
Owner

In version 2.15 I added customTypeAliases parameter which simplifies solution using Id type alias. Now there is no need for extra file and import.

Maven example:

<configuration>
    <customTypeMappings>
        <mapping>IdRepresentation1:Id</mapping>
        <mapping>IdRepresentation2:Id</mapping>
    </customTypeMappings>
    <customTypeAliases>
        <alias>Id&lt;T>:string</alias>
    </customTypeAliases>
</configuration>

Please note that < character must be escaped using &lt; in XML.

Is this acceptable solution for you?

@rajesh99
Copy link

I am having same problem. It is working perfect in 2.0.400 version.
I wanted to upgrade to the latest and use the following feature.
<jackson2Configuration> <enumsUsingToString>true</enumsUsingToString> </jackson2Configuration>

But a side effect got introduced into the <customTypeMappings>, as explained by the original author.
Regards.

@vojtechhabarta
Copy link
Owner

In 2.16.538 release I changed customTypeMappings parameter format (#384). So now it should be possible to use:

<customTypeMappings>
    <mapping>IdRepresentation[T]:string</mapping>
</customTypeMappings>

@bjorsa could you please try it?

@xdy
Copy link

xdy commented Aug 16, 2019

I'm a colleague of @bjorsa working in the same project as him, I just updated typescript-generator to 2.16.538 which seems to work fine. Thank you!

@vojtechhabarta
Copy link
Owner

@bjorsa, @rajesh99, @xdy thanks for your feedback.

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

4 participants