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

Mapping generic classes using customTypeMappings parameter #384

Closed
vojtechhabarta opened this issue Jul 6, 2019 · 4 comments
Closed

Mapping generic classes using customTypeMappings parameter #384

vojtechhabarta opened this issue Jul 6, 2019 · 4 comments

Comments

@vojtechhabarta
Copy link
Owner

It is possible to configure how some Java class is mapped to TypeScript. For example org.joda.time.LocalDateTime Java class can be mapped to string in TypeScript. For this purpose there is customTypeMappings parameter which is list where each item specifies one mapping, for example like this: org.joda.time.LocalDateTime:string.

Mapping generic Java classes to TypeScript non-generic types

Originally when Java class was generic (for example com.package.IdRepresentation<T>) it could have been mapped to non-generic type like string using com.package.IdRepresentation:string mapping.

Mapping generic Java classes to TypeScript generic types

To support custom mapping of container types like lists and maps typescript-generator from version 2.10.466 mapped Java generic type parameters to TypeScript generic types. For example mapping Java class ListWrapper<T> to TypeScript type ListWrapper<T> could have been configured using com.package.ListWrapper:ListWrapper custom mapping. Unfortunately this feature broke previous possibility to map generic Java class to non-generic TypeScript type.

Solution

To distinguish between these two cases new syntax is needed. But it is not clear which behavior should be preserved and which would use new syntax. To avoid ambiguity new syntax is required for all generic Java classes. This new syntax supports both mentioned cases and also allows new possibilities. Here are some examples:

com.package.IdRepresentation<T1>:string                     # first case
com.package.MyGenericClass<T1,T2>:TsGenericType2<T1,T2>     # second case
com.package.MyGenericClass<T1,T2>:TsGenericType2<T2,T1>     # new
com.package.MyGenericClass<T1,T2>:TsGenericType1<T2>        # new
com.package.MyGenericClass<T1,T2>:TsGenericType3<T1,T2,T1>  # new
com.package.Wrapper<T>:T                                    # new ("unwrap")

To prevent character escaping in Maven pom.xml files it is also possible to use [] characters instead of <>. Here is Maven example:

<configuration>
    <customTypeMappings>
        <mapping>com.package.IdRepresentation[T1]:string</mapping>
        <mapping>com.package.MyGenericClass[T1,T2]:TsGenericType2[T1,T2]</mapping>
        <mapping>com.package.Wrapper[T]:T</mapping>
    </customTypeMappings>
</configuration>
@vojtechhabarta
Copy link
Owner Author

Released in 2.16.538.

@jjoslet
Copy link
Contributor

jjoslet commented Aug 6, 2019

Hello @vojtechhabarta ,

I was using the following type mapping my.custom.JavaType:Map<string,string> that now fails using 2.16.538: message is Unknown generic type parameter 'string'.

Is it possible to do this mapping in that version ? Is this a regression ?

Thanks in advance for your answers.

vojtechhabarta added a commit that referenced this issue Aug 6, 2019
@vojtechhabarta
Copy link
Owner Author

Hello @jjoslet,
actually I didn't think about this case, 5c82b28 should fix it.

@vojtechhabarta vojtechhabarta reopened this Aug 6, 2019
@vojtechhabarta
Copy link
Owner Author

Fix released in version 2.17.558.

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

2 participants