Skip to content

Commit a63e3f1

Browse files
defmonk0wing328
authored andcommitted
[TypeScript-Angular] Path URI Encoding Update (#6769)
* Replaced the method for updating path to prep for URL encoding. The new method will switch TypeScript-Angular variables from snake_case to camelCase in the URL generation. Imported StringBuffer, Matcher, and Pattern, since the new solution needs them. Some extra whitespace on blank lines was removed. * Since these were not up to date with the current master, I ran them and am commiting them here. This way, the changes are shown here instead of after future commits. * Simplified the code for the path conversion A LOT. New version is much simpler to follow, and very efficient - only one iteration through the length of the string. Removed regex Matcher and Pattern classes, since they weren't needed anymore.
1 parent c6b6249 commit a63e3f1

File tree

7 files changed

+66
-15
lines changed

7 files changed

+66
-15
lines changed

modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/TypeScriptAngularClientCodegen.java

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.swagger.codegen.languages;
22

33
import java.io.File;
4+
import java.lang.StringBuffer;
45
import java.text.SimpleDateFormat;
56
import java.util.ArrayList;
67
import java.util.Date;
@@ -82,7 +83,7 @@ public void processOpts() {
8283
supportingFiles.add(new SupportingFile("apis.mustache", apiPackage().replace('.', File.separatorChar), "api.ts"));
8384
supportingFiles.add(new SupportingFile("index.mustache", getIndexDirectory(), "index.ts"));
8485
supportingFiles.add(new SupportingFile("api.module.mustache", getIndexDirectory(), "api.module.ts"));
85-
supportingFiles.add(new SupportingFile("rxjs-operators.mustache", getIndexDirectory(), "rxjs-operators.ts"));
86+
supportingFiles.add(new SupportingFile("rxjs-operators.mustache", getIndexDirectory(), "rxjs-operators.ts"));
8687
supportingFiles.add(new SupportingFile("configuration.mustache", getIndexDirectory(), "configuration.ts"));
8788
supportingFiles.add(new SupportingFile("variables.mustache", getIndexDirectory(), "variables.ts"));
8889
supportingFiles.add(new SupportingFile("encoder.mustache", getIndexDirectory(), "encoder.ts"));
@@ -149,7 +150,7 @@ private String getIndexDirectory() {
149150
public boolean isDataTypeFile(final String dataType) {
150151
return dataType != null && dataType.equals("Blob");
151152
}
152-
153+
153154
@Override
154155
public String getTypeDeclaration(Property p) {
155156
Property inner;
@@ -247,8 +248,54 @@ public Map<String, Object> postProcessOperations(Map<String, Object> operations)
247248
}
248249
}
249250

250-
// Convert path to TypeScript template string, applying URI encoding
251-
op.path = op.path.replaceAll("\\{(.*?)\\}", "\\$\\{encodeURIComponent(String($1))\\}");
251+
// Prep a string buffer where we're going to set up our new version of the string.
252+
StringBuffer pathBuffer = new StringBuffer();
253+
254+
// Set up other variables for tracking the current state of the string.
255+
int insideCurly = 0;
256+
boolean foundUnderscore = false;
257+
258+
// Iterate through existing string, one character at a time.
259+
for(int i = 0; i < op.path.length(); i++) {
260+
switch(op.path.charAt(i)) {
261+
case '{':
262+
// We entered curly braces, so track that.
263+
insideCurly++;
264+
265+
// Add the more complicated component instead of just the brace.
266+
pathBuffer.append("${encodeURIComponent(String(");
267+
break;
268+
case '}':
269+
// We exited curly braces, so track that.
270+
insideCurly--;
271+
272+
// Add the more complicated component instead of just the brace.
273+
pathBuffer.append("))}");
274+
break;
275+
case '_':
276+
// If we're inside the curly brace, the following character will need to be uppercase.
277+
// Otherwise, just add the character.
278+
if (insideCurly > 0) {
279+
foundUnderscore = true;
280+
} else {
281+
pathBuffer.append(op.path.charAt(i));
282+
}
283+
break;
284+
default:
285+
// If we previously found an underscore, we need an uppercase letter.
286+
// Otherwise, just add the character.
287+
if (foundUnderscore) {
288+
pathBuffer.append(Character.toUpperCase(op.path.charAt(i)));
289+
foundUnderscore = false;
290+
} else {
291+
pathBuffer.append(op.path.charAt(i));
292+
}
293+
break;
294+
}
295+
}
296+
297+
// Overwrite path to TypeScript template string, after applying everything we just did.
298+
op.path = pathBuffer.toString();
252299
}
253300

254301
// Add additional filename information for model imports in the services
@@ -272,7 +319,7 @@ public Map<String, Object> postProcessModels(Map<String, Object> objs) {
272319
CodegenModel cm = (CodegenModel) mo.get("model");
273320
mo.put("tsImports", toTsImports(cm,cm.imports));
274321
}
275-
322+
276323
return result;
277324
}
278325

samples/client/petstore/typescript-angular-v4.3/npm/model/apiResponse.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,5 @@ export interface ApiResponse {
2323
message?: string;
2424

2525
}
26+
27+

samples/client/petstore/typescript-angular-v4.3/npm/model/category.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,5 @@ export interface Category {
2121
name?: string;
2222

2323
}
24+
25+

samples/client/petstore/typescript-angular-v4.3/npm/model/order.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,7 @@ export interface Order {
3333

3434
}
3535
export namespace Order {
36-
export enum StatusEnum {
37-
Placed = <any> 'placed',
38-
Approved = <any> 'approved',
39-
Delivered = <any> 'delivered'
40-
}
36+
export type StatusEnum = 'placed' | 'approved' | 'delivered';
4137
}
38+
39+

samples/client/petstore/typescript-angular-v4.3/npm/model/pet.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,7 @@ export interface Pet {
3535

3636
}
3737
export namespace Pet {
38-
export enum StatusEnum {
39-
Available = <any> 'available',
40-
Pending = <any> 'pending',
41-
Sold = <any> 'sold'
42-
}
38+
export type StatusEnum = 'available' | 'pending' | 'sold';
4339
}
40+
41+

samples/client/petstore/typescript-angular-v4.3/npm/model/tag.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,5 @@ export interface Tag {
2121
name?: string;
2222

2323
}
24+
25+

samples/client/petstore/typescript-angular-v4.3/npm/model/user.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,5 @@ export interface User {
3636
userStatus?: number;
3737

3838
}
39+
40+

0 commit comments

Comments
 (0)