Skip to content

Commit c9deda3

Browse files
jaxrs: restResponseType, restOptionsType
1 parent 1c03813 commit c9deda3

File tree

6 files changed

+71
-8
lines changed

6 files changed

+71
-8
lines changed

typescript-generator-core/src/main/java/cz/habarta/typescript/generator/Settings.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ public class Settings {
4141
public ClassMapping mapClasses; // default is ClassMapping.asInterfaces
4242
public boolean disableTaggedUnions = false;
4343
public boolean generateJaxrsApplicationInterface = false;
44+
public String restResponseType = null;
45+
public String restOptionsType = null;
4446
public TypeProcessor customTypeProcessor = null;
4547
public boolean sortDeclarations = false;
4648
public boolean sortTypeDeclarations = false;

typescript-generator-core/src/main/java/cz/habarta/typescript/generator/TsType.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,20 @@ public String format(Settings settings) {
6262
}
6363
}
6464

65+
public static class VerbatimType extends TsType {
66+
67+
public final String verbatimType;
68+
69+
public VerbatimType(String verbatimType) {
70+
this.verbatimType = verbatimType;
71+
}
72+
73+
@Override
74+
public String format(Settings settings) {
75+
return verbatimType;
76+
}
77+
}
78+
6579
/**
6680
* Identifier which references some type, for example interface or type alias.
6781
*/

typescript-generator-core/src/main/java/cz/habarta/typescript/generator/compiler/ModelCompiler.java

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -270,15 +270,22 @@ private static List<TsPropertyModel> getImplementedProperties(SymbolTable symbol
270270
}
271271

272272
private TsModel createJaxrsInterface(SymbolTable symbolTable, TsModel tsModel, Model model) {
273-
final Symbol responseSymbol = symbolTable.getSyntheticSymbol("RestResponse");
274-
final TsType.GenericVariableType varR = new TsType.GenericVariableType("R");
275-
final TsType.GenericReferenceType responseTypeDefinition = new TsType.GenericReferenceType(symbolTable.getSyntheticSymbol("Promise"), Arrays.<TsType>asList(varR));
276-
final TsAliasModel responseTypeAlias = new TsAliasModel(null, responseSymbol, Arrays.asList(varR), responseTypeDefinition, null);
277-
tsModel.getTypeAliases().add(responseTypeAlias);
278273
final JaxrsApplicationModel jaxrsApplication = model.getJaxrsApplication();
279274
if (jaxrsApplication == null || jaxrsApplication.getMethods().isEmpty()) {
280275
return tsModel;
281276
}
277+
// response type
278+
final Symbol responseSymbol = symbolTable.getSyntheticSymbol("RestResponse");
279+
final TsType.GenericVariableType varR = new TsType.GenericVariableType("R");
280+
final TsAliasModel responseTypeAlias;
281+
if (settings.restResponseType != null) {
282+
responseTypeAlias = new TsAliasModel(null, responseSymbol, Arrays.asList(varR), new TsType.VerbatimType(settings.restResponseType), null);
283+
} else {
284+
final TsType.GenericReferenceType responseTypeDefinition = new TsType.GenericReferenceType(symbolTable.getSyntheticSymbol("Promise"), Arrays.<TsType>asList(varR));
285+
responseTypeAlias = new TsAliasModel(null, responseSymbol, Arrays.asList(varR), responseTypeDefinition, null);
286+
}
287+
tsModel.getTypeAliases().add(responseTypeAlias);
288+
// application interface
282289
final String applicationPath = jaxrsApplication.getApplicationPath();
283290
final String pathPrefix = applicationPath != null && !applicationPath.isEmpty() ? applicationPath + "/" : "";
284291
final List<TsMethodModel> methods = new ArrayList<>();
@@ -314,6 +321,10 @@ private TsMethodModel processJaxrsMethod(SymbolTable symbolTable, String pathPre
314321
for (MethodParameterModel parameter : method.getPathParams()) {
315322
parameters.add(processParameter(symbolTable, method, parameter));
316323
}
324+
// entity param
325+
if (method.getEntityParam() != null) {
326+
parameters.add(processParameter(symbolTable, method, method.getEntityParam()));
327+
}
317328
// query params
318329
final List<MethodParameterModel> queryParams = method.getQueryParams();
319330
if (queryParams != null && !queryParams.isEmpty()) {
@@ -325,9 +336,9 @@ private TsMethodModel processJaxrsMethod(SymbolTable symbolTable, String pathPre
325336
final TsParameterModel queryParameter = new TsParameterModel("queryParams", new TsType.OptionalType(new TsType.ObjectType(properties)));
326337
parameters.add(queryParameter);
327338
}
328-
// entity param
329-
if (method.getEntityParam() != null) {
330-
parameters.add(processParameter(symbolTable, method, method.getEntityParam()));
339+
if (settings.restOptionsType != null) {
340+
final TsParameterModel optionsParameter = new TsParameterModel("options", new TsType.OptionalType(new TsType.VerbatimType(settings.restOptionsType)));
341+
parameters.add(optionsParameter);
331342
}
332343
// return type
333344
final TsType returnType = typeFromJava(symbolTable, method.getReturnType(), method.getName(), method.getOriginClass());

typescript-generator-core/src/test/java/cz/habarta/typescript/generator/JaxrsApplicationTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,7 @@ public void complexInterfaceTest() {
271271
settings.generateJaxrsApplicationInterface = true;
272272
final String output = new TypeScriptGenerator(settings).generateTypeScript(Input.from(OrganizationApplication.class));
273273
final String errorMessage = "Unexpected output: " + output;
274+
Assert.assertTrue(errorMessage, output.contains("type RestResponse<R> = Promise<R>;"));
274275
Assert.assertTrue(errorMessage, output.contains("interface Organization"));
275276
Assert.assertTrue(errorMessage, output.contains("interface OrganizationApplication"));
276277
Assert.assertTrue(errorMessage, output.contains("HTTP GET /api/organizations/{ organizationCode : [a-z]+ }/{organizationId}"));
@@ -296,6 +297,18 @@ public void methodNameConflictTest() {
296297
Assert.assertTrue(errorMessage, output.contains("person$GET$conflict_personId(personId: number): RestResponse<Person>;"));
297298
}
298299

300+
@Test
301+
public void customizationsTest() {
302+
final Settings settings = TestUtils.settings();
303+
settings.generateJaxrsApplicationInterface = true;
304+
settings.restResponseType = "AxiosPromise";
305+
settings.restOptionsType = "AxiosRequestConfig";
306+
final String output = new TypeScriptGenerator(settings).generateTypeScript(Input.from(OrganizationApplication.class));
307+
final String errorMessage = "Unexpected output: " + output;
308+
Assert.assertTrue(errorMessage, output.contains("type RestResponse<R> = AxiosPromise;"));
309+
Assert.assertTrue(errorMessage, output.contains("searchOrganizations(queryParams?: { name?: string; \"search-limit\"?: number; }, options?: AxiosRequestConfig): RestResponse<Organization[]>;"));
310+
}
311+
299312
@ApplicationPath("api")
300313
private static class OrganizationApplication extends Application {
301314
@Override

typescript-generator-gradle-plugin/src/main/java/cz/habarta/typescript/generator/gradle/GenerateTask.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ public class GenerateTask extends DefaultTask {
4141
public ClassMapping mapClasses;
4242
public boolean disableTaggedUnions;
4343
public boolean experimentalJaxrsApplicationInterface;
44+
public String restResponseType;
45+
public String restOptionsType;
4446
public String customTypeProcessor;
4547
public boolean sortDeclarations;
4648
public boolean sortTypeDeclarations;
@@ -101,6 +103,8 @@ public void generate() throws Exception {
101103
settings.mapClasses = mapClasses;
102104
settings.disableTaggedUnions = disableTaggedUnions;
103105
settings.generateJaxrsApplicationInterface = experimentalJaxrsApplicationInterface;
106+
settings.restResponseType = restResponseType;
107+
settings.restOptionsType = restOptionsType;
104108
settings.loadCustomTypeProcessor(classLoader, customTypeProcessor);
105109
settings.sortDeclarations = sortDeclarations;
106110
settings.sortTypeDeclarations = sortTypeDeclarations;

typescript-generator-maven-plugin/src/main/java/cz/habarta/typescript/generator/maven/GenerateMojo.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,23 @@ public class GenerateMojo extends AbstractMojo {
239239
@Parameter
240240
private boolean experimentalJaxrsApplicationInterface;
241241

242+
/**
243+
* Specifies HTTP response type in JAXRS application.
244+
* Default value is <code>Promise&amp;R></code> which means data object returned asynchronously.
245+
* Useful for example when underlying HTTP response object (like <code>XMLHttpRequest</code> or <code>AxiosPromise</code>)
246+
* is returned instead of actual response data.
247+
*/
248+
@Parameter
249+
private String restResponseType;
250+
251+
/**
252+
* Specifies HTTP request options type in JAXRS application.
253+
* By default no <code>options</code> parameter is generated.
254+
* Useful when passing additional parameters to underlying HTTP request method (like jQuery ajax settings or <code>AxiosRequestConfig</code>).
255+
*/
256+
@Parameter
257+
private String restOptionsType;
258+
242259
/**
243260
* Specifies custom class implementing {@link cz.habarta.typescript.generator.TypeProcessor}.
244261
* This allows to customize how Java types are mapped to TypeScript.
@@ -355,6 +372,8 @@ public void execute() {
355372
settings.mapClasses = mapClasses;
356373
settings.disableTaggedUnions = disableTaggedUnions;
357374
settings.generateJaxrsApplicationInterface = experimentalJaxrsApplicationInterface;
375+
settings.restResponseType = restResponseType;
376+
settings.restOptionsType = restOptionsType;
358377
settings.loadCustomTypeProcessor(classLoader, customTypeProcessor);
359378
settings.sortDeclarations = sortDeclarations;
360379
settings.sortTypeDeclarations = sortTypeDeclarations;

0 commit comments

Comments
 (0)