Skip to content

Commit

Permalink
[TypeScript-Angular] Path URI Encoding Update (#6769)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
defmonk0 authored and wing328 committed Oct 23, 2017
1 parent c6b6249 commit a63e3f1
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.swagger.codegen.languages;

import java.io.File;
import java.lang.StringBuffer;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
Expand Down Expand Up @@ -82,7 +83,7 @@ public void processOpts() {
supportingFiles.add(new SupportingFile("apis.mustache", apiPackage().replace('.', File.separatorChar), "api.ts"));
supportingFiles.add(new SupportingFile("index.mustache", getIndexDirectory(), "index.ts"));
supportingFiles.add(new SupportingFile("api.module.mustache", getIndexDirectory(), "api.module.ts"));
supportingFiles.add(new SupportingFile("rxjs-operators.mustache", getIndexDirectory(), "rxjs-operators.ts"));
supportingFiles.add(new SupportingFile("rxjs-operators.mustache", getIndexDirectory(), "rxjs-operators.ts"));
supportingFiles.add(new SupportingFile("configuration.mustache", getIndexDirectory(), "configuration.ts"));
supportingFiles.add(new SupportingFile("variables.mustache", getIndexDirectory(), "variables.ts"));
supportingFiles.add(new SupportingFile("encoder.mustache", getIndexDirectory(), "encoder.ts"));
Expand Down Expand Up @@ -149,7 +150,7 @@ private String getIndexDirectory() {
public boolean isDataTypeFile(final String dataType) {
return dataType != null && dataType.equals("Blob");
}

@Override
public String getTypeDeclaration(Property p) {
Property inner;
Expand Down Expand Up @@ -247,8 +248,54 @@ public Map<String, Object> postProcessOperations(Map<String, Object> operations)
}
}

// Convert path to TypeScript template string, applying URI encoding
op.path = op.path.replaceAll("\\{(.*?)\\}", "\\$\\{encodeURIComponent(String($1))\\}");
// Prep a string buffer where we're going to set up our new version of the string.
StringBuffer pathBuffer = new StringBuffer();

// Set up other variables for tracking the current state of the string.
int insideCurly = 0;
boolean foundUnderscore = false;

// Iterate through existing string, one character at a time.
for(int i = 0; i < op.path.length(); i++) {
switch(op.path.charAt(i)) {
case '{':
// We entered curly braces, so track that.
insideCurly++;

// Add the more complicated component instead of just the brace.
pathBuffer.append("${encodeURIComponent(String(");
break;
case '}':
// We exited curly braces, so track that.
insideCurly--;

// Add the more complicated component instead of just the brace.
pathBuffer.append("))}");
break;
case '_':
// If we're inside the curly brace, the following character will need to be uppercase.
// Otherwise, just add the character.
if (insideCurly > 0) {
foundUnderscore = true;
} else {
pathBuffer.append(op.path.charAt(i));
}
break;
default:
// If we previously found an underscore, we need an uppercase letter.
// Otherwise, just add the character.
if (foundUnderscore) {
pathBuffer.append(Character.toUpperCase(op.path.charAt(i)));
foundUnderscore = false;
} else {
pathBuffer.append(op.path.charAt(i));
}
break;
}
}

// Overwrite path to TypeScript template string, after applying everything we just did.
op.path = pathBuffer.toString();
}

// Add additional filename information for model imports in the services
Expand All @@ -272,7 +319,7 @@ public Map<String, Object> postProcessModels(Map<String, Object> objs) {
CodegenModel cm = (CodegenModel) mo.get("model");
mo.put("tsImports", toTsImports(cm,cm.imports));
}

return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,5 @@ export interface ApiResponse {
message?: string;

}


Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,5 @@ export interface Category {
name?: string;

}


Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ export interface Order {

}
export namespace Order {
export enum StatusEnum {
Placed = <any> 'placed',
Approved = <any> 'approved',
Delivered = <any> 'delivered'
}
export type StatusEnum = 'placed' | 'approved' | 'delivered';
}


Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ export interface Pet {

}
export namespace Pet {
export enum StatusEnum {
Available = <any> 'available',
Pending = <any> 'pending',
Sold = <any> 'sold'
}
export type StatusEnum = 'available' | 'pending' | 'sold';
}


Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,5 @@ export interface Tag {
name?: string;

}


Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,5 @@ export interface User {
userStatus?: number;

}


0 comments on commit a63e3f1

Please sign in to comment.