Skip to content

Commit

Permalink
[csharp][generichost] Fix invalid key name (#19633)
Browse files Browse the repository at this point in the history
* fix invalid key name

* fix comment
  • Loading branch information
devhl-labs authored Sep 29, 2024
1 parent 0b32c50 commit fb5e69f
Show file tree
Hide file tree
Showing 93 changed files with 209 additions and 156 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,8 @@ protected ImmutableMap.Builder<String, Lambda> addMustacheLambdas() {
.put("uniqueLines", new UniqueLambda("\n", false))
.put("unique", new UniqueLambda("\n", true))
.put("camel_case", new CamelCaseLambda())
.put("escape_reserved_word", new EscapeKeywordLambda(this::escapeKeyword));
.put("escape_reserved_word", new EscapeKeywordLambda(this::escapeKeyword))
.put("alphabet_or_underscore", new ReplaceAllLambda("[^A-Za-z]", "_"));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright 2018 OpenAPI-Generator Contributors (https://openapi-generator.tech)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.openapitools.codegen.templating.mustache;

import com.samskivert.mustache.Mustache;
import com.samskivert.mustache.Template;

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

/**
* Replaces all regex captures with the provided string.
*
* Register:
* <pre>
* additionalProperties.put("regex", new ReplaceAllLambda());
* </pre>
*
* Use:
* <pre>
* {{#regex}}{{summary}}{{/regex}}
* </pre>
*/
public class ReplaceAllLambda implements Mustache.Lambda {
private String regex;
private String replacement;

public ReplaceAllLambda(String regex, String replacement) {
this.regex = regex;
this.replacement = replacement;
}

@Override
public void execute(Template.Fragment fragment, Writer writer) throws IOException {
writer.write(fragment.execute()
.replaceAll(regex, replacement));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace {{packageName}}.Test.{{apiPackage}}
{{#lambda.trimTrailingWithNewLine}}
{{#apiKeyMethods}}
string apiKeyTokenValue{{-index}} = context.Configuration["<token>"] ?? throw new Exception("Token not found.");
ApiKeyToken apiKeyToken{{-index}} = new{{^net70OrLater}} ApiKeyToken{{/net70OrLater}}(apiKeyTokenValue{{-index}}, ClientUtils.ApiKeyHeader.{{#lambda.titlecase}}{{keyParamName}}{{/lambda.titlecase}}, timeout: TimeSpan.FromSeconds(1));
ApiKeyToken apiKeyToken{{-index}} = new{{^net70OrLater}} ApiKeyToken{{/net70OrLater}}(apiKeyTokenValue{{-index}}, ClientUtils.ApiKeyHeader.{{#lambda.titlecase}}{{#lambda.alphabet_or_underscore}}{{keyParamName}}{{/lambda.alphabet_or_underscore}}{{/lambda.titlecase}}, timeout: TimeSpan.FromSeconds(1));
options.AddTokens(apiKeyToken{{-index}});

{{/apiKeyMethods}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ using System.Runtime.CompilerServices;
/// <summary>
/// The {{keyParamName}} header
/// </summary>
{{#lambda.titlecase}}{{keyParamName}}{{/lambda.titlecase}}{{^-last}},{{/-last}}
{{#lambda.titlecase}}{{#lambda.alphabet_or_underscore}}{{keyParamName}}{{/lambda.alphabet_or_underscore}}{{/lambda.titlecase}}{{^-last}},{{/-last}}
{{/apiKeyMethods}}
}

Expand All @@ -76,7 +76,7 @@ using System.Runtime.CompilerServices;
return value switch
{
{{#apiKeyMethods}}
ApiKeyHeader.{{#lambda.titlecase}}{{keyParamName}}{{/lambda.titlecase}} => "{{keyParamName}}",
ApiKeyHeader.{{#lambda.titlecase}}{{#lambda.alphabet_or_underscore}}{{keyParamName}}{{/lambda.alphabet_or_underscore}}{{/lambda.titlecase}} => "{{keyParamName}}",
{{/apiKeyMethods}}
_ => throw new System.ComponentModel.InvalidEnumArgumentException(nameof(value), (int)value, typeof(ApiKeyHeader)),
};
Expand All @@ -85,7 +85,7 @@ using System.Runtime.CompilerServices;
switch(value)
{
{{#apiKeyMethods}}
case ApiKeyHeader.{{#lambda.titlecase}}{{keyParamName}}{{/lambda.titlecase}}:
case ApiKeyHeader.{{#lambda.titlecase}}{{#lambda.alphabet_or_underscore}}{{keyParamName}}{{/lambda.alphabet_or_underscore}}{{/lambda.titlecase}}:
return "{{keyParamName}}";
{{/apiKeyMethods}}
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace {{packageName}}.Test.{{apiPackage}}
{
{{#lambda.trimTrailingWithNewLine}}
{{#apiKeyMethods}}
ApiKeyToken apiKeyToken{{-index}} = new{{^net70OrLater}} ApiKeyToken{{/net70OrLater}}("<token>", ClientUtils.ApiKeyHeader.{{#lambda.titlecase}}{{keyParamName}}{{/lambda.titlecase}}, timeout: TimeSpan.FromSeconds(1));
ApiKeyToken apiKeyToken{{-index}} = new{{^net70OrLater}} ApiKeyToken{{/net70OrLater}}("<token>", ClientUtils.ApiKeyHeader.{{#lambda.titlecase}}{{#lambda.alphabet_or_underscore}}{{keyParamName}}{{/lambda.alphabet_or_underscore}}{{/lambda.titlecase}}, timeout: TimeSpan.FromSeconds(1));
options.AddTokens(apiKeyToken{{-index}});

{{/apiKeyMethods}}
Expand Down Expand Up @@ -55,7 +55,7 @@ namespace {{packageName}}.Test.{{apiPackage}}
{
{{#lambda.trimTrailingWithNewLine}}
{{#apiKeyMethods}}
ApiKeyToken apiKeyToken{{-index}} = new{{^net70OrLater}} ApiKeyToken{{/net70OrLater}}("<token>", ClientUtils.ApiKeyHeader.{{#lambda.titlecase}}{{keyParamName}}{{/lambda.titlecase}}, timeout: TimeSpan.FromSeconds(1));
ApiKeyToken apiKeyToken{{-index}} = new{{^net70OrLater}} ApiKeyToken{{/net70OrLater}}("<token>", ClientUtils.ApiKeyHeader.{{#lambda.titlecase}}{{#lambda.alphabet_or_underscore}}{{keyParamName}}{{/lambda.alphabet_or_underscore}}{{/lambda.titlecase}}, timeout: TimeSpan.FromSeconds(1));
options.AddTokens(apiKeyToken{{-index}});

{{/apiKeyMethods}}
Expand Down Expand Up @@ -92,7 +92,7 @@ namespace {{packageName}}.Test.{{apiPackage}}
{
{{#lambda.trimTrailingWithNewLine}}
{{#apiKeyMethods}}
ApiKeyToken apiKeyToken{{-index}} = new{{^net70OrLater}} ApiKeyToken{{/net70OrLater}}("<token>", ClientUtils.ApiKeyHeader.{{#lambda.titlecase}}{{keyParamName}}{{/lambda.titlecase}}, timeout: TimeSpan.FromSeconds(1));
ApiKeyToken apiKeyToken{{-index}} = new{{^net70OrLater}} ApiKeyToken{{/net70OrLater}}("<token>", ClientUtils.ApiKeyHeader.{{#lambda.titlecase}}{{#lambda.alphabet_or_underscore}}{{keyParamName}}{{/lambda.alphabet_or_underscore}}{{/lambda.titlecase}}, timeout: TimeSpan.FromSeconds(1));
options.AddTokens(apiKeyToken{{-index}});

{{/apiKeyMethods}}
Expand Down Expand Up @@ -129,7 +129,7 @@ namespace {{packageName}}.Test.{{apiPackage}}
{
{{#lambda.trimTrailingWithNewLine}}
{{#apiKeyMethods}}
ApiKeyToken apiKeyToken{{-index}} = new{{^net70OrLater}} ApiKeyToken{{/net70OrLater}}("<token>", ClientUtils.ApiKeyHeader.{{#lambda.titlecase}}{{keyParamName}}{{/lambda.titlecase}}, timeout: TimeSpan.FromSeconds(1));
ApiKeyToken apiKeyToken{{-index}} = new{{^net70OrLater}} ApiKeyToken{{/net70OrLater}}("<token>", ClientUtils.ApiKeyHeader.{{#lambda.titlecase}}{{#lambda.alphabet_or_underscore}}{{keyParamName}}{{/lambda.alphabet_or_underscore}}{{/lambda.titlecase}}, timeout: TimeSpan.FromSeconds(1));
options.AddTokens(apiKeyToken{{-index}});

{{/apiKeyMethods}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1379,7 +1379,7 @@ components:
'read:pets': read your pets
api_key:
type: apiKey
name: api_key
name: api-key
in: header
api_key_query:
type: apiKey
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3045,7 +3045,7 @@ components:
type: oauth2
api_key:
in: header
name: api_key
name: api-key
type: apiKey
api_key_query:
in: query
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -409,9 +409,9 @@ namespace Example
Configuration config = new Configuration();
config.BasePath = "http://petstore.swagger.io:80/v2";
// Configure API key authorization: api_key
config.AddApiKey("api_key", "YOUR_API_KEY");
config.AddApiKey("api-key", "YOUR_API_KEY");
// Uncomment below to setup prefix (e.g. Bearer) for API key, if needed
// config.AddApiKeyPrefix("api_key", "Bearer");
// config.AddApiKeyPrefix("api-key", "Bearer");
// Configure API key authorization: api_key_query
config.AddApiKey("api_key_query", "YOUR_API_KEY");
// Uncomment below to setup prefix (e.g. Bearer) for API key, if needed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,9 @@ namespace Example
Configuration config = new Configuration();
config.BasePath = "http://petstore.swagger.io:80/v2";
// Configure API key authorization: api_key
config.AddApiKey("api_key", "YOUR_API_KEY");
config.AddApiKey("api-key", "YOUR_API_KEY");
// Uncomment below to setup prefix (e.g. Bearer) for API key, if needed
// config.AddApiKeyPrefix("api_key", "Bearer");
// config.AddApiKeyPrefix("api-key", "Bearer");
var apiInstance = new StoreApi(config);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1699,7 +1699,7 @@ public async Task<IGetPetByIdApiResponse> GetPetByIdAsync(long petId, System.Thr
System.Collections.Specialized.NameValueCollection parseQueryStringLocalVar = System.Web.HttpUtility.ParseQueryString(string.Empty);

List<TokenBase> tokenBaseLocalVars = new List<TokenBase>();
ApiKeyToken apiKeyTokenLocalVar1 = (ApiKeyToken) await ApiKeyProvider.GetAsync("api_key", cancellationToken).ConfigureAwait(false);
ApiKeyToken apiKeyTokenLocalVar1 = (ApiKeyToken) await ApiKeyProvider.GetAsync("api-key", cancellationToken).ConfigureAwait(false);
tokenBaseLocalVars.Add(apiKeyTokenLocalVar1);
apiKeyTokenLocalVar1.UseInHeader(httpRequestMessageLocalVar);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -618,7 +618,7 @@ public async Task<IGetInventoryApiResponse> GetInventoryAsync(System.Threading.C
uriBuilderLocalVar.Path = ClientUtils.CONTEXT_PATH + "/store/inventory";

List<TokenBase> tokenBaseLocalVars = new List<TokenBase>();
ApiKeyToken apiKeyTokenLocalVar1 = (ApiKeyToken) await ApiKeyProvider.GetAsync("api_key", cancellationToken).ConfigureAwait(false);
ApiKeyToken apiKeyTokenLocalVar1 = (ApiKeyToken) await ApiKeyProvider.GetAsync("api-key", cancellationToken).ConfigureAwait(false);
tokenBaseLocalVars.Add(apiKeyTokenLocalVar1);
apiKeyTokenLocalVar1.UseInHeader(httpRequestMessageLocalVar);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ static ClientUtils()
public enum ApiKeyHeader
{
/// <summary>
/// The api_key header
/// The api-key header
/// </summary>
Api_key,
/// <summary>
Expand All @@ -78,7 +78,7 @@ public static string ApiKeyHeaderToString(ApiKeyHeader value)
switch(value)
{
case ApiKeyHeader.Api_key:
return "api_key";
return "api-key";
case ApiKeyHeader.Api_key_query:
return "api_key_query";
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ Authentication schemes defined for the API:
### api_key

- **Type**: API key
- **API key parameter name**: api_key
- **API key parameter name**: api-key
- **Location**: HTTP header

<a id="api_key_query"></a>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3069,7 +3069,7 @@ components:
type: oauth2
api_key:
in: header
name: api_key
name: api-key
type: apiKey
api_key_query:
in: query
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -409,9 +409,9 @@ namespace Example
Configuration config = new Configuration();
config.BasePath = "http://petstore.swagger.io:80/v2";
// Configure API key authorization: api_key
config.AddApiKey("api_key", "YOUR_API_KEY");
config.AddApiKey("api-key", "YOUR_API_KEY");
// Uncomment below to setup prefix (e.g. Bearer) for API key, if needed
// config.AddApiKeyPrefix("api_key", "Bearer");
// config.AddApiKeyPrefix("api-key", "Bearer");
// Configure API key authorization: api_key_query
config.AddApiKey("api_key_query", "YOUR_API_KEY");
// Uncomment below to setup prefix (e.g. Bearer) for API key, if needed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,9 @@ namespace Example
Configuration config = new Configuration();
config.BasePath = "http://petstore.swagger.io:80/v2";
// Configure API key authorization: api_key
config.AddApiKey("api_key", "YOUR_API_KEY");
config.AddApiKey("api-key", "YOUR_API_KEY");
// Uncomment below to setup prefix (e.g. Bearer) for API key, if needed
// config.AddApiKeyPrefix("api_key", "Bearer");
// config.AddApiKeyPrefix("api-key", "Bearer");
var apiInstance = new StoreApi(config);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1699,7 +1699,7 @@ public async Task<IGetPetByIdApiResponse> GetPetByIdAsync(long petId, System.Thr
System.Collections.Specialized.NameValueCollection parseQueryStringLocalVar = System.Web.HttpUtility.ParseQueryString(string.Empty);

List<TokenBase> tokenBaseLocalVars = new List<TokenBase>();
ApiKeyToken apiKeyTokenLocalVar1 = (ApiKeyToken) await ApiKeyProvider.GetAsync("api_key", cancellationToken).ConfigureAwait(false);
ApiKeyToken apiKeyTokenLocalVar1 = (ApiKeyToken) await ApiKeyProvider.GetAsync("api-key", cancellationToken).ConfigureAwait(false);
tokenBaseLocalVars.Add(apiKeyTokenLocalVar1);
apiKeyTokenLocalVar1.UseInHeader(httpRequestMessageLocalVar);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -618,7 +618,7 @@ public async Task<IGetInventoryApiResponse> GetInventoryAsync(System.Threading.C
uriBuilderLocalVar.Path = ClientUtils.CONTEXT_PATH + "/store/inventory";

List<TokenBase> tokenBaseLocalVars = new List<TokenBase>();
ApiKeyToken apiKeyTokenLocalVar1 = (ApiKeyToken) await ApiKeyProvider.GetAsync("api_key", cancellationToken).ConfigureAwait(false);
ApiKeyToken apiKeyTokenLocalVar1 = (ApiKeyToken) await ApiKeyProvider.GetAsync("api-key", cancellationToken).ConfigureAwait(false);
tokenBaseLocalVars.Add(apiKeyTokenLocalVar1);
apiKeyTokenLocalVar1.UseInHeader(httpRequestMessageLocalVar);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ static ClientUtils()
public enum ApiKeyHeader
{
/// <summary>
/// The api_key header
/// The api-key header
/// </summary>
Api_key,
/// <summary>
Expand All @@ -78,7 +78,7 @@ public static string ApiKeyHeaderToString(ApiKeyHeader value)
switch(value)
{
case ApiKeyHeader.Api_key:
return "api_key";
return "api-key";
case ApiKeyHeader.Api_key_query:
return "api_key_query";
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ Authentication schemes defined for the API:
### api_key

- **Type**: API key
- **API key parameter name**: api_key
- **API key parameter name**: api-key
- **Location**: HTTP header

<a id="api_key_query"></a>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3045,7 +3045,7 @@ components:
type: oauth2
api_key:
in: header
name: api_key
name: api-key
type: apiKey
api_key_query:
in: query
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -409,9 +409,9 @@ namespace Example
Configuration config = new Configuration();
config.BasePath = "http://petstore.swagger.io:80/v2";
// Configure API key authorization: api_key
config.AddApiKey("api_key", "YOUR_API_KEY");
config.AddApiKey("api-key", "YOUR_API_KEY");
// Uncomment below to setup prefix (e.g. Bearer) for API key, if needed
// config.AddApiKeyPrefix("api_key", "Bearer");
// config.AddApiKeyPrefix("api-key", "Bearer");
// Configure API key authorization: api_key_query
config.AddApiKey("api_key_query", "YOUR_API_KEY");
// Uncomment below to setup prefix (e.g. Bearer) for API key, if needed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,9 @@ namespace Example
Configuration config = new Configuration();
config.BasePath = "http://petstore.swagger.io:80/v2";
// Configure API key authorization: api_key
config.AddApiKey("api_key", "YOUR_API_KEY");
config.AddApiKey("api-key", "YOUR_API_KEY");
// Uncomment below to setup prefix (e.g. Bearer) for API key, if needed
// config.AddApiKeyPrefix("api_key", "Bearer");
// config.AddApiKeyPrefix("api-key", "Bearer");
var apiInstance = new StoreApi(config);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1699,7 +1699,7 @@ public async Task<IGetPetByIdApiResponse> GetPetByIdAsync(long petId, System.Thr
System.Collections.Specialized.NameValueCollection parseQueryStringLocalVar = System.Web.HttpUtility.ParseQueryString(string.Empty);

List<TokenBase> tokenBaseLocalVars = new List<TokenBase>();
ApiKeyToken apiKeyTokenLocalVar1 = (ApiKeyToken) await ApiKeyProvider.GetAsync("api_key", cancellationToken).ConfigureAwait(false);
ApiKeyToken apiKeyTokenLocalVar1 = (ApiKeyToken) await ApiKeyProvider.GetAsync("api-key", cancellationToken).ConfigureAwait(false);
tokenBaseLocalVars.Add(apiKeyTokenLocalVar1);
apiKeyTokenLocalVar1.UseInHeader(httpRequestMessageLocalVar);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -618,7 +618,7 @@ public async Task<IGetInventoryApiResponse> GetInventoryAsync(System.Threading.C
uriBuilderLocalVar.Path = ClientUtils.CONTEXT_PATH + "/store/inventory";

List<TokenBase> tokenBaseLocalVars = new List<TokenBase>();
ApiKeyToken apiKeyTokenLocalVar1 = (ApiKeyToken) await ApiKeyProvider.GetAsync("api_key", cancellationToken).ConfigureAwait(false);
ApiKeyToken apiKeyTokenLocalVar1 = (ApiKeyToken) await ApiKeyProvider.GetAsync("api-key", cancellationToken).ConfigureAwait(false);
tokenBaseLocalVars.Add(apiKeyTokenLocalVar1);
apiKeyTokenLocalVar1.UseInHeader(httpRequestMessageLocalVar);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ static ClientUtils()
public enum ApiKeyHeader
{
/// <summary>
/// The api_key header
/// The api-key header
/// </summary>
Api_key,
/// <summary>
Expand All @@ -78,7 +78,7 @@ public static string ApiKeyHeaderToString(ApiKeyHeader value)
switch(value)
{
case ApiKeyHeader.Api_key:
return "api_key";
return "api-key";
case ApiKeyHeader.Api_key_query:
return "api_key_query";
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ Authentication schemes defined for the API:
### api_key

- **Type**: API key
- **API key parameter name**: api_key
- **API key parameter name**: api-key
- **Location**: HTTP header

<a id="api_key_query"></a>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3069,7 +3069,7 @@ components:
type: oauth2
api_key:
in: header
name: api_key
name: api-key
type: apiKey
api_key_query:
in: query
Expand Down
Loading

0 comments on commit fb5e69f

Please sign in to comment.