Skip to content

Commit

Permalink
[scala-sttp] Compilation failed when URI parameter name is not camelC…
Browse files Browse the repository at this point in the history
…ase, fixes #9345 (#9354)
  • Loading branch information
softdevca committed May 12, 2021
1 parent c4c15ce commit be06541
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
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;

Expand Down Expand Up @@ -179,8 +181,17 @@ public String getHelp() {

@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
Expand Down
Original file line number Diff line number Diff line change
@@ -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");
}

}

0 comments on commit be06541

Please sign in to comment.