forked from loafle/openapi-generator-original
[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
This commit is contained in:
committed by
William Cheng
parent
1139f3f053
commit
0e34bcf4e4
@@ -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())
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
package io.swagger.codegen.mustache;
|
||||
|
||||
import io.swagger.codegen.languages.CSharpClientCodegen;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import static org.testng.Assert.*;
|
||||
@@ -28,4 +29,21 @@ public class LowercaseLambdaTest extends MustacheTestBase {
|
||||
assertEquals(lowercaseResult, "lowercase input");
|
||||
assertEquals(uppercaseResult, "uppercase input");
|
||||
}
|
||||
|
||||
@Test(description = "lowercase escapes reserved words")
|
||||
public void testEscapingReservedWords() {
|
||||
// Arrange
|
||||
String template = "{{#lowercase}}{{value}}{{/lowercase}}";
|
||||
String expected = "_class";
|
||||
Object ctx = context(
|
||||
"lowercase", new LowercaseLambda().generator(new CSharpClientCodegen()),
|
||||
"value", "CLASS"
|
||||
);
|
||||
|
||||
// Act
|
||||
String actual = compile(template, ctx);
|
||||
|
||||
// Assert
|
||||
assertEquals(actual, expected);
|
||||
}
|
||||
}
|
||||
@@ -38,12 +38,12 @@ namespace IO.Swagger.Model
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="MyClassWithInvalidRequiredEnumUsageOnRef" /> class.
|
||||
/// </summary>
|
||||
/// <param name="First">First.</param>
|
||||
/// <param name="Days">Days.</param>
|
||||
public MyClassWithInvalidRequiredEnumUsageOnRef(bool? First = default(bool?), WeekDays? Days = default(WeekDays?))
|
||||
/// <param name="first">first.</param>
|
||||
/// <param name="days">days.</param>
|
||||
public MyClassWithInvalidRequiredEnumUsageOnRef(bool? first = default(bool?), WeekDays? days = default(WeekDays?))
|
||||
{
|
||||
this.First = First;
|
||||
this.Days = Days;
|
||||
this.First = first;
|
||||
this.Days = days;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -38,14 +38,14 @@ namespace IO.Swagger.Model
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="MyClassWithOptionalEnum" /> class.
|
||||
/// </summary>
|
||||
/// <param name="Quarantine">Quarantine.</param>
|
||||
/// <param name="Grayware">Grayware.</param>
|
||||
/// <param name="Days">Days.</param>
|
||||
public MyClassWithOptionalEnum(bool? Quarantine = default(bool?), bool? Grayware = default(bool?), WeekDays? Days = default(WeekDays?))
|
||||
/// <param name="quarantine">quarantine.</param>
|
||||
/// <param name="grayware">grayware.</param>
|
||||
/// <param name="days">days.</param>
|
||||
public MyClassWithOptionalEnum(bool? quarantine = default(bool?), bool? grayware = default(bool?), WeekDays? days = default(WeekDays?))
|
||||
{
|
||||
this.Quarantine = Quarantine;
|
||||
this.Grayware = Grayware;
|
||||
this.Days = Days;
|
||||
this.Quarantine = quarantine;
|
||||
this.Grayware = grayware;
|
||||
this.Days = days;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -88,14 +88,14 @@ namespace IO.Swagger.Model
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="MyClassWithOptionalInlineEnum" /> class.
|
||||
/// </summary>
|
||||
/// <param name="Quarantine">Quarantine.</param>
|
||||
/// <param name="Grayware">Grayware.</param>
|
||||
/// <param name="Days">Days.</param>
|
||||
public MyClassWithOptionalInlineEnum(bool? Quarantine = default(bool?), bool? Grayware = default(bool?), DaysEnum? Days = default(DaysEnum?))
|
||||
/// <param name="quarantine">quarantine.</param>
|
||||
/// <param name="grayware">grayware.</param>
|
||||
/// <param name="days">days.</param>
|
||||
public MyClassWithOptionalInlineEnum(bool? quarantine = default(bool?), bool? grayware = default(bool?), DaysEnum? days = default(DaysEnum?))
|
||||
{
|
||||
this.Quarantine = Quarantine;
|
||||
this.Grayware = Grayware;
|
||||
this.Days = Days;
|
||||
this.Quarantine = quarantine;
|
||||
this.Grayware = grayware;
|
||||
this.Days = days;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -93,22 +93,22 @@ namespace IO.Swagger.Model
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="MyClassWithRequiredInlineEnum" /> class.
|
||||
/// </summary>
|
||||
/// <param name="Quarantine">Quarantine.</param>
|
||||
/// <param name="Grayware">Grayware.</param>
|
||||
/// <param name="Days">Days (required).</param>
|
||||
public MyClassWithRequiredInlineEnum(bool? Quarantine = default(bool?), bool? Grayware = default(bool?), DaysEnum Days = default(DaysEnum))
|
||||
/// <param name="quarantine">quarantine.</param>
|
||||
/// <param name="grayware">grayware.</param>
|
||||
/// <param name="days">days (required).</param>
|
||||
public MyClassWithRequiredInlineEnum(bool? quarantine = default(bool?), bool? grayware = default(bool?), DaysEnum days = default(DaysEnum))
|
||||
{
|
||||
// to ensure "Days" is required (not null)
|
||||
if (Days == null)
|
||||
// to ensure "days" is required (not null)
|
||||
if (days == null)
|
||||
{
|
||||
throw new InvalidDataException("Days is a required property for MyClassWithRequiredInlineEnum and cannot be null");
|
||||
throw new InvalidDataException("days is a required property for MyClassWithRequiredInlineEnum and cannot be null");
|
||||
}
|
||||
else
|
||||
{
|
||||
this.Days = Days;
|
||||
this.Days = days;
|
||||
}
|
||||
this.Quarantine = Quarantine;
|
||||
this.Grayware = Grayware;
|
||||
this.Quarantine = quarantine;
|
||||
this.Grayware = grayware;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
Reference in New Issue
Block a user