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

Edit to allow core and framework targets on the same build #20233

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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 @@ -4,6 +4,6 @@ inputSpec: modules/openapi-generator/src/test/resources/3_0/petstore.yaml
templateDir: modules/openapi-generator/src/main/resources/csharp
additionalProperties:
packageGuid: '{321C8C3F-0156-40C1-AE42-D59761FB9B6C}'
targetFramework: netstandard2.1;net47
targetFramework: netstandard2.1;net47;net8.0
useCompareNetObjects: "true"
equatable: true

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ using System.Text;
using System.Threading;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
{{^net60OrLater}}
#if !NET6_0_OR_GREATER
using System.Web;
{{/net60OrLater}}
#endif
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using RestSharp;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ using System.Text;
using System.Threading;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
{{^net60OrLater}}
#if !NET6_0_OR_GREATER
using System.Web;
{{/net60OrLater}}
#endif
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using RestSharp;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,14 @@ namespace {{packageName}}.Client
// https://msdn.microsoft.com/en-us/library/az4se3k1(v=vs.110).aspx#Anchor_8
// For example: 2009-06-15T13:45:30.0000000
return dateTimeOffset.ToString((configuration ?? GlobalConfiguration.Instance).DateTimeFormat);
{{#net60OrLater}}
#if NET6_0_OR_GREATER
if (obj is DateOnly dateOnly)
// Return a formatted date string - Can be customized with Configuration.DateTimeFormat
// Defaults to an ISO 8601, using the known as a Round-trip date/time pattern ("o")
// https://msdn.microsoft.com/en-us/library/az4se3k1(v=vs.110).aspx#Anchor_8
// For example: 2009-06-15
return dateOnly.ToString((configuration ?? GlobalConfiguration.Instance).DateTimeFormat);
{{/net60OrLater}}
#endif
if (obj is bool boolean)
return boolean ? "true" : "false";
if (obj is ICollection collection) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ namespace {{packageName}}.Client
/// <returns>ECDSA signature</returns>
private string GetECDSASignature(byte[] dataToSign)
{
{{#net60OrLater}}
#if NET6_0_OR_GREATER
if (!File.Exists(KeyFilePath) && string.IsNullOrEmpty(KeyString))
{
throw new Exception("No API key has been provided.");
Expand Down Expand Up @@ -382,10 +382,10 @@ namespace {{packageName}}.Client
var signedString = System.Convert.ToBase64String(derBytes);

return signedString;
{{/net60OrLater}}
{{^net60OrLater}}
#endif
#if !NET6_0_OR_GREATER
throw new Exception("ECDSA signing is supported only on NETCOREAPP3_0 and above");
{{/net60OrLater}}
#endif
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,11 @@ namespace {{packageName}}.{{clientPackage}}
/// </summary>
/// <param name="result"></param>
/// <returns></returns>
bool Try{{.}}({{#net60OrLater}}[NotNullWhen(true)]{{/net60OrLater}}out TType{{nrt?}} result);
bool Try{{.}}(
#if NET6_0_OR_GREATER
[NotNullWhen(true)]
#endif
out TType{{nrt?}} result);
}
{{/x-http-statuses-with-return}}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
// This logic may be modified with the AsModel.mustache template
return Is{{vendorExtensions.x-http-status}}
? System.Text.Json.JsonSerializer.Deserialize<{{#isModel}}{{^containerType}}{{packageName}}.{{modelPackage}}.{{/containerType}}{{/isModel}}{{{dataType}}}>(RawContent, _jsonSerializerOptions)
: {{#net60OrLater}}null{{/net60OrLater}}{{^net60OrLater}}default{{/net60OrLater}};
:
#if NET6_0_OR_GREATER
null;
#else
default;
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,15 @@ using System.Runtime.CompilerServices;
/// <param name="options"></param>
/// <param name="result"></param>
/// <returns></returns>
public static bool TryDeserialize<T>(string json, JsonSerializerOptions options, {{#net60OrLater}}[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] {{/net60OrLater}}out T{{#nrt}}{{#net60OrLater}}?{{/net60OrLater}}{{/nrt}} result)
public static bool TryDeserialize<T>(string json, JsonSerializerOptions options,
#if NET6_0_OR_GREATER
[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)]
#endif
out T{{#nrt}}
#if NET6_0_OR_GREATER
?
#endif
{{/nrt}} result)
{
try
{
Expand All @@ -129,7 +137,15 @@ using System.Runtime.CompilerServices;
/// <param name="options"></param>
/// <param name="result"></param>
/// <returns></returns>
public static bool TryDeserialize<T>(ref Utf8JsonReader reader, JsonSerializerOptions options, {{#net60OrLater}}[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] {{/net60OrLater}}out T{{#nrt}}{{#net60OrLater}}?{{/net60OrLater}}{{/nrt}} result)
public static bool TryDeserialize<T>(ref Utf8JsonReader reader, JsonSerializerOptions options,
#if NET6_0_OR_GREATER
[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)]
#endif
out T{{#nrt}}
#if NET6_0_OR_GREATER
?
#endif
{{/nrt}} result)
{
try
{
Expand Down Expand Up @@ -176,10 +192,10 @@ using System.Runtime.CompilerServices;
// https://msdn.microsoft.com/en-us/library/az4se3k1(v=vs.110).aspx#Anchor_8
// For example: 2009-06-15T13:45:30.0000000
return dateTimeOffset.ToString(format);
{{#net60OrLater}}
#if NET6_0_OR_GREATER
if (obj is DateOnly dateOnly)
return dateOnly.ToString(format);
{{/net60OrLater}}
#endif
if (obj is bool boolean)
return boolean
? "true"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ namespace {{packageName}}.{{clientPackage}}
{{/isEnum}}
{{/model}}
{{/models}}
JsonSerializerOptionsProvider jsonSerializerOptionsProvider = new{{^net60OrLater}} JsonSerializerOptionsProvider{{/net60OrLater}}(_jsonOptions);
JsonSerializerOptionsProvider jsonSerializerOptionsProvider = new JsonSerializerOptionsProvider(_jsonOptions);
_services.AddSingleton(jsonSerializerOptionsProvider);
{{#useSourceGeneration}}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ namespace {{packageName}}.{{clientPackage}}
/// <returns></returns>
private string GetECDSASignature(byte[] dataToSign)
{
{{#net60OrLater}}
#if NET6_0_OR_GREATER
if (!File.Exists(KeyFilePath))
throw new Exception("key file path does not exist.");

Expand Down Expand Up @@ -310,10 +310,10 @@ namespace {{packageName}}.{{clientPackage}}
var signedString = System.Convert.ToBase64String(derBytes);

return signedString;
{{/net60OrLater}}
{{^net60OrLater}}
#endif
#if !NET6_0_OR_GREATER
throw new Exception("ECDSA signing is supported only on NETCOREAPP3_0 and above");
{{/net60OrLater}}
#endif
}

private byte[] ConvertToECDSAANS1Format(byte[] signedBytes)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,11 @@ namespace {{packageName}}.{{apiPackage}}
tokenBaseLocalVars.Add(httpSignatureTokenLocalVar{{-index}});

if (httpRequestMessageLocalVar.Content != null) {
string requestBodyLocalVar = await httpRequestMessageLocalVar.Content.ReadAsStringAsync({{#net60OrLater}}cancellationToken{{/net60OrLater}}).ConfigureAwait(false);
string requestBodyLocalVar = await httpRequestMessageLocalVar.Content.ReadAsStringAsync(
#if NET6_0_OR_GREATER
cancellationToken
#endif
).ConfigureAwait(false);

httpSignatureTokenLocalVar{{-index}}.UseInHeader(httpRequestMessageLocalVar, requestBodyLocalVar, cancellationToken);
}
Expand Down Expand Up @@ -607,23 +611,25 @@ namespace {{packageName}}.{{apiPackage}}
httpRequestMessageLocalVar.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue(acceptLocalVar));
{{/-first}}
{{/produces}}
{{#net60OrLater}}

#if NET6_0_OR_GREATER
httpRequestMessageLocalVar.Method = HttpMethod.{{#lambda.titlecase}}{{#lambda.lowercase}}{{httpMethod}}{{/lambda.lowercase}}{{/lambda.titlecase}};
{{/net60OrLater}}
{{^net60OrLater}}
#else
httpRequestMessageLocalVar.Method = new HttpMethod("{{#lambda.uppercase}}{{httpMethod}}{{/lambda.uppercase}}");
{{/net60OrLater}}
#endif

DateTime requestedAtLocalVar = DateTime.UtcNow;

using (HttpResponseMessage httpResponseMessageLocalVar = await HttpClient.SendAsync(httpRequestMessageLocalVar, cancellationToken).ConfigureAwait(false))
{
string responseContentLocalVar = await httpResponseMessageLocalVar.Content.ReadAsStringAsync({{#net60OrLater}}cancellationToken{{/net60OrLater}}).ConfigureAwait(false);
string responseContentLocalVar = await httpResponseMessageLocalVar.Content.ReadAsStringAsync(
#if NET6_0_OR_GREATER
cancellationToken
#endif
).ConfigureAwait(false);

ILogger<{{#vendorExtensions.x-duplicates}}{{.}}.{{/vendorExtensions.x-duplicates}}{{operationId}}ApiResponse> apiResponseLoggerLocalVar = LoggerFactory.CreateLogger<{{#vendorExtensions.x-duplicates}}{{.}}.{{/vendorExtensions.x-duplicates}}{{operationId}}ApiResponse>();

{{#vendorExtensions.x-duplicates}}{{.}}.{{/vendorExtensions.x-duplicates}}{{operationId}}ApiResponse apiResponseLocalVar = new{{^net60OrLater}} {{operationId}}ApiResponse{{/net60OrLater}}(apiResponseLoggerLocalVar, httpRequestMessageLocalVar, httpResponseMessageLocalVar, responseContentLocalVar, "{{{path}}}", requestedAtLocalVar, _jsonSerializerOptions);
{{#vendorExtensions.x-duplicates}}{{.}}.{{/vendorExtensions.x-duplicates}}{{operationId}}ApiResponse apiResponseLocalVar = new {{#vendorExtensions.x-duplicates}}{{.}}.{{/vendorExtensions.x-duplicates}}{{operationId}}ApiResponse(apiResponseLoggerLocalVar, httpRequestMessageLocalVar, httpResponseMessageLocalVar, responseContentLocalVar, "{{{path}}}", requestedAtLocalVar, _jsonSerializerOptions);

After{{operationId}}DefaultImplementation({{#lambda.joinWithComma}}apiResponseLocalVar {{#allParams}}{{paramName}} {{/allParams}}{{/lambda.joinWithComma}});

Expand Down Expand Up @@ -761,7 +767,11 @@ namespace {{packageName}}.{{apiPackage}}
/// </summary>
/// <param name="result"></param>
/// <returns></returns>
public bool Try{{vendorExtensions.x-http-status}}({{#net60OrLater}}[NotNullWhen(true)]{{/net60OrLater}}out {{#isModel}}{{^containerType}}{{packageName}}.{{modelPackage}}.{{/containerType}}{{/isModel}}{{{dataType}}}{{#nrt}}?{{/nrt}}{{^nrt}}{{#vendorExtensions.x-is-value-type}}?{{/vendorExtensions.x-is-value-type}}{{/nrt}} result)
public bool Try{{vendorExtensions.x-http-status}}(
#if NET6_0_OR_GREATER
[NotNullWhen(true)]
#endif
out {{#isModel}}{{^containerType}}{{packageName}}.{{modelPackage}}.{{/containerType}}{{/isModel}}{{{dataType}}}{{#nrt}}?{{/nrt}}{{^nrt}}{{#vendorExtensions.x-is-value-type}}?{{/vendorExtensions.x-is-value-type}}{{/nrt}} result)
{
result = null;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>{{#useGenericHost}}
<GenerateAssemblyInfo>true</GenerateAssemblyInfo> <!-- setting GenerateAssemblyInfo to false causes this bug https://github.com/dotnet/project-system/issues/3934 -->{{/useGenericHost}}{{^useGenericHost}}
<GenerateAssemblyInfo>false</GenerateAssemblyInfo><!-- setting GenerateAssemblyInfo to false causes this bug https://github.com/dotnet/project-system/issues/3934 -->{{/useGenericHost}}
Expand All @@ -24,6 +23,16 @@
<ManagePackageVersionsCentrally>false</ManagePackageVersionsCentrally>
</PropertyGroup>

<Choose>
<When Condition="$([MSBuild]::GetTargetFrameworkIdentifier('$(TargetFramework)')) == '.NETCoreApp'">
<PropertyGroup><IsUnifiedNETVersion>true</IsUnifiedNETVersion></PropertyGroup>
</When>
<When Condition="$([MSBuild]::GetTargetFrameworkIdentifier('$(TargetFramework)')) == '.NETFramework'">
<PropertyGroup><IsNetFramework>true</IsNetFramework></PropertyGroup>
</When>
<When Condition="$([MSBuild]::GetTargetFrameworkIdentifier('$(TargetFramework)')) == '.NETStandard'"></When>
</Choose>

<ItemGroup>
{{#useCompareNetObjects}}
<PackageReference Include="CompareNETObjects" Version="4.83.0" />
Expand All @@ -33,52 +42,42 @@
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
{{/useGenericHost}}
{{#useRestSharp}}
<PackageReference Include="RestSharp" Version="112.0.0" />
<PackageReference Include="RestSharp" Version="112.1.0" />
<PackageReference Include="System.Text.Json" Version="8.0.5" />
{{/useRestSharp}}
{{#useGenericHost}}
<PackageReference Include="Microsoft.Extensions.Http" Version="{{#lambda.first}}{{#netStandard}}5.0.0 {{/netStandard}}{{#net47}}7.0.0 {{/net47}}{{#net48}}7.0.0 {{/net48}}{{#net6.0}}6.0.0 {{/net6.0}}{{#net7.0}}7.0.0 {{/net7.0}}{{#net8.0}}8.0.0 {{/net8.0}}{{/lambda.first}}" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="{{#lambda.first}}{{#netStandard}}5.0.0 {{/netStandard}}{{#net47}}7.0.0 {{/net47}}{{#net48}}7.0.0 {{/net48}}{{#net6.0}}6.0.1 {{/net6.0}}{{#net7.0}}7.0.1 {{/net7.0}}{{#net8.0}}8.0.0 {{/net8.0}}{{/lambda.first}}" />
<PackageReference Include="Microsoft.Extensions.Http" Version="{{#lambda.first}}{{#net8.0}}8.0.1 {{/net8.0}}{{#net48}}7.0.0 {{/net48}}{{#net47}}7.0.0 {{/net47}}{{#net7.0}}7.0.0 {{/net7.0}}{{#net6.0}}6.0.1 {{/net6.0}}{{#netStandard}}5.0.0 {{/netStandard}}{{/lambda.first}}" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="{{#lambda.first}}{{#net8.0}}8.0.1 {{/net8.0}}{{#net48}}7.0.1 {{/net48}}{{#net47}}7.0.1 {{/net47}}{{#net7.0}}7.0.1 {{/net7.0}}{{#net6.0}}6.0.1 {{/net6.0}}{{#netStandard}}5.0.0 {{/netStandard}}{{/lambda.first}}" />
<PackageReference Include="System.Text.Json" Version="8.0.5" />
{{#supportsRetry}}
<PackageReference Include="Microsoft.Extensions.Http.Polly" Version="{{#lambda.first}}{{#netStandard}}5.0.1 {{/netStandard}}{{#net47}}7.0.0 {{/net47}}{{#net48}}7.0.0 {{/net48}}{{#net6.0}}6.0.19 {{/net6.0}}{{#net7.0}}7.0.11 {{/net7.0}}{{#net8.0}}8.0.8 {{/net8.0}}{{/lambda.first}}" />
<PackageReference Include="Microsoft.Extensions.Http.Polly" Version="{{#lambda.first}}{{#net8.0}}8.0.11 {{/net8.0}}{{#net48}}7.0.20 {{/net48}}{{#net47}}7.0.20 {{/net47}}{{#net7.0}}7.0.20 {{/net7.0}}{{#net6.0}}6.0.36 {{/net6.0}}{{#netStandard}}5.0.1 {{/netStandard}}{{/lambda.first}}" />
{{/supportsRetry}}
{{#net80OrLater}}
<PackageReference Include="Microsoft.Net.Http.Headers" Version="8.0.8" />
{{/net80OrLater}}
{{^net60OrLater}}
<PackageReference Include="System.Threading.Channels" Version="8.0.0" />
{{#net47OrLater}}
<Reference Include="System.Web" />
{{/net47OrLater}}
{{/net60OrLater}}
{{/useGenericHost}}
{{^useGenericHost}}
{{#supportsRetry}}
<PackageReference Include="Polly" Version="{{^netStandard}}8.1.0{{/netStandard}}{{#netStandard}}8.1.0{{/netStandard}}" />
<PackageReference Include="Polly" Version="8.5.0" />
{{/supportsRetry}}
{{/useGenericHost}}
{{#validatable}}
{{^net60OrLater}}
</ItemGroup>
{{#validatable}}
<ItemGroup>
<PackageReference Include="System.ComponentModel.Annotations" Version="5.0.0" />
{{/net60OrLater}}
{{/validatable}}
</ItemGroup>
{{/validatable}}

{{^useGenericHost}}
<ItemGroup>
{{^net60OrLater}}
<ItemGroup Condition="$(IsNetFramework) == 'true'">
<None Remove="System.Web" />
{{/net60OrLater}}
{{#net48}}
<None Remove="System.Net.Http" />
{{/net48}}
<Reference Include="System.Web" />
</ItemGroup>

<ItemGroup>
{{^net60OrLater}}
<Reference Include="System.Web" />
{{/net60OrLater}}
{{#net48}}
<Reference Include="System.Net.Http" />
{{/net48}}
<None Remove="System.Net.Http" />
<PackageReference Include="System.Net.Http" Version="4.3.4" />
</ItemGroup>
{{/useGenericHost}}
{{>netcore_project.additions}}</Project>
Loading
Loading