Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public class AspNetCoreServerCodegen extends AbstractCSharpCodegen {
public static final String GENERATE_BODY = "generateBody";
public static final String BUILD_TARGET = "buildTarget";
public static final String MODEL_CLASS_MODIFIER = "modelClassModifier";
public static final String TARGET_FRAMEWORK= "targetFramework";
public static final String TARGET_FRAMEWORK = "targetFramework";

public static final String PROJECT_SDK = "projectSdk";
public static final String SDK_WEB = "Microsoft.NET.Sdk.Web";
Expand Down Expand Up @@ -150,8 +150,8 @@ public AspNetCoreServerCodegen() {

// CLI options
addOption(CodegenConstants.PACKAGE_DESCRIPTION,
CodegenConstants.PACKAGE_DESCRIPTION_DESC,
packageDescription);
CodegenConstants.PACKAGE_DESCRIPTION_DESC,
packageDescription);

addOption(CodegenConstants.LICENSE_URL,
CodegenConstants.LICENSE_URL_DESC,
Expand Down Expand Up @@ -472,10 +472,9 @@ public Map<String, Object> postProcessOperationsWithModels(Map<String, Object> o
continue;
}

if(consumesString.toString().isEmpty()) {
if (consumesString.toString().isEmpty()) {
consumesString = new StringBuilder("\"" + consume.get("mediaType") + "\"");
}
else {
} else {
consumesString.append(", \"").append(consume.get("mediaType")).append("\"");
}

Expand All @@ -500,7 +499,7 @@ public Map<String, Object> postProcessOperationsWithModels(Map<String, Object> o
}
}

if(!consumesString.toString().isEmpty()) {
if (!consumesString.toString().isEmpty()) {
operation.vendorExtensions.put("x-aspnetcore-consumes", consumesString.toString());
}
}
Expand Down Expand Up @@ -596,7 +595,7 @@ private void setModelClassModifier() {
private void setBuildTarget() {
setCliOption(buildTarget);
if ("library".equals(buildTarget.getOptValue())) {
LOGGER.warn("buildTarget is {} so changing default isLibrary to true", buildTarget.getOptValue());
LOGGER.warn("buildTarget is {} so changing default isLibrary to true", buildTarget.getOptValue());
isLibrary = true;
projectSdk = SDK_LIB;
additionalProperties.put(CLASS_MODIFIER, "abstract");
Expand All @@ -619,7 +618,7 @@ private void setAspnetCoreVersion(String packageFolder) {
compatibilityVersion = "Version_" + aspnetCoreVersion.getOptValue().replace(".", "_");
}
LOGGER.info("ASP.NET core version: {}", aspnetCoreVersion.getOptValue());
if(!additionalProperties.containsKey(CodegenConstants.TEMPLATE_DIR)){
if (!additionalProperties.containsKey(CodegenConstants.TEMPLATE_DIR)) {
templateDir = embeddedTemplateDir = "aspnetcore" + File.separator + determineTemplateVersion(aspnetCoreVersion.getOptValue());
}
additionalProperties.put(COMPATIBILITY_VERSION, compatibilityVersion);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ public CSharpNetCoreClientCodegen() {
this.packageName);

addOption(CodegenConstants.API_NAME,
"Must be a valid C# class name. Only used in Generic Host library. Default: " + this.apiName,
"Must be a valid C# class name. Only used in Generic Host library. Default: " + this.apiName,
this.apiName);

addOption(CodegenConstants.PACKAGE_VERSION,
Expand Down Expand Up @@ -275,7 +275,7 @@ public CSharpNetCoreClientCodegen() {
addSwitch(CodegenConstants.OPTIONAL_EMIT_DEFAULT_VALUES,
CodegenConstants.OPTIONAL_EMIT_DEFAULT_VALUES_DESC,
this.optionalEmitDefaultValuesFlag);

addSwitch(CodegenConstants.OPTIONAL_CONDITIONAL_SERIALIZATION,
CodegenConstants.OPTIONAL_CONDITIONAL_SERIALIZATION_DESC,
this.conditionalSerialization);
Expand Down Expand Up @@ -334,9 +334,9 @@ public CSharpNetCoreClientCodegen() {

@Override
public String apiDocFileFolder() {
if (GENERICHOST.equals(getLibrary())){
if (GENERICHOST.equals(getLibrary())) {
return (outputFolder + "/" + apiDocPath + File.separatorChar + "apis").replace('/', File.separatorChar);
}else{
} else {
return (outputFolder + "/" + apiDocPath).replace('/', File.separatorChar);
}
}
Expand Down Expand Up @@ -396,27 +396,27 @@ public CodegenModel fromModel(String name, Schema model) {
}

// avoid breaking changes
if (GENERICHOST.equals(getLibrary())){
if (GENERICHOST.equals(getLibrary())) {
Comparator<CodegenProperty> comparatorByDefaultValue = new Comparator<CodegenProperty>() {
@Override
@Override
public int compare(CodegenProperty one, CodegenProperty another) {
if (one.defaultValue == another.defaultValue)
if (one.defaultValue == another.defaultValue)
return 0;
else if (Boolean.FALSE.equals(one.defaultValue))
else if (Boolean.FALSE.equals(one.defaultValue))
return -1;
else
else
return 1;
}
};

Comparator<CodegenProperty> comparatorByRequired = new Comparator<CodegenProperty>() {
@Override
public int compare(CodegenProperty one, CodegenProperty another) {
if (one.required == another.required)
if (one.required == another.required)
return 0;
else if (Boolean.TRUE.equals(one.required))
return -1;
else
else
return 1;
}
};
Expand Down Expand Up @@ -502,9 +502,9 @@ public void setNonPublicApi(final boolean nonPublicApi) {

@Override
public String modelDocFileFolder() {
if (GENERICHOST.equals(getLibrary())){
if (GENERICHOST.equals(getLibrary())) {
return (outputFolder + "/" + modelDocPath + File.separator + "models").replace('/', File.separatorChar);
}else{
} else {
return (outputFolder + "/" + modelDocPath).replace('/', File.separatorChar);
}
}
Expand Down Expand Up @@ -630,7 +630,7 @@ public void processOpts() {

clientPackage = "Client";

if (GENERICHOST.equals(getLibrary())){
if (GENERICHOST.equals(getLibrary())) {
setLibrary(GENERICHOST);
additionalProperties.put("useGenericHost", true);
// all c# libraries should be doing this, but we only do it here to avoid breaking changes
Expand All @@ -656,7 +656,7 @@ public void processOpts() {
additionalProperties.put("multiTarget", true);
} else {
// just a single value
frameworks = new String [] {inputFramework};
frameworks = new String[]{inputFramework};
}

for (String framework : frameworks) {
Expand All @@ -668,14 +668,14 @@ public void processOpts() {
}

if (frameworkStrategy != FrameworkStrategy.NETSTANDARD_2_0 && "restsharp".equals(getLibrary())) {
LOGGER.warn("If using built-in templates, RestSharp only supports netstandard 2.0 or later.");
LOGGER.warn("If using built-in templates, RestSharp only supports netstandard 2.0 or later.");
}
}

if (!strategyMatched) {
// throws exception if the input targetFramework is invalid
throw new IllegalArgumentException("The input (" + inputFramework + ") contains Invalid .NET framework version: " +
framework + ". List of supported versions: " +
framework + ". List of supported versions: " +
frameworkStrategies.stream()
.map(p -> p.name)
.collect(Collectors.joining(", ")));
Expand All @@ -693,13 +693,13 @@ public void processOpts() {
if (!netStandard) {
setNetCoreProjectFileFlag(true);

if (!additionalProperties.containsKey(CodegenConstants.NULLABLE_REFERENCE_TYPES) && !strategies.stream().anyMatch(s ->
s.equals(FrameworkStrategy.NETCOREAPP_2_0) ||
s.equals(FrameworkStrategy.NETCOREAPP_2_1) ||
s.equals(FrameworkStrategy.NETCOREAPP_3_0) ||
s.equals(FrameworkStrategy.NETCOREAPP_3_1) ||
s.equals(FrameworkStrategy.NET_5_0) ||
s.equals(FrameworkStrategy.NETFRAMEWORK_4_7))) {
if (!additionalProperties.containsKey(CodegenConstants.NULLABLE_REFERENCE_TYPES) && !strategies.stream().anyMatch(s ->
s.equals(FrameworkStrategy.NETCOREAPP_2_0) ||
s.equals(FrameworkStrategy.NETCOREAPP_2_1) ||
s.equals(FrameworkStrategy.NETCOREAPP_3_0) ||
s.equals(FrameworkStrategy.NETCOREAPP_3_1) ||
s.equals(FrameworkStrategy.NET_5_0) ||
s.equals(FrameworkStrategy.NETFRAMEWORK_4_7))) {
// starting in .net 6.0, NRT is enabled by default. If not specified, lets enable NRT to match the framework's default
setNullableReferenceTypes(true);
}
Expand Down Expand Up @@ -749,19 +749,17 @@ public void processOpts() {
apiTestTemplateFiles.put("api_test.mustache", ".cs");
}

if(HTTPCLIENT.equals(getLibrary())) {
if (HTTPCLIENT.equals(getLibrary())) {
supportingFiles.add(new SupportingFile("FileParameter.mustache", clientPackageDir, "FileParameter.cs"));
typeMapping.put("file", "FileParameter");
addRestSharpSupportingFiles(clientPackageDir, packageFolder, excludeTests, testPackageFolder, testPackageName, modelPackageDir);
additionalProperties.put("apiDocPath", apiDocPath);
additionalProperties.put("modelDocPath", modelDocPath);
}
else if (GENERICHOST.equals(getLibrary())){
} else if (GENERICHOST.equals(getLibrary())) {
addGenericHostSupportingFiles(clientPackageDir, packageFolder, excludeTests, testPackageFolder, testPackageName, modelPackageDir);
additionalProperties.put("apiDocPath", apiDocPath + File.separatorChar + "apis");
additionalProperties.put("modelDocPath", modelDocPath + File.separatorChar + "models");
}
else{
} else {
addRestSharpSupportingFiles(clientPackageDir, packageFolder, excludeTests, testPackageFolder, testPackageName, modelPackageDir);
additionalProperties.put("apiDocPath", apiDocPath);
additionalProperties.put("modelDocPath", modelDocPath);
Expand All @@ -777,7 +775,7 @@ public CodegenOperation fromOperation(String path,
List<Server> servers) {
CodegenOperation op = super.fromOperation(path, httpMethod, operation, servers);

if (!GENERICHOST.equals(getLibrary())){
if (!GENERICHOST.equals(getLibrary())) {
return op;
}

Expand Down Expand Up @@ -808,32 +806,32 @@ else if (Boolean.TRUE.equals(one.required))
return op;
}

private void addTestInstructions(){
if (GENERICHOST.equals(getLibrary())){
additionalProperties.put("testInstructions",
private void addTestInstructions() {
if (GENERICHOST.equals(getLibrary())) {
additionalProperties.put("testInstructions",
"/* *********************************************************************************" +
"\n* Follow these manual steps to construct tests." +
"\n* This file will not be overwritten." +
"\n* *********************************************************************************" +
"\n* 1. Navigate to ApiTests.Base.cs and ensure any tokens are being created correctly." +
"\n* Take care not to commit credentials to any repository." +
"\n*" +
"\n* 2. Mocking is coordinated by ApiTestsBase#AddApiHttpClients." +
"\n* To mock the client, use the generic AddApiHttpClients." +
"\n* To mock the server, change the client's BaseAddress." +
"\n*" +
"\n* 3. Locate the test you want below" +
"\n* - remove the skip property from the Fact attribute" +
"\n* - set the value of any variables if necessary" +
"\n*" +
"\n* 4. Run the tests and ensure they work." +
"\n*" +
"\n*/");
"\n* Follow these manual steps to construct tests." +
"\n* This file will not be overwritten." +
"\n* *********************************************************************************" +
"\n* 1. Navigate to ApiTests.Base.cs and ensure any tokens are being created correctly." +
"\n* Take care not to commit credentials to any repository." +
"\n*" +
"\n* 2. Mocking is coordinated by ApiTestsBase#AddApiHttpClients." +
"\n* To mock the client, use the generic AddApiHttpClients." +
"\n* To mock the server, change the client's BaseAddress." +
"\n*" +
"\n* 3. Locate the test you want below" +
"\n* - remove the skip property from the Fact attribute" +
"\n* - set the value of any variables if necessary" +
"\n*" +
"\n* 4. Run the tests and ensure they work." +
"\n*" +
"\n*/");
}
}

public void addRestSharpSupportingFiles(final String clientPackageDir, final String packageFolder,
final AtomicReference<Boolean> excludeTests, final String testPackageFolder, final String testPackageName, final String modelPackageDir){
public void addRestSharpSupportingFiles(final String clientPackageDir, final String packageFolder,
final AtomicReference<Boolean> excludeTests, final String testPackageFolder, final String testPackageName, final String modelPackageDir) {
supportingFiles.add(new SupportingFile("IApiAccessor.mustache", clientPackageDir, "IApiAccessor.cs"));
supportingFiles.add(new SupportingFile("Configuration.mustache", clientPackageDir, "Configuration.cs"));
supportingFiles.add(new SupportingFile("ApiClient.mustache", clientPackageDir, "ApiClient.cs"));
Expand All @@ -842,10 +840,10 @@ public void addRestSharpSupportingFiles(final String clientPackageDir, final Str
supportingFiles.add(new SupportingFile("ExceptionFactory.mustache", clientPackageDir, "ExceptionFactory.cs"));
supportingFiles.add(new SupportingFile("OpenAPIDateConverter.mustache", clientPackageDir, "OpenAPIDateConverter.cs"));
supportingFiles.add(new SupportingFile("ClientUtils.mustache", clientPackageDir, "ClientUtils.cs"));
if(needsCustomHttpMethod) {
if (needsCustomHttpMethod) {
supportingFiles.add(new SupportingFile("HttpMethod.mustache", clientPackageDir, "HttpMethod.cs"));
}
if(needsUriBuilder) {
if (needsUriBuilder) {
supportingFiles.add(new SupportingFile("WebRequestPathBuilder.mustache", clientPackageDir, "WebRequestPathBuilder.cs"));
}
if (ProcessUtils.hasHttpSignatureMethods(openAPI)) {
Expand Down Expand Up @@ -882,8 +880,8 @@ public void addRestSharpSupportingFiles(final String clientPackageDir, final Str
supportingFiles.add(new SupportingFile("AbstractOpenAPISchema.mustache", modelPackageDir, "AbstractOpenAPISchema.cs"));
}

public void addGenericHostSupportingFiles(final String clientPackageDir, final String packageFolder,
final AtomicReference<Boolean> excludeTests, final String testPackageFolder, final String testPackageName, final String modelPackageDir){
public void addGenericHostSupportingFiles(final String clientPackageDir, final String packageFolder,
final AtomicReference<Boolean> excludeTests, final String testPackageFolder, final String testPackageName, final String modelPackageDir) {
supportingFiles.add(new SupportingFile("TokenProvider`1.mustache", clientPackageDir, "TokenProvider`1.cs"));
supportingFiles.add(new SupportingFile("RateLimitProvider`1.mustache", clientPackageDir, "RateLimitProvider`1.cs"));
supportingFiles.add(new SupportingFile("TokenContainer`1.mustache", clientPackageDir, "TokenContainer`1.cs"));
Expand Down Expand Up @@ -952,7 +950,7 @@ public void setOptionalEmitDefaultValuesFlag(boolean flag) {
this.optionalEmitDefaultValuesFlag = flag;
}

public void setConditionalSerialization(boolean flag){
public void setConditionalSerialization(boolean flag) {
this.conditionalSerialization = flag;
}

Expand All @@ -970,12 +968,13 @@ public void setPackageName(String packageName) {
this.packageName = packageName;
}

/**
/**
* Sets the api name. This value must be a valid class name.
*
* @param apiName The api name
*/
*/
public void setApiName(String apiName) {
if (!"".equals(apiName) && (Boolean.FALSE.equals(apiName.matches("^[a-zA-Z0-9_]*$")) || Boolean.FALSE.equals(apiName.matches("^[a-zA-Z].*")))){
if (!"".equals(apiName) && (Boolean.FALSE.equals(apiName.matches("^[a-zA-Z0-9_]*$")) || Boolean.FALSE.equals(apiName.matches("^[a-zA-Z].*")))) {
throw new RuntimeException("Invalid project name " + apiName + ". May only contain alphanumeric characaters or underscore and start with a letter.");
}
this.apiName = apiName;
Expand All @@ -1000,8 +999,8 @@ public void setTargetFramework(String dotnetFramework) {
throw new IllegalArgumentException("Invalid .NET framework version: " +
dotnetFramework + ". List of supported versions: " +
frameworkStrategies.stream()
.map(p -> p.name)
.collect(Collectors.joining(", ")));
.map(p -> p.name)
.collect(Collectors.joining(", ")));
} else {
this.targetFramework = dotnetFramework;
}
Expand Down