Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/issue #12366 #1

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,9 @@ samples/client/petstore/csharp/SwaggerClient/bin/Debug/
samples/client/petstore/csharp/SwaggerClient/packages
samples/client/petstore/csharp/SwaggerClient/TestResult.xml
samples/client/petstore/csharp/SwaggerClientWithPropertyChanged/IO.Swagger.userprefs
samples/client/petstore/csharp-dotnet-core/obj/*
samples/client/petstore/csharp-dotnet-core/bin/*
samples/client/petstore/csharp-dotnet-core/.vs/*

# Python
*.pyc
Expand Down
31 changes: 31 additions & 0 deletions bin/csharp-dotnet-core-petstore.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/bin/sh

SCRIPT="$0"

while [ -h "$SCRIPT" ] ; do
ls=`ls -ld "$SCRIPT"`
link=`expr "$ls" : '.*-> \(.*\)$'`
if expr "$link" : '/.*' > /dev/null; then
SCRIPT="$link"
else
SCRIPT=`dirname "$SCRIPT"`/"$link"
fi
done

if [ ! -d "${APP_DIR}" ]; then
APP_DIR=`dirname "$SCRIPT"`/..
APP_DIR=`cd "${APP_DIR}"; pwd`
fi

executable="./modules/swagger-codegen-cli/target/swagger-codegen-cli.jar"

if [ ! -f "$executable" ]
then
mvn clean package
fi

# if you've executed sbt assembly previously it will use that instead.
export JAVA_OPTS="${JAVA_OPTS} -XX:MaxPermSize=256M -Xmx1024M -DloggerPath=conf/log4j.properties"
ags="$@ generate -i modules/swagger-codegen/src/test/resources/2_0/petstore.yaml -l csharp-dotnet-core -o samples/client/petstore/csharp-dotnet-core/SwaggerClientTest/Lib/SwaggerClient --additional-properties hideGenerationTimestamp=true"

java $JAVA_OPTS -jar $executable $ags
10 changes: 10 additions & 0 deletions bin/windows/csharp-dotnet-core-petstore.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
set executable=.\modules\swagger-codegen-cli\target\swagger-codegen-cli.jar

If Not Exist %executable% (
mvn clean package
)

REM set JAVA_OPTS=%JAVA_OPTS% -Xmx1024M
set ags=generate -i modules/swagger-codegen/src/test/resources/2_0/petstore.yaml -l csharp-dotnet-core -o samples/client/petstore/csharp-dotnet-core/SwaggerClientTest/Lib/SwaggerClient --additional-properties hideGenerationTimestamp=true

java %JAVA_OPTS% -jar %executable% %ags%
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,7 @@ private void addMustacheLambdas(Map<String, Object> objs) {
.put("lowercase", new LowercaseLambda().generator(this))
.put("uppercase", new UppercaseLambda())
.put("titlecase", new TitlecaseLambda())
.put("pascalcase", new PascalCaseLambda())
.put("camelcase", new CamelCaseLambda().generator(this))
.put("camelcase_param", new CamelCaseLambda().generator(this).escapeAsParamName(true))
.put("indented", new IndentedLambda())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
package io.swagger.codegen.languages;

import io.swagger.codegen.*;
import org.checkerframework.common.returnsreceiver.qual.This;

import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;

public class CsharpDotnetCoreClientCodegen extends AbstractCSharpCodegen {
public static final String CLIENT_PACKAGE = "clientPackage";
public static final String USE_CSPROJ_FILE = "useCsProjFile";
public static final String DefaultargetFramwork = "net8.0";
protected String clientPackage = "IO.Swagger.Client";
protected String systemTextJsonVersion = "8.0.3";
protected String apiDocPath = "docs";
protected String modelDocPath = "docs";

protected Map<String, String> versions = new HashMap<>();
protected String exceptionTypeName;
protected String apiClientBaseTypeName;

public CsharpDotnetCoreClientCodegen() {
super();

versions.put("net8.0","8.0.3");
versions.put("net7.0","7.0.4");
versions.put("net6.0","6.0.9");
versions.put("net5.0","5.0.2");

importMapping.clear();

modelTemplateFiles.put("model.mustache", ".cs");
apiTemplateFiles.put("api.mustache", ".cs");

setApiPackage(packageName + ".Api");
setModelPackage(packageName + ".Model");
setClientPackage(packageName + ".Client");
setSourceFolder("src" + File.separator + "main" + File.separator + "CsharpDotNet2");

modelDocTemplateFiles.put("model_doc.mustache", ".md");
apiDocTemplateFiles.put("api_doc.mustache", ".md");

cliOptions.clear();
cliOptions.add(new CliOption(CodegenConstants.PACKAGE_NAME,
"C# package name (convention: Camel.Case).")
.defaultValue(packageName));
cliOptions.add(new CliOption(CodegenConstants.DOTNET_FRAMEWORK,
CodegenConstants.DOTNET_FRAMEWORK_DESC)
.defaultValue("net8.0"));
cliOptions.add(new CliOption(CodegenConstants.PACKAGE_VERSION,
"C# package version.")
.defaultValue(packageVersion));
}

@Override
public void processOpts() {
super.processOpts();

sourceFolder = "";

if (additionalProperties.containsKey(CLIENT_PACKAGE)) {
setClientPackage((String) additionalProperties.get(CLIENT_PACKAGE));
} else {
additionalProperties.put(CLIENT_PACKAGE, getClientPackage());
}

String generatorVersion = this.getClass().getPackage().getSpecificationVersion();
if(generatorVersion == null) {
generatorVersion = "1.0";
}
additionalProperties.put("generateorVersion", generatorVersion);

final String clientPackage = getClientPackage();

if(!additionalProperties.containsKey("apiDocPath")) {
additionalProperties.put("apiDocPath", apiDocPath);
}
if(!additionalProperties.containsKey("modelDocPath")) {
additionalProperties.put("modelDocPath", modelDocPath);
}

String exceptionTypeName = clientPackage.replace(".", "") + "ApiException";
additionalProperties.put("exceptionTypeName", exceptionTypeName);
String apiClientBaseTypeName = clientPackage.replace(".", "") + "ApiClientBase";
additionalProperties.put("apiClientBaseTypeName", apiClientBaseTypeName);

supportingFiles.add(new SupportingFile("ApiException.mustache", "", exceptionTypeName + ".cs"));
supportingFiles.add(new SupportingFile("ApiClient.mustache", "", "ApiClient.cs"));
supportingFiles.add(new SupportingFile("README.mustache", "", "README.md"));

if (additionalProperties.containsKey(USE_CSPROJ_FILE) && Boolean.parseBoolean(additionalProperties.get(USE_CSPROJ_FILE).toString())) {
supportingFiles.add(new SupportingFile("csproj.mustache", "", clientPackage + ".csproj"));
}
if(!additionalProperties.containsKey(CodegenConstants.DOTNET_FRAMEWORK)) {
additionalProperties.put(CodegenConstants.DOTNET_FRAMEWORK, DefaultargetFramwork);
}
String version = additionalProperties.get(CodegenConstants.DOTNET_FRAMEWORK).toString();
boolean contains = versions.containsKey(version);
if(contains) {
setSystemTextJsonVersion(versions.get(version));
}
}

@Override
public String apiPackage() {
return packageName + ".Clients";
}

@Override
public String modelPackage() {
return packageName + ".Models";
}

public void setSystemTextJsonVersion(String systemTextJsonVersion){
this.systemTextJsonVersion = systemTextJsonVersion;
}

public String getSystemTextJsonVersion(){
return this.systemTextJsonVersion;
}

public String getClientPackage() {
return clientPackage;
}

public void setClientPackage(String clientPackage) {
this.clientPackage = clientPackage;
}

@Override
protected void processOperation(CodegenOperation operation) {
CodegenParameter cancellationTokenParameter = new CodegenParameter();
cancellationTokenParameter.dataType = "CancellationToken";
cancellationTokenParameter.paramName = "ct";
cancellationTokenParameter.secondaryParam = true;
operation.hasMore = false;

if(operation.allParams.size() != 0) {
CodegenParameter lastParameter = operation.allParams.get(operation.allParams.size() - 1);
lastParameter.hasMore = true;
}

operation.allParams.add(cancellationTokenParameter);

super.processOperation(operation);
}

@Override
public CodegenType getTag() {
return CodegenType.CLIENT;
}

@Override
public String getName() {
return "csharp-dotnet-core";
}

@Override
public String getHelp() {
return "Generates a C# dotnet Core client library.";
}

@Override
public String apiFileFolder() {
return outputFolder + File.separator + "Clients";
}

@Override
public String modelFileFolder() {
return outputFolder + File.separator + "Models";
}

@Override
public String apiDocFileFolder() {
return handleAbsolutePathIfPresentFromProperties("apiDocPath");
}

@Override
public String modelDocFileFolder() {
return handleAbsolutePathIfPresentFromProperties("modelDocPath");
}

private String handleAbsolutePathIfPresentFromProperties(String propertyWithPathName){
String pathFromProperty = additionalProperties.get(propertyWithPathName).toString();
return handleAbsolutePathIfPresent(pathFromProperty);
}

private String handleAbsolutePathIfPresent(String value){
String pathFromProperties = value;
Path path = Paths.get(pathFromProperties);

if (path.isAbsolute()) {
return pathFromProperties.replace('/', File.separatorChar);
}

return (outputFolder + "/" +pathFromProperties).replace('/', File.separatorChar);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package io.swagger.codegen.mustache;

import com.samskivert.mustache.Mustache;
import com.samskivert.mustache.Template;
import io.swagger.codegen.CodegenConfig;
import io.swagger.codegen.DefaultCodegen;

import java.io.IOException;
import java.io.Writer;

/**
* Converts text in a fragment to PascalCase.
*
* Register:
* <pre>
* additionalProperties.put("pascalcase", new PascalCaseLambda());
* </pre>
*
* Use:
* <pre>
* {{#pascalcase}}{{name}}{{/pascalcase}}
* </pre>
*/
public class PascalCaseLambda implements Mustache.Lambda {
private CodegenConfig generator = null;
private Boolean escapeParam = false;

public PascalCaseLambda() {

}

public PascalCaseLambda generator(final CodegenConfig generator) {
this.generator = generator;
return this;
}

public PascalCaseLambda 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(), false);
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ io.swagger.codegen.languages.ClojureClientCodegen
io.swagger.codegen.languages.ConfluenceWikiGenerator
io.swagger.codegen.languages.CppRestClientCodegen
io.swagger.codegen.languages.CsharpDotNet2ClientCodegen
io.swagger.codegen.languages.CsharpDotnetCoreClientCodegen
io.swagger.codegen.languages.DartClientCodegen
io.swagger.codegen.languages.DartJaguarClientCodegen
io.swagger.codegen.languages.ElixirClientCodegen
Expand Down
Loading
Loading