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

[C#][netcore] Add multiple frameworks support #9196

Merged
merged 3 commits into from
Apr 9, 2021
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
4 changes: 4 additions & 0 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ build_script:
- dotnet build samples\server\petstore\aspnetcore-3.0\Org.OpenAPITools.sln
# build C# aspnetcore 2.2 server
- dotnet build samples\server\petstore\aspnetcore\Org.OpenAPITools.sln
# build C# API client (multiple frameworks)
- dotnet build samples\client\petstore\csharp-netcore\OpenAPIClientCoreAndNet47\Org.OpenAPITools.sln
# build C# API client (httpclient)
- dotnet build samples\client\petstore\csharp-netcore\OpenAPIClient-httpclient\Org.OpenAPITools.sln
# build C# API client (netcore)
Expand All @@ -66,6 +68,8 @@ build_script:
# run the locally installed openapi-generator-gradle-plugin
- gradle -b modules\openapi-generator-gradle-plugin\samples\local-spec\build.gradle buildGoSdk --stacktrace
test_script:
# test c# API client (multiple frameworks)
- dotnet test samples\client\petstore\csharp-netcore\OpenAPIClientCoreAndNet47\src\Org.OpenAPITools.Test\Org.OpenAPITools.Test.csproj
# test c# API client (httpclient)
- dotnet test samples\client\petstore\csharp-netcore\OpenAPIClient-httpclient\src\Org.OpenAPITools.Test\Org.OpenAPITools.Test.csproj
# test c# API client (netcore)
Expand Down
8 changes: 8 additions & 0 deletions bin/configs/csharp-netcore-OpenAPIClientCoreAndNet47.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
generatorName: csharp-netcore
outputDir: samples/client/petstore/csharp-netcore/OpenAPIClientCoreAndNet47
inputSpec: modules/openapi-generator/src/test/resources/3_0/petstore.yaml
templateDir: modules/openapi-generator/src/main/resources/csharp-netcore
additionalProperties:
packageGuid: '{321C8C3F-0156-40C1-AE42-D59761FB9B6C}'
targetFramework: netstandard2.1;netcoreapp3.0
useCompareNetObjects: "true"
Original file line number Diff line number Diff line change
Expand Up @@ -590,38 +590,51 @@ public void processOpts() {
throw new RuntimeException("Invalid HTTP library " + getLibrary() + ". Only restsharp, httpclient are supported.");
}

String framework = (String) additionalProperties.getOrDefault(CodegenConstants.DOTNET_FRAMEWORK, defaultFramework.name);
boolean strategyMatched = false;
FrameworkStrategy strategy = defaultFramework;
for (FrameworkStrategy frameworkStrategy : frameworkStrategies) {
if (framework.equals(frameworkStrategy.name)) {
strategy = frameworkStrategy;
strategyMatched = true;
}
String inputFramework = (String) additionalProperties.getOrDefault(CodegenConstants.DOTNET_FRAMEWORK, defaultFramework.name);
String[] frameworks;
List<FrameworkStrategy> strategies = new ArrayList<>();

if (inputFramework.contains(";")) {
// multiple target framework
frameworks = inputFramework.split(";");
additionalProperties.put("multiTarget", true);
} else {
// just a single value
frameworks = new String [] {inputFramework};
}

// throws exception if the input targetFramework is invalid
if (strategyMatched == false) {
throw new IllegalArgumentException("Invalid .NET framework version: " +
framework + ". List of supported versions: " +
frameworkStrategies.stream()
.map(p -> p.name)
.collect(Collectors.joining(", ")));
}
for (String framework : frameworks) {
boolean strategyMatched = false;
for (FrameworkStrategy frameworkStrategy : frameworkStrategies) {
if (framework.equals(frameworkStrategy.name)) {
strategies.add(frameworkStrategy);
strategyMatched = true;
}

strategy.configureAdditionalProperties(additionalProperties);
setTargetFrameworkNuget(strategy.getNugetFrameworkIdentifier());
setTargetFramework(strategy.name);
setTestTargetFramework(strategy.testTargetFramework);
if (frameworkStrategy != FrameworkStrategy.NETSTANDARD_2_0 && "restsharp".equals(getLibrary())) {
LOGGER.warn("If using built-in templates, RestSharp only supports netstandard 2.0 or later.");
}
}

if (strategy != FrameworkStrategy.NETSTANDARD_2_0) {
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: " +
frameworkStrategies.stream()
.map(p -> p.name)
.collect(Collectors.joining(", ")));
}
}

configureAdditionalPropertiesForFrameworks(additionalProperties, strategies);
setTargetFrameworkNuget(strategies);
setTargetFramework(strategies);
setTestTargetFramework(strategies);

setSupportsAsync(Boolean.TRUE);
setNetStandard(strategy.isNetStandard);
setNetStandard(strategies.stream().anyMatch(p -> Boolean.TRUE.equals(p.isNetStandard)));

if (!strategy.isNetStandard) {
if (!netStandard) {
setNetCoreProjectFileFlag(true);
}

Expand Down Expand Up @@ -774,14 +787,39 @@ public void setTargetFramework(String dotnetFramework) {
LOGGER.info("Generating code for .NET Framework " + this.targetFramework);
}

public void setTargetFramework(List<FrameworkStrategy> strategies) {
for (FrameworkStrategy strategy : strategies) {
if (!frameworks.containsKey(strategy.name)) {
throw new IllegalArgumentException("Invalid .NET framework version: " +
strategy.name + ". List of supported versions: " +
frameworkStrategies.stream()
.map(p -> p.name)
.collect(Collectors.joining(", ")));
}
}
this.targetFramework = strategies.stream().map(p -> p.name)
.collect(Collectors.joining(";"));
LOGGER.info("Generating code for .NET Framework " + this.targetFramework);
}

public void setTestTargetFramework(String testTargetFramework) {
this.testTargetFramework = testTargetFramework;
}

public void setTestTargetFramework(List<FrameworkStrategy> strategies) {
this.testTargetFramework = strategies.stream().map(p -> p.testTargetFramework)
.collect(Collectors.joining(";"));
}

public void setTargetFrameworkNuget(String targetFrameworkNuget) {
this.targetFrameworkNuget = targetFrameworkNuget;
}

public void setTargetFrameworkNuget(List<FrameworkStrategy> strategies) {
this.targetFrameworkNuget = strategies.stream().map(p -> p.getNugetFrameworkIdentifier())
.collect(Collectors.joining(";"));
}

public void setValidatable(boolean validatable) {
this.validatable = validatable;
}
Expand Down Expand Up @@ -1003,6 +1041,23 @@ protected String getTargetFrameworkVersion() {
}
}

protected void configureAdditionalPropertiesForFrameworks(final Map<String, Object> properties, List<FrameworkStrategy> strategies) {
properties.putIfAbsent(CodegenConstants.DOTNET_FRAMEWORK, strategies.stream()
.map(p -> p.name)
.collect(Collectors.joining(";")));

// not intended to be user-settable
properties.put(TARGET_FRAMEWORK_IDENTIFIER, strategies.stream()
.map(p -> p.getTargetFrameworkIdentifier())
.collect(Collectors.joining(";")));
properties.put(TARGET_FRAMEWORK_VERSION, strategies.stream()
.map(p -> p.getTargetFrameworkVersion())
.collect(Collectors.joining(";")));
properties.putIfAbsent(MCS_NET_VERSION_KEY, "4.6-api");

properties.put(NET_STANDARD, strategies.stream().anyMatch(p -> Boolean.TRUE.equals(p.isNetStandard)));
}

/**
* Return the instantiation type of the property, especially for map and array
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>{{testPackageName}}</RootNamespace>
<AssemblyName>{{testPackageName}}</AssemblyName>
<TargetFramework>{{testTargetFramework}}</TargetFramework>
<TargetFramework{{#multiTarget}}s{{/multiTarget}}>{{testTargetFramework}}</TargetFramework{{#multiTarget}}s{{/multiTarget}}>
<IsPackable>false</IsPackable>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<TargetFramework>{{targetFramework}}</TargetFramework>
<TargetFramework{{#multiTarget}}s{{/multiTarget}}>{{targetFramework}}</TargetFramework{{#multiTarget}}s{{/multiTarget}}>
<AssemblyName>{{packageName}}</AssemblyName>
<PackageId>{{packageName}}</PackageId>
<OutputType>Library</OutputType>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<AssemblyName>{{testPackageName}}</AssemblyName>
<RootNamespace>{{testPackageName}}</RootNamespace>
<TargetFramework>{{testTargetFramework}}</TargetFramework>
<TargetFramework{{#multiTarget}}s{{/multiTarget}}>{{testTargetFramework}}</TargetFramework{{#multiTarget}}s{{/multiTarget}}>
<IsPackable>false</IsPackable>
</PropertyGroup>

Expand Down
Loading