From 6cb49c8077ab2ef8f36f2fef337317fd31794fbb Mon Sep 17 00:00:00 2001 From: Steve Harrigan Date: Fri, 20 Oct 2017 04:49:43 -0400 Subject: [PATCH 1/3] 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. --- .../typescript-angular-v4.3/npm/model/apiResponse.ts | 2 ++ .../typescript-angular-v4.3/npm/model/category.ts | 2 ++ .../petstore/typescript-angular-v4.3/npm/model/order.ts | 8 +++----- .../petstore/typescript-angular-v4.3/npm/model/pet.ts | 8 +++----- .../petstore/typescript-angular-v4.3/npm/model/tag.ts | 2 ++ .../petstore/typescript-angular-v4.3/npm/model/user.ts | 2 ++ 6 files changed, 14 insertions(+), 10 deletions(-) diff --git a/samples/client/petstore/typescript-angular-v4.3/npm/model/apiResponse.ts b/samples/client/petstore/typescript-angular-v4.3/npm/model/apiResponse.ts index 3af781cf580..e79fabe8c8c 100644 --- a/samples/client/petstore/typescript-angular-v4.3/npm/model/apiResponse.ts +++ b/samples/client/petstore/typescript-angular-v4.3/npm/model/apiResponse.ts @@ -23,3 +23,5 @@ export interface ApiResponse { message?: string; } + + diff --git a/samples/client/petstore/typescript-angular-v4.3/npm/model/category.ts b/samples/client/petstore/typescript-angular-v4.3/npm/model/category.ts index d09f8d7b265..56608731f95 100644 --- a/samples/client/petstore/typescript-angular-v4.3/npm/model/category.ts +++ b/samples/client/petstore/typescript-angular-v4.3/npm/model/category.ts @@ -21,3 +21,5 @@ export interface Category { name?: string; } + + diff --git a/samples/client/petstore/typescript-angular-v4.3/npm/model/order.ts b/samples/client/petstore/typescript-angular-v4.3/npm/model/order.ts index 402a86689c8..84b59f1b09a 100644 --- a/samples/client/petstore/typescript-angular-v4.3/npm/model/order.ts +++ b/samples/client/petstore/typescript-angular-v4.3/npm/model/order.ts @@ -33,9 +33,7 @@ export interface Order { } export namespace Order { - export enum StatusEnum { - Placed = 'placed', - Approved = 'approved', - Delivered = 'delivered' - } + export type StatusEnum = 'placed' | 'approved' | 'delivered'; } + + diff --git a/samples/client/petstore/typescript-angular-v4.3/npm/model/pet.ts b/samples/client/petstore/typescript-angular-v4.3/npm/model/pet.ts index 0d6137d02cf..c0fc942934c 100644 --- a/samples/client/petstore/typescript-angular-v4.3/npm/model/pet.ts +++ b/samples/client/petstore/typescript-angular-v4.3/npm/model/pet.ts @@ -35,9 +35,7 @@ export interface Pet { } export namespace Pet { - export enum StatusEnum { - Available = 'available', - Pending = 'pending', - Sold = 'sold' - } + export type StatusEnum = 'available' | 'pending' | 'sold'; } + + diff --git a/samples/client/petstore/typescript-angular-v4.3/npm/model/tag.ts b/samples/client/petstore/typescript-angular-v4.3/npm/model/tag.ts index 3ed1eeff8f3..dbfa473f538 100644 --- a/samples/client/petstore/typescript-angular-v4.3/npm/model/tag.ts +++ b/samples/client/petstore/typescript-angular-v4.3/npm/model/tag.ts @@ -21,3 +21,5 @@ export interface Tag { name?: string; } + + diff --git a/samples/client/petstore/typescript-angular-v4.3/npm/model/user.ts b/samples/client/petstore/typescript-angular-v4.3/npm/model/user.ts index f4914ae2608..4dfad607c51 100644 --- a/samples/client/petstore/typescript-angular-v4.3/npm/model/user.ts +++ b/samples/client/petstore/typescript-angular-v4.3/npm/model/user.ts @@ -36,3 +36,5 @@ export interface User { userStatus?: number; } + + From 7038c33eee3cef90cce551adbfbaf33c555bab09 Mon Sep 17 00:00:00 2001 From: Steve Harrigan Date: Fri, 20 Oct 2017 04:30:38 -0400 Subject: [PATCH 2/3] 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. --- .../TypeScriptAngularClientCodegen.java | 60 +++++++++++++++++-- 1 file changed, 55 insertions(+), 5 deletions(-) diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/TypeScriptAngularClientCodegen.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/TypeScriptAngularClientCodegen.java index cab99599846..22a7cb7880a 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/TypeScriptAngularClientCodegen.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/TypeScriptAngularClientCodegen.java @@ -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; @@ -8,6 +9,8 @@ import java.util.List; import java.util.Locale; import java.util.Map; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import java.util.Set; import io.swagger.codegen.CliOption; @@ -82,7 +85,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")); @@ -149,7 +152,7 @@ private String getIndexDirectory() { public boolean isDataTypeFile(final String dataType) { return dataType != null && dataType.equals("Blob"); } - + @Override public String getTypeDeclaration(Property p) { Property inner; @@ -247,8 +250,55 @@ public Map postProcessOperations(Map operations) } } - // Convert path to TypeScript template string, applying URI encoding - op.path = op.path.replaceAll("\\{(.*?)\\}", "\\$\\{encodeURIComponent(String($1))\\}"); + // Generate the regex objects needed to parse the path properly. + Pattern pathPattern = Pattern.compile("\\{(.*?)\\}"); + Matcher pathMatcher = pathPattern.matcher(op.path); + + // Prep a string buffer where we're going to set up our new version of the string. + StringBuffer pathBuffer = new StringBuffer(); + + // Iterate through the matches for the regex. + // This should be anything that was contained inside curly-braces. + while(pathMatcher.find()) { + // Get a substring of our match. + String templateSection = op.path.substring(pathMatcher.start(), pathMatcher.end()); + + // Generate regex objects again, this time for the substring. + // We needed to nest these, so we didn't accidentally find matches outside the curly-braces. + Pattern templateSectionPattern = Pattern.compile("\\_(\\w)"); + Matcher templateSectionMatcher = templateSectionPattern.matcher(templateSection); + + // Prep a string buffer where we're going to set up our new version of the string. + StringBuffer templateSectionBuffer = new StringBuffer(); + + // Iterate through the matches for the regex. + // This should be any underscores followed by a single character. + while(templateSectionMatcher.find()) { + // Add the piece of the string up to our current match. + templateSectionMatcher.appendReplacement(templateSectionBuffer, ""); + + // Grab the match itself. + String underscoreSection = templateSection.substring(templateSectionMatcher.start(), templateSectionMatcher.end()); + + // Add the capitalized character while leaving out the underscore. + templateSectionBuffer.append(underscoreSection.substring(1).toUpperCase()); + } + + // Append the rest of the string after our final match. + templateSectionMatcher.appendTail(templateSectionBuffer); + + // Replace the substring in path with the new, correctly formatted section. + String replacement = templateSectionBuffer.toString(); + replacement = replacement.substring(1, replacement.length() - 1); + replacement = "\\$\\{encodeURIComponent\\(String\\(" + replacement + "\\)\\)\\}"; + pathMatcher.appendReplacement(pathBuffer, replacement); + } + + // Append the rest of the string after our final match. + pathMatcher.appendTail(pathBuffer); + + // 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 @@ -272,7 +322,7 @@ public Map postProcessModels(Map objs) { CodegenModel cm = (CodegenModel) mo.get("model"); mo.put("tsImports", toTsImports(cm,cm.imports)); } - + return result; } From 2a6b5cf371765658d8579956a34cb2e827382ec5 Mon Sep 17 00:00:00 2001 From: Steve Harrigan Date: Fri, 20 Oct 2017 20:26:51 -0400 Subject: [PATCH 3/3] 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. --- .../TypeScriptAngularClientCodegen.java | 77 +++++++++---------- 1 file changed, 37 insertions(+), 40 deletions(-) diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/TypeScriptAngularClientCodegen.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/TypeScriptAngularClientCodegen.java index 22a7cb7880a..fefbf143b0c 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/TypeScriptAngularClientCodegen.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/TypeScriptAngularClientCodegen.java @@ -9,8 +9,6 @@ import java.util.List; import java.util.Locale; import java.util.Map; -import java.util.regex.Matcher; -import java.util.regex.Pattern; import java.util.Set; import io.swagger.codegen.CliOption; @@ -250,53 +248,52 @@ public Map postProcessOperations(Map operations) } } - // Generate the regex objects needed to parse the path properly. - Pattern pathPattern = Pattern.compile("\\{(.*?)\\}"); - Matcher pathMatcher = pathPattern.matcher(op.path); - // Prep a string buffer where we're going to set up our new version of the string. StringBuffer pathBuffer = new StringBuffer(); - // Iterate through the matches for the regex. - // This should be anything that was contained inside curly-braces. - while(pathMatcher.find()) { - // Get a substring of our match. - String templateSection = op.path.substring(pathMatcher.start(), pathMatcher.end()); - - // Generate regex objects again, this time for the substring. - // We needed to nest these, so we didn't accidentally find matches outside the curly-braces. - Pattern templateSectionPattern = Pattern.compile("\\_(\\w)"); - Matcher templateSectionMatcher = templateSectionPattern.matcher(templateSection); - - // Prep a string buffer where we're going to set up our new version of the string. - StringBuffer templateSectionBuffer = new StringBuffer(); + // Set up other variables for tracking the current state of the string. + int insideCurly = 0; + boolean foundUnderscore = false; - // Iterate through the matches for the regex. - // This should be any underscores followed by a single character. - while(templateSectionMatcher.find()) { - // Add the piece of the string up to our current match. - templateSectionMatcher.appendReplacement(templateSectionBuffer, ""); + // 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++; - // Grab the match itself. - String underscoreSection = templateSection.substring(templateSectionMatcher.start(), templateSectionMatcher.end()); + // 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 capitalized character while leaving out the underscore. - templateSectionBuffer.append(underscoreSection.substring(1).toUpperCase()); + // 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; } - - // Append the rest of the string after our final match. - templateSectionMatcher.appendTail(templateSectionBuffer); - - // Replace the substring in path with the new, correctly formatted section. - String replacement = templateSectionBuffer.toString(); - replacement = replacement.substring(1, replacement.length() - 1); - replacement = "\\$\\{encodeURIComponent\\(String\\(" + replacement + "\\)\\)\\}"; - pathMatcher.appendReplacement(pathBuffer, replacement); } - // Append the rest of the string after our final match. - pathMatcher.appendTail(pathBuffer); - // Overwrite path to TypeScript template string, after applying everything we just did. op.path = pathBuffer.toString(); }