Skip to content

Commit 5a19af1

Browse files
jaxrs client: encode URI variables
1 parent 09562de commit 5a19af1

File tree

9 files changed

+104
-22
lines changed

9 files changed

+104
-22
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,7 @@ private TsModel processModel(SymbolTable symbolTable, Model model) {
101101
for (EnumModel<?> enumModel : model.getEnums()) {
102102
enums.add(processEnum(symbolTable, enumModel));
103103
}
104-
final List<TsAliasModel> typeAliases = new ArrayList<>();
105-
return new TsModel(beans, enums, typeAliases);
104+
return new TsModel().setBeans(beans).setEnums(enums);
106105
}
107106

108107
private Map<Type, List<BeanModel>> createChildrenMap(Model model) {
@@ -351,6 +350,8 @@ private TsModel createJaxrsClient(SymbolTable symbolTable, TsModel tsModel, Jaxr
351350
final TsType interfaceType = settings.generateJaxrsApplicationInterface ? new TsType.ReferenceType(symbolTable.getSyntheticSymbol(applicationName)) : null;
352351
final TsBeanModel clientModel = new TsBeanModel(Application.class, TsBeanCategory.Service, true, symbolTable.getSyntheticSymbol(applicationClientName), null, null, null, Utils.listFromNullable(interfaceType), null, constructor, methods, null);
353352
tsModel.getBeans().add(clientModel);
353+
// helper
354+
tsModel.getHelpers().add(TsHelper.loadFromResource("/helpers/uriEncoding.ts"));
354355
return tsModel;
355356
}
356357

@@ -380,7 +381,6 @@ private static Map<String, Long> groupingByMethodName(List<JaxrsMethodModel> met
380381
}
381382
return methodNamesCount;
382383
}
383-
384384

385385
private TsMethodModel processJaxrsMethod(SymbolTable symbolTable, String pathPrefix, Symbol responseSymbol, JaxrsMethodModel method, boolean createLongName, TsType optionsType, boolean implement) {
386386
final String path = Utils.joinPath(pathPrefix, method.getPath());
@@ -469,7 +469,7 @@ private static TsTemplateLiteral processPathTemplate(PathTemplate pathTemplate)
469469
spans.add(new TsIdentifierReference(parameter.getName()));
470470
}
471471
}
472-
return new TsTemplateLiteral(spans);
472+
return new TsTaggedTemplateLiteral(new TsIdentifierReference("uriEncoding"), spans);
473473
}
474474

475475
private TsModel transformDates(SymbolTable symbolTable, TsModel tsModel) {

typescript-generator-core/src/main/java/cz/habarta/typescript/generator/emitter/Emitter.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ private void emitElements(TsModel model, boolean exportKeyword, boolean declareK
106106
emitBeans(model, exportKeyword);
107107
emitTypeAliases(model, exportKeyword);
108108
emitNumberEnums(model, exportKeyword, declareKeyword);
109+
emitHelpers(model);
109110
for (EmitterExtension emitterExtension : settings.extensions) {
110111
writeNewLine();
111112
writeNewLine();
@@ -247,6 +248,18 @@ private void emitNumberEnums(TsModel model, boolean exportKeyword, boolean decla
247248
}
248249
}
249250

251+
private void emitHelpers(TsModel model) {
252+
for (TsHelper helper : model.getHelpers()) {
253+
writeNewLine();
254+
for (String line : helper.getLines()) {
255+
writeIndentedLine(line
256+
.replace("\t", settings.indentString)
257+
.replace("\"", settings.quotes)
258+
);
259+
}
260+
}
261+
}
262+
250263
private void emitUmdNamespace() {
251264
if (settings.umdNamespace != null) {
252265
writeNewLine();
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
package cz.habarta.typescript.generator.emitter;
3+
4+
import cz.habarta.typescript.generator.util.Utils;
5+
import java.util.*;
6+
7+
8+
public class TsHelper {
9+
10+
private final List<String> lines;
11+
12+
public TsHelper(List<String> lines) {
13+
this.lines = lines;
14+
}
15+
16+
public static TsHelper loadFromResource(String resourceName) {
17+
final String text = Utils.readString(TsHelper.class.getResourceAsStream(resourceName));
18+
return new TsHelper(Utils.splitMultiline(text, false));
19+
}
20+
21+
public List<String> getLines() {
22+
return lines;
23+
}
24+
25+
}

typescript-generator-core/src/main/java/cz/habarta/typescript/generator/emitter/TsModel.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,20 @@ public class TsModel {
1010
private final List<TsBeanModel> beans;
1111
private final List<TsEnumModel<?>> enums;
1212
private final List<TsAliasModel> typeAliases;
13+
private final List<TsHelper> helpers;
1314

1415
public TsModel() {
15-
this (new ArrayList<TsBeanModel>(), new ArrayList<TsEnumModel<?>>(), new ArrayList<TsAliasModel>());
16+
this (new ArrayList<TsBeanModel>(), new ArrayList<TsEnumModel<?>>(), new ArrayList<TsAliasModel>(), new ArrayList<TsHelper>());
1617
}
1718

18-
public TsModel(List<TsBeanModel> beans, List<TsEnumModel<?>> enums, List<TsAliasModel> typeAliases) {
19+
public TsModel(List<TsBeanModel> beans, List<TsEnumModel<?>> enums, List<TsAliasModel> typeAliases, List<TsHelper> helpers) {
1920
if (beans == null) throw new NullPointerException();
2021
if (enums == null) throw new NullPointerException();
2122
if (typeAliases == null) throw new NullPointerException();
2223
this.beans = beans;
2324
this.enums = enums;
2425
this.typeAliases = typeAliases;
26+
this.helpers = helpers;
2527
}
2628

2729
public List<TsBeanModel> getBeans() {
@@ -40,7 +42,7 @@ public TsBeanModel getBean(Class<?> origin) {
4042
}
4143

4244
public TsModel setBeans(List<TsBeanModel> beans) {
43-
return new TsModel(beans, enums, typeAliases);
45+
return new TsModel(beans, enums, typeAliases, helpers);
4446
}
4547

4648
public List<TsEnumModel<?>> getEnums() {
@@ -59,7 +61,7 @@ public <T> List<TsEnumModel<T>> getEnums(EnumKind<T> enumKind) {
5961
}
6062

6163
public TsModel setEnums(List<TsEnumModel<?>> enums) {
62-
return new TsModel(beans, enums, typeAliases);
64+
return new TsModel(beans, enums, typeAliases, helpers);
6365
}
6466

6567
public List<TsAliasModel> getTypeAliases() {
@@ -78,7 +80,11 @@ public TsAliasModel getTypeAlias(Class<?> origin) {
7880
}
7981

8082
public TsModel setTypeAliases(List<TsAliasModel> typeAliases) {
81-
return new TsModel(beans, enums, typeAliases);
83+
return new TsModel(beans, enums, typeAliases, helpers);
84+
}
85+
86+
public List<TsHelper> getHelpers() {
87+
return helpers;
8288
}
8389

8490
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
package cz.habarta.typescript.generator.emitter;
3+
4+
import cz.habarta.typescript.generator.Settings;
5+
import java.util.List;
6+
7+
8+
public class TsTaggedTemplateLiteral extends TsTemplateLiteral {
9+
10+
private final TsExpression tagFunction;
11+
12+
public TsTaggedTemplateLiteral(TsExpression tagFunction, List<TsExpression/*|TsStringLiteral*/> spans) {
13+
super(spans);
14+
this.tagFunction = tagFunction;
15+
}
16+
17+
public TsExpression getTagFunction() {
18+
return tagFunction;
19+
}
20+
21+
@Override
22+
public String format(Settings settings) {
23+
return tagFunction.format(settings) + super.format(settings);
24+
}
25+
26+
}

typescript-generator-core/src/main/java/cz/habarta/typescript/generator/parser/Javadoc.java

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -198,23 +198,14 @@ private static List<String> getComments(String dComments, List<TagInfo> tags) {
198198
}
199199
final List<String> result = new ArrayList<>();
200200
if (dComments != null) {
201-
result.addAll(splitMultiline(dComments));
201+
result.addAll(Utils.splitMultiline(dComments, true));
202202
}
203203
if (tags != null) {
204204
for (TagInfo tag : tags) {
205-
result.addAll(splitMultiline(tag.getName() + " " + tag.getText()));
205+
result.addAll(Utils.splitMultiline(tag.getName() + " " + tag.getText(), true));
206206
}
207207
}
208208
return result;
209209
}
210210

211-
private static List<String> splitMultiline(String comments) {
212-
final List<String> result = new ArrayList<>();
213-
final String[] lines = comments.split("\\r\\n|\\n|\\r");
214-
for (String line : lines) {
215-
result.add(line.trim());
216-
}
217-
return result;
218-
}
219-
220211
}

typescript-generator-core/src/main/java/cz/habarta/typescript/generator/util/Utils.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,15 @@ private static String normalizeLineEndings(String text, String lineEndings) {
178178
return text.replaceAll("\\r\\n|\\n|\\r", lineEndings);
179179
}
180180

181+
public static List<String> splitMultiline(String text, boolean trimLines) {
182+
final List<String> result = new ArrayList<>();
183+
final String[] lines = text.split("\\r\\n|\\n|\\r");
184+
for (String line : lines) {
185+
result.add(trimLines ? line.trim() : line);
186+
}
187+
return result;
188+
}
189+
181190
public static File replaceExtension(File file, String newExtension) {
182191
final String name = file.getName();
183192
final int dotIndex = name.lastIndexOf(".");
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
function uriEncoding(template: TemplateStringsArray, ...substitutions: any[]): string {
2+
let result = "";
3+
for (let i = 0; i < substitutions.length; i++) {
4+
result += template[i];
5+
result += encodeURIComponent(substitutions[i]);
6+
}
7+
result += template[template.length - 1];
8+
return result;
9+
}

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,7 @@ public void basicInterfaceTest() {
263263
Assert.assertTrue(errorMessage, output.contains("interface RestApplication"));
264264
Assert.assertTrue(errorMessage, output.contains("getA(): RestResponse<A>;"));
265265
Assert.assertTrue(errorMessage, output.contains("type RestResponse<R> = Promise<R>;"));
266+
Assert.assertTrue(errorMessage, !output.contains("function uriEncoding"));
266267
}
267268

268269
@Test
@@ -322,8 +323,10 @@ public void basicClientTest() {
322323
// application client
323324
Assert.assertTrue(errorMessage, output.contains("class OrganizationApplicationClient"));
324325
Assert.assertTrue(errorMessage, output.contains("getPerson(personId: number): RestResponse<Person>"));
325-
Assert.assertTrue(errorMessage, output.contains("return this.httpClient.request({ method: \"GET\", url: `api/people/${personId}` });"));
326+
Assert.assertTrue(errorMessage, output.contains("return this.httpClient.request({ method: \"GET\", url: uriEncoding`api/people/${personId}` });"));
326327
Assert.assertTrue(errorMessage, output.contains("type RestResponse<R> = Promise<R>;"));
328+
// helper
329+
Assert.assertTrue(errorMessage, output.contains("function uriEncoding"));
327330
}
328331

329332
@Test
@@ -340,7 +343,7 @@ public void clientCustomizationTest() {
340343
// application client
341344
Assert.assertTrue(errorMessage, output.contains("class OrganizationApplicationClient"));
342345
Assert.assertTrue(errorMessage, output.contains("getPerson(personId: number, options?: AxiosRequestConfig): RestResponse<Person>"));
343-
Assert.assertTrue(errorMessage, output.contains("return this.httpClient.request({ method: \"GET\", url: `api/people/${personId}`, options: options });"));
346+
Assert.assertTrue(errorMessage, output.contains("return this.httpClient.request({ method: \"GET\", url: uriEncoding`api/people/${personId}`, options: options });"));
344347
Assert.assertTrue(errorMessage, output.contains("type RestResponse<R> = AxiosPromise;"));
345348
}
346349

0 commit comments

Comments
 (0)