Skip to content

Commit

Permalink
ci: updated templates
Browse files Browse the repository at this point in the history
  • Loading branch information
emajo authored Oct 30, 2024
1 parent ffbd3b8 commit 4c682e4
Show file tree
Hide file tree
Showing 12 changed files with 96 additions and 69 deletions.
28 changes: 18 additions & 10 deletions templates/openapi-generator/csharp/ApiClient.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@ using System.Text;
using System.Threading;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
{{^netStandard}}
{{^net60OrLater}}
using System.Web;
{{/netStandard}}
{{/net60OrLater}}
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using RestSharp;
using RestSharp.Serializers;
using RestSharpMethod = RestSharp.Method;
using FileIO = System.IO.File;
{{#supportsRetry}}
using Polly;
{{/supportsRetry}}
Expand Down Expand Up @@ -113,7 +114,7 @@ namespace {{packageName}}.Client
if (match.Success)
{
string fileName = filePath + ClientUtils.SanitizeFilename(match.Groups[1].Value.Replace("\"", "").Replace("'", ""));
File.WriteAllBytes(fileName, bytes);
FileIO.WriteAllBytes(fileName, bytes);
return new FileStream(fileName, FileMode.Open);
}
}
Expand Down Expand Up @@ -146,13 +147,13 @@ namespace {{packageName}}.Client
public ISerializer Serializer => this;
public IDeserializer Deserializer => this;

public string[] AcceptedContentTypes => RestSharp.ContentType.JsonAccept;
public string[] AcceptedContentTypes => ContentType.JsonAccept;

public SupportsContentType SupportsContentType => contentType =>
contentType.Value.EndsWith("json", StringComparison.InvariantCultureIgnoreCase) ||
contentType.Value.EndsWith("javascript", StringComparison.InvariantCultureIgnoreCase);

public ContentType ContentType { get; set; } = RestSharp.ContentType.Json;
public ContentType ContentType { get; set; } = ContentType.Json;

public DataFormat DataFormat => DataFormat.Json;
}
Expand Down Expand Up @@ -200,7 +201,7 @@ namespace {{packageName}}.Client
/// </summary>
public ApiClient()
{
_baseUrl = {{packageName}}.Client.GlobalConfiguration.Instance.BasePath;
_baseUrl = GlobalConfiguration.Instance.BasePath;
}

/// <summary>
Expand Down Expand Up @@ -257,14 +258,14 @@ namespace {{packageName}}.Client

/// <summary>
/// Provides all logic for constructing a new RestSharp <see cref="RestRequest"/>.
/// At this point, all information for querying the service is known. Here, it is simply
/// mapped into the RestSharp request.
/// At this point, all information for querying the service is known.
/// Here, it is simply mapped into the RestSharp request.
/// </summary>
/// <param name="method">The http verb.</param>
/// <param name="path">The target path (or resource).</param>
/// <param name="options">The additional request options.</param>
/// <param name="configuration">A per-request configuration object. It is assumed that any merge with
/// GlobalConfiguration has been done before calling this method.</param>
/// <param name="configuration">A per-request configuration object.
/// It is assumed that any merge with GlobalConfiguration has been done before calling this method.</param>
/// <returns>[private] A new RestRequest instance.</returns>
/// <exception cref="ArgumentNullException"></exception>
private RestRequest NewRequest(
Expand Down Expand Up @@ -383,6 +384,13 @@ namespace {{packageName}}.Client
return request;
}
/// <summary>
/// Transforms a RestResponse instance into a new ApiResponse instance.
/// At this point, we have a concrete http response from the service.
/// Here, it is simply mapped into the [public] ApiResponse object.
/// </summary>
/// <param name="response">The RestSharp response object</param>
/// <returns>A new ApiResponse instance.</returns>
private ApiResponse<T> ToApiResponse<T>(RestResponse<T> response)
{
T result = response.Data;
Expand Down
4 changes: 2 additions & 2 deletions templates/openapi-generator/csharp/Configuration.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ namespace {{packageName}}.Client
/// <summary>
/// Initializes a new instance of the <see cref="Configuration" /> class
/// </summary>
[System.Diagnostics.CodeAnalysis.SuppressMessage("ReSharper", "VirtualMemberCallInConstructor")]
[global::System.Diagnostics.CodeAnalysis.SuppressMessage("ReSharper", "VirtualMemberCallInConstructor")]
public Configuration()
{
Proxy = null;
Expand Down Expand Up @@ -206,7 +206,7 @@ namespace {{packageName}}.Client
/// <summary>
/// Initializes a new instance of the <see cref="Configuration" /> class
/// </summary>
[System.Diagnostics.CodeAnalysis.SuppressMessage("ReSharper", "VirtualMemberCallInConstructor")]
[global::System.Diagnostics.CodeAnalysis.SuppressMessage("ReSharper", "VirtualMemberCallInConstructor")]
public Configuration(
IDictionary<string, string> defaultHeaders,
IDictionary<string, string> apiKey,
Expand Down
4 changes: 2 additions & 2 deletions templates/openapi-generator/csharp/api.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ namespace {{packageName}}.{{apiPackage}}
{{#isDeprecated}}
[Obsolete]
{{/isDeprecated}}
{{#returnType}}System.Threading.Tasks.Task<{{{.}}}>{{/returnType}}{{^returnType}}System.Threading.Tasks.Task{{/returnType}} {{operationId}}Async({{#allParams}}{{{dataType}}} {{paramName}}{{^required}}{{#optionalMethodArgument}} = default({{{dataType}}}){{/optionalMethodArgument}}{{/required}}{{^-last}}, {{/-last}}{{/allParams}}{{#allParams.0}}, {{/allParams.0}}int operationIndex = 0, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken));
{{#returnType}}System.Threading.Tasks.Task<{{{.}}}>{{/returnType}}{{^returnType}}System.Threading.Tasks.Task{{/returnType}} {{operationId}}Async({{#allParams}}{{{dataType}}} {{paramName}}{{^required}}{{#optionalMethodArgument}} = default({{{dataType}}}){{/optionalMethodArgument}}{{/required}}{{^-last}}, {{/-last}}{{/allParams}}{{#allParams.0}}, {{/allParams.0}}int operationIndex = 0, System.Threading.CancellationToken cancellationToken = default(global::System.Threading.CancellationToken));

/// <summary>
/// {{summary}}
Expand All @@ -98,7 +98,7 @@ namespace {{packageName}}.{{apiPackage}}
{{#isDeprecated}}
[Obsolete]
{{/isDeprecated}}
System.Threading.Tasks.Task<ApiResponse<{{{returnType}}}{{^returnType}}Object{{/returnType}}>> {{operationId}}WithHttpInfoAsync({{#allParams}}{{{dataType}}} {{paramName}}{{^required}}{{#optionalMethodArgument}} = default({{{dataType}}}){{/optionalMethodArgument}}{{/required}}{{^-last}}, {{/-last}}{{/allParams}}{{#allParams.0}}, {{/allParams.0}}int operationIndex = 0, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken));
System.Threading.Tasks.Task<ApiResponse<{{{returnType}}}{{^returnType}}Object{{/returnType}}>> {{operationId}}WithHttpInfoAsync({{#allParams}}{{{dataType}}} {{paramName}}{{^required}}{{#optionalMethodArgument}} = default({{{dataType}}}){{/optionalMethodArgument}}{{/required}}{{^-last}}, {{/-last}}{{/allParams}}{{#allParams.0}}, {{/allParams.0}}int operationIndex = 0, System.Threading.CancellationToken cancellationToken = default(global::System.Threading.CancellationToken));
{{/operation}}
#endregion Asynchronous Operations
}
Expand Down
2 changes: 2 additions & 0 deletions templates/openapi-generator/csharp/modelGeneric.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,9 @@
{{^conditionalSerialization}}
[DataMember(Name = "{{baseName}}"{{#required}}, IsRequired = true{{/required}}, EmitDefaultValue = {{#vendorExtensions.x-emit-default-value}}true{{/vendorExtensions.x-emit-default-value}}{{^vendorExtensions.x-emit-default-value}}{{#required}}true{{/required}}{{^required}}{{#isBoolean}}true{{/isBoolean}}{{^isBoolean}}{{#isNullable}}true{{/isNullable}}{{^isNullable}}false{{/isNullable}}{{/isBoolean}}{{/required}}{{/vendorExtensions.x-emit-default-value}})]
{{#isDate}}
{{^supportsDateOnly}}
[JsonConverter(typeof(OpenAPIDateConverter))]
{{/supportsDateOnly}}
{{/isDate}}
{{#deprecated}}
[Obsolete]
Expand Down
16 changes: 10 additions & 6 deletions templates/openapi-generator/csharp/netcore_project.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<PackageReleaseNotes>{{.}}</PackageReleaseNotes>{{/releaseNote}}{{#packageTags}}
<PackageTags>{{{.}}}</PackageTags>{{/packageTags}}{{#nrt}}
<Nullable>{{#useGenericHost}}enable{{/useGenericHost}}{{^useGenericHost}}annotations{{/useGenericHost}}</Nullable>{{/nrt}}
<ManagePackageVersionsCentrally>false</ManagePackageVersionsCentrally>
</PropertyGroup>

<ItemGroup>
Expand All @@ -33,23 +34,26 @@
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
{{/useGenericHost}}
{{#useRestSharp}}
<PackageReference Include="RestSharp" Version="110.2.0" />
<PackageReference Include="RestSharp" Version="112.0.0" />
{{/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}}" />
{{#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.3 {{/net8.0}}{{/lambda.first}}" />
<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}}" />
{{/supportsRetry}}
{{#net80OrLater}}
<PackageReference Include="Microsoft.Net.Http.Headers" Version="8.0.3" />
<PackageReference Include="Microsoft.Net.Http.Headers" Version="8.0.8" />
{{/net80OrLater}}
{{#netStandard}}
{{^net60OrLater}}
<PackageReference Include="System.Threading.Channels" Version="8.0.0" />
{{/netStandard}}
{{#net47OrLater}}
<Reference Include="System.Web" />
{{/net47OrLater}}
{{/net60OrLater}}
{{/useGenericHost}}
{{^useGenericHost}}
{{#supportsRetry}}
{{#supportsRetry}}
<PackageReference Include="Polly" Version="{{^netStandard}}8.1.0{{/netStandard}}{{#netStandard}}8.1.0{{/netStandard}}" />
{{/supportsRetry}}
{{/useGenericHost}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="{{^netStandard}}17.9.0{{/netStandard}}{{#netStandard}}15.9.2{{/netStandard}}" />
<PackageReference Include="xunit" Version="{{^netStandard}}2.7.0{{/netStandard}}{{#netStandard}}2.4.2{{/netStandard}}" />
<PackageReference Include="xunit.runner.visualstudio" Version="{{^netStandard}}2.5.7{{/netStandard}}{{#netStandard}}2.4.5{{/netStandard}}" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="{{^netStandard}}17.11.11{{/netStandard}}{{#netStandard}}15.9.2{{/netStandard}}" />
<PackageReference Include="xunit" Version="{{^netStandard}}2.9.0{{/netStandard}}{{#netStandard}}2.4.2{{/netStandard}}" />
<PackageReference Include="xunit.runner.visualstudio" Version="{{^netStandard}}2.8.2{{/netStandard}}{{#netStandard}}2.4.5{{/netStandard}}" />
<PackageReference Include="Moq" Version="4.16.1" />
</ItemGroup>

Expand Down
34 changes: 28 additions & 6 deletions templates/openapi-generator/java/ApiClient.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -808,6 +808,30 @@ public class ApiClient {
}
{{/dynamicOperations}}
/**
* Formats the specified free-form query parameters to a list of {@code Pair} objects.
*
* @param value The free-form query parameters.
* @return A list of {@code Pair} objects.
*/
public List<Pair> freeFormParameterToPairs(Object value) {
List<Pair> params = new ArrayList<>();
// preconditions
if (value == null || !(value instanceof Map )) {
return params;
}
final Map<String, Object> valuesMap = (Map<String, Object>) value;
for (Map.Entry<String, Object> entry : valuesMap.entrySet()) {
params.add(new Pair(entry.getKey(), parameterToString(entry.getValue())));
}
return params;
}
/**
* Formats the specified collection path parameter to a string value.
*
Expand Down Expand Up @@ -1271,10 +1295,6 @@ public class ApiClient {
* @throws {{invokerPackage}}.ApiException If fail to serialize the request body object
*/
public Request buildRequest(String baseUrl, String path, String method, List<Pair> queryParams, List<Pair> collectionQueryParams, Object body, Map<String, String> headerParams, Map<String, String> cookieParams, Map<String, Object> formParams, String[] authNames, ApiCallback callback) throws ApiException {
// aggregate queryParams (non-collection) and collectionQueryParams into allQueryParams
List<Pair> allQueryParams = new ArrayList<Pair>(queryParams);
allQueryParams.addAll(collectionQueryParams);
final String url = buildUrl(baseUrl, path, queryParams, collectionQueryParams);
// prepare HTTP request body
Expand Down Expand Up @@ -1302,10 +1322,12 @@ public class ApiClient {
reqBody = serialize(body, contentType);
}

List<Pair> updatedQueryParams = new ArrayList<>(queryParams);

// update parameters with authentication settings
updateParamsForAuth(authNames, allQueryParams, headerParams, cookieParams, requestBodyToString(reqBody), method, URI.create(url));
updateParamsForAuth(authNames, updatedQueryParams, headerParams, cookieParams, requestBodyToString(reqBody), method, URI.create(url));

final Request.Builder reqBuilder = new Request.Builder().url(url);
final Request.Builder reqBuilder = new Request.Builder().url(buildUrl(baseUrl, path, updatedQueryParams, collectionQueryParams));
processHeaderParams(headerParams, reqBuilder);
processCookieParams(cookieParams, reqBuilder);

Expand Down
19 changes: 6 additions & 13 deletions templates/openapi-generator/java/JSON.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

package {{invokerPackage}};

import com.fasterxml.jackson.databind.util.StdDateFormat;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonParseException;
import com.google.gson.TypeAdapter;
import com.google.gson.internal.bind.util.ISO8601Utils;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import com.google.gson.JsonElement;
Expand All @@ -27,16 +27,14 @@ import java.io.StringReader;
import java.lang.reflect.Type;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.ParsePosition;
import java.time.LocalDate;
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.Locale;
import java.util.Map;
import java.util.HashMap;
import java.util.TimeZone;

/*
* A JSON utility class
Expand All @@ -59,11 +57,6 @@ public class JSON {
{{/jsr310}}
private static ByteArrayAdapter byteArrayAdapter = new ByteArrayAdapter();

private static final StdDateFormat sdf = new StdDateFormat()
.withTimeZone(TimeZone.getTimeZone(ZoneId.systemDefault()))
.withColonInTimeZone(true);
private static final DateTimeFormatter dtf = DateTimeFormatter.ISO_OFFSET_DATE_TIME;

@SuppressWarnings("unchecked")
public static GsonBuilder createGson() {
GsonFireBuilder fireBuilder = new GsonFireBuilder()
Expand Down Expand Up @@ -457,7 +450,7 @@ public class JSON {
if (dateFormat != null) {
return new java.sql.Date(dateFormat.parse(date).getTime());
}
return new java.sql.Date(sdf.parse(date).getTime());
return new java.sql.Date(ISO8601Utils.parse(date, new ParsePosition(0)).getTime());
} catch (ParseException e) {
throw new JsonParseException(e);
}
Expand All @@ -467,7 +460,7 @@ public class JSON {

/**
* Gson TypeAdapter for java.util.Date type
* If the dateFormat is null, DateTimeFormatter will be used.
* If the dateFormat is null, ISO8601Utils will be used.
*/
public static class DateTypeAdapter extends TypeAdapter<Date> {
Expand All @@ -492,7 +485,7 @@ public class JSON {
if (dateFormat != null) {
value = dateFormat.format(date);
} else {
value = date.toInstant().atOffset(ZoneOffset.UTC).format(dtf);
value = ISO8601Utils.format(date, true);
}
out.value(value);
}
Expand All @@ -511,7 +504,7 @@ public class JSON {
if (dateFormat != null) {
return dateFormat.parse(date);
}
return sdf.parse(date);
return ISO8601Utils.parse(date, new ParsePosition(0));
} catch (ParseException e) {
throw new JsonParseException(e);
}
Expand Down
6 changes: 3 additions & 3 deletions templates/openapi-generator/java/api.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ import io.swagger.v3.oas.models.parameters.Parameter;
import java.io.IOException;

{{#useBeanValidation}}
import jakarta.validation.constraints.*;
import jakarta.validation.Valid;
import {{javaxPackage}}.validation.constraints.*;
import {{javaxPackage}}.validation.Valid;
{{/useBeanValidation}}
{{#performBeanValidation}}
import {{javaxPackage}}.validation.ConstraintViolation;
Expand Down Expand Up @@ -169,7 +169,7 @@ public class {{classname}} {
{{^dynamicOperations}}
{{#queryParams}}
if ({{paramName}} != null) {
{{#collectionFormat}}localVarCollectionQueryParams.addAll(localVarApiClient.parameterToPairs("{{{.}}}", {{/collectionFormat}}{{^collectionFormat}}localVarQueryParams.addAll(localVarApiClient.parameterToPair({{/collectionFormat}}"{{baseName}}", {{paramName}}));
{{#isFreeFormObject}}localVarQueryParams.addAll(localVarApiClient.freeFormParameterToPairs({{paramName}}));{{/isFreeFormObject}}{{^isFreeFormObject}}{{#collectionFormat}}localVarCollectionQueryParams.addAll(localVarApiClient.parameterToPairs("{{{.}}}", {{/collectionFormat}}{{^collectionFormat}}localVarQueryParams.addAll(localVarApiClient.parameterToPair({{/collectionFormat}}"{{baseName}}", {{paramName}}));{{/isFreeFormObject}}
}

{{/queryParams}}
Expand Down
2 changes: 1 addition & 1 deletion templates/openapi-generator/java/build.gradle.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ dependencies {
implementation 'javax.ws.rs:javax.ws.rs-api:2.1.1'
implementation "io.vavr:vavr:0.10.4"
testImplementation(platform('org.junit:junit-bom:5.10.2'))
testImplementation('org.junit.jupiter:junit-jupiter:5.10.2')
testImplementation('org.junit.jupiter:junit-jupiter:5.10.3')
testImplementation 'org.mockito:mockito-core:4.5.1'
testImplementation 'com.squareup.okhttp3:mockwebserver:4.9.3'
}
Expand Down
Loading

0 comments on commit 4c682e4

Please sign in to comment.