Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TypeScript-Angular] Path URI Encoding Update #6769

Merged
merged 3 commits into from
Oct 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;

}