-
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[csharp] ctor params should always be camelCase (#7519)
* [csharp] ctor params should always be camelCase After PR #6305, var names defaulted to PascalCase results in constructor arguments also being PacalCase. Model properties and constructor arguments have no reason to be the same case, and in fact may cause issues (`name = name` will result in a compilation error). This commit forces all constructor params in models to lowerCase. This is a necessary change, for instance, if client SDK consumers assign using named args: var a = new Model(first = "", second = "") The PacalCase default and update to constructor arg casing will break existing consumers of the client. See #7070 for more details and discussion. * [csharp] Regenerate samples * [csharp] Remove client models generated from a different spec. * [csharp] Escape reserved words on camelcase/lowercase lambdas * [csharp] Regenerate samples
- Loading branch information
1 parent
1139f3f
commit 0e34bcf
Showing
159 changed files
with
1,368 additions
and
1,383 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -216,4 +216,6 @@ public interface CodegenConfig { | |
|
||
String toGetter(String name); | ||
|
||
String sanitizeName(String name); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
modules/swagger-codegen/src/main/java/io/swagger/codegen/mustache/CamelCaseLambda.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package io.swagger.codegen.mustache; | ||
|
||
import com.google.common.base.CaseFormat; | ||
import com.samskivert.mustache.Mustache; | ||
import com.samskivert.mustache.Template; | ||
import io.swagger.codegen.CodegenConfig; | ||
import io.swagger.codegen.DefaultCodegen; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.apache.commons.lang3.text.WordUtils; | ||
|
||
import java.io.IOException; | ||
import java.io.Writer; | ||
|
||
/** | ||
* Converts text in a fragment to camelCase. | ||
* | ||
* Register: | ||
* <pre> | ||
* additionalProperties.put("camelcase", new CamelCaseLambda()); | ||
* </pre> | ||
* | ||
* Use: | ||
* <pre> | ||
* {{#camelcase}}{{name}}{{/camelcase}} | ||
* </pre> | ||
*/ | ||
public class CamelCaseLambda implements Mustache.Lambda { | ||
private CodegenConfig generator = null; | ||
private Boolean escapeParam = false; | ||
|
||
public CamelCaseLambda() { | ||
|
||
} | ||
|
||
public CamelCaseLambda generator(final CodegenConfig generator) { | ||
this.generator = generator; | ||
return this; | ||
} | ||
|
||
public CamelCaseLambda escapeAsParamName(final Boolean escape) { | ||
this.escapeParam = escape; | ||
return this; | ||
} | ||
|
||
@Override | ||
public void execute(Template.Fragment fragment, Writer writer) throws IOException { | ||
String text = DefaultCodegen.camelize(fragment.execute(), true); | ||
if (generator != null) { | ||
text = generator.sanitizeName(text); | ||
if (generator.reservedWords().contains(text)) { | ||
// Escaping must be done *after* camelize, because generators may escape using characters removed by camelize function. | ||
text = generator.escapeReservedWord(text); | ||
} | ||
|
||
if (escapeParam) { | ||
// NOTE: many generators call escapeReservedWord in toParamName, but we can't assume that's always the case. | ||
// Here, we'll have to accept that we may be duplicating some work. | ||
text = generator.toParamName(text); | ||
} | ||
} | ||
writer.write(text); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
modules/swagger-codegen/src/test/java/io/swagger/codegen/mustache/CamelCaseLambdaTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package io.swagger.codegen.mustache; | ||
|
||
import io.swagger.codegen.CodegenConfig; | ||
import io.swagger.codegen.languages.CSharpClientCodegen; | ||
import io.swagger.codegen.languages.ScalaClientCodegen; | ||
import org.testng.annotations.Factory; | ||
import org.testng.annotations.Test; | ||
|
||
import static org.testng.Assert.*; | ||
|
||
public class CamelCaseLambdaTest extends MustacheTestBase { | ||
private final String input; | ||
private final String expected; | ||
private Boolean escapeAsParamName = false; | ||
|
||
private CodegenConfig generator = null; | ||
|
||
public CamelCaseLambdaTest(String input, String expected) { | ||
this.input = input; | ||
this.expected = expected; | ||
} | ||
|
||
public CamelCaseLambdaTest generator(CodegenConfig generator) { | ||
this.generator = generator; | ||
return this; | ||
} | ||
|
||
public CamelCaseLambdaTest escapeAsParamName(Boolean setting) { | ||
this.escapeAsParamName = setting; | ||
return this; | ||
} | ||
|
||
@Test(description = "camelCase expected inputs") | ||
public void testExecute() throws Exception { | ||
// Arrange | ||
String template = "{{#camelcase}}{{value}}{{/camelcase}}"; | ||
Object inputCtx = context( | ||
"camelcase", new CamelCaseLambda().generator(this.generator).escapeAsParamName(this.escapeAsParamName), | ||
"value", this.input | ||
); | ||
|
||
// Act | ||
String actual = compile(template, inputCtx); | ||
|
||
|
||
// Assert | ||
assertEquals(actual, this.expected); | ||
} | ||
|
||
@Factory | ||
public static Object[] factoryMethod() { | ||
return new Object[] { | ||
new CamelCaseLambdaTest("lowercase input", "lowercase input"), | ||
|
||
// NOTE: DefaultCodegen.camelize(string, true) only results in first character of first word being lowercased. | ||
// Keeping this behavior as it will match whatever is expected by existing codegen implementations. | ||
new CamelCaseLambdaTest("UPPERCASE INPUT", "uPPERCASE INPUT"), | ||
new CamelCaseLambdaTest("inputText", "inputText"), | ||
new CamelCaseLambdaTest("input_text", "inputText"), | ||
|
||
// TODO: This result for INPUT_TEXT may be unexpected, but is the result of DefaultCodegen.camelize. | ||
// CamelCaseLambda can be extended to accept a method reference after move to Java 8. | ||
new CamelCaseLambdaTest("INPUT_TEXT", "iNPUTTEXT"), | ||
new CamelCaseLambdaTest("input-text", "inputText"), | ||
new CamelCaseLambdaTest("input-text input-text input-text input-text input-text", "inputText inputText inputText inputText inputText"), | ||
// C# codegen at time of writing this test escapes using a character that would be removed by camelize function. | ||
new CamelCaseLambdaTest("class", "_class").generator(new CSharpClientCodegen()), | ||
new CamelCaseLambdaTest("123List", "_123List").generator(new CSharpClientCodegen()).escapeAsParamName(true), | ||
// Scala codegen is only one at time of writing this test that uses a Mustache.Escaper | ||
new CamelCaseLambdaTest("class", "`class`").generator(new ScalaClientCodegen()) | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.