[scala-sttp] Compilation failed when URI parameter name is not camelCase, fixes #9345 (#9354)

This commit is contained in:
Sheldon Young
2021-05-12 06:16:27 -07:00
committed by GitHub
parent c4c15cecb6
commit be06541b47
2 changed files with 33 additions and 2 deletions

View File

@@ -37,6 +37,8 @@ import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static org.openapitools.codegen.utils.StringUtils.camelize;
@@ -179,8 +181,17 @@ public class ScalaSttpClientCodegen extends AbstractScalaCodegen implements Code
@Override
public String encodePath(String input) {
String result = super.encodePath(input);
return result.replace("{", "${");
String path = super.encodePath(input);
// The parameter names in the URI must be converted to the same case as
// the method parameter.
StringBuffer buf = new StringBuffer(path.length());
Matcher matcher = Pattern.compile("[{](.*?)[}]").matcher(path);
while (matcher.find()) {
matcher.appendReplacement(buf, "\\${" + toParamName(matcher.group(0)) + "}");
}
matcher.appendTail(buf);
return buf.toString();
}
@Override

View File

@@ -0,0 +1,20 @@
package org.openapitools.codegen.scala;
import org.openapitools.codegen.languages.ScalaSttpClientCodegen;
import org.testng.Assert;
import org.testng.annotations.Test;
public class SttpCodegenTest {
private final ScalaSttpClientCodegen codegen = new ScalaSttpClientCodegen();
@Test
public void encodePath() {
Assert.assertEquals(codegen.encodePath("{user_name}"), "${userName}");
Assert.assertEquals(codegen.encodePath("{userName}"), "${userName}");
Assert.assertEquals(codegen.encodePath("{UserName}"), "${userName}");
Assert.assertEquals(codegen.encodePath("user_name"), "user_name");
Assert.assertEquals(codegen.encodePath("before/{UserName}/after"), "before/${userName}/after");
}
}