typescript-generator icon indicating copy to clipboard operation
typescript-generator copied to clipboard

output type and annotation information

Open dewitt-li opened this issue 3 years ago • 1 comments

Thanks for such a useful tool, have been using it in multiple projects. In order to facilitate the client side validation logic and angular reactive form creation, I am looking for ways to output the java class name and annotation information. For example with the following java class:

package com.example;
public class Point {
    @Min(0)
    @Max(100)
    private int x;
    @Min(0)
    @Max(100)
    private int y;
}

Is there any way to output the class name as one field in typescript interface definition, something similar as below?

export interface Point {
    x: number;
   y: number;
   _class:string="com.example.Point";
}

Is there any way to output the annotations in the typescript-generator-info.json, something similar as below? or in any other file which could be available to the frontend code.

{
      "javaClass": "com.example.Point",
      "typeName": "Point ",
      "fieldsInfo":{
            "x":{"annotations":[{"name":"Min","attributes":{"value":0}},{"name":"Max","attributes":{"value":100}}]}
            "y":{"annotations":[{"name":"Min","attributes":{"value":0}},{"name":"Max","attributes":{"value":100}}]}
       }
    }

I have been looking at all the existing options, but could not find anything obvious. Was looking for ways to extend typescript generator to get what I want, but not sure if it is the right approach. I would appreciate if anyone can give me some help or advice

dewitt-li avatar Dec 29 '20 19:12 dewitt-li

There is no support for adding Java class name as a field in TypeScript interface but it should be possible to create an extension for it. See some examples here: ext. But instead of interface you would need to generate class because it is not possible to have initializer (default value) in TypeScript interfaces.

Regarding "info" JSON: this wasn't meant to be extensible. Maybe you could create an extension that would read those annotations and output your own JSON file. Or this extension could transform those annotations to TypeScript decorators, here is an example: DecoratorsTest.

vojtechhabarta avatar Jan 02 '21 10:01 vojtechhabarta