Skip to content

Commit

Permalink
Merge pull request #211 from atc-net/hotfix/client-generation
Browse files Browse the repository at this point in the history
Hotfix/client generation
  • Loading branch information
perkops authored Dec 13, 2024
2 parents e36b618 + 242b90d commit 76dbf20
Show file tree
Hide file tree
Showing 562 changed files with 21,503 additions and 4,691 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ private static void AppendParameters(
{
var useListForDataType = openApiParameter.Schema.IsTypeArray();

var parameterName = openApiParameter.Name.EnsureValidFormattedPropertyName();

var dataType = useListForDataType
? openApiParameter.Schema.Items.GetDataType()
: openApiParameter.Schema.GetDataType();
Expand All @@ -47,17 +49,27 @@ private static void AppendParameters(

if (parameters.FirstOrDefault(x => x.Name == openApiParameter.Name) is null)
{
var defaultValueInitializer = openApiParameter.Schema.GetDefaultValueAsString();

if (!string.IsNullOrEmpty(defaultValueInitializer) &&
openApiParameter.ContainsEnumInSchemaOrProperties())
{
defaultValueInitializer = dataType.Equals(parameterName, StringComparison.Ordinal)
? $"{ContentGeneratorConstants.Contracts}.{dataType}.{defaultValueInitializer.PascalCase(ApiOperationExtractor.ModelNameSeparators, removeSeparators: true)}"
: $"{dataType}.{defaultValueInitializer.PascalCase(ApiOperationExtractor.ModelNameSeparators, removeSeparators: true)}";
}

parameters.Add(new ContentGeneratorClientParameterParametersProperty(
openApiParameter.Name,
openApiParameter.Name.EnsureValidFormattedPropertyName(),
parameterName,
openApiParameter.ExtractDocumentationTags(),
dataType,
isSimpleType,
useListForDataType,
GetIsNullable(openApiParameter, useListForDataType),
openApiParameter.Required,
GetAdditionalValidationAttributes(openApiParameter),
openApiParameter.Schema.GetDefaultValueAsString()));
defaultValueInitializer));
}
}
}
Expand Down Expand Up @@ -94,7 +106,7 @@ private static void AppendParametersFromBody(
var requestBodyType = "string?";
if (requestSchema.Reference is not null)
{
requestBodyType = requestSchema.Reference.Id.EnsureFirstCharacterToUpper();
requestBodyType = requestSchema.Reference.Id.PascalCase(ApiOperationExtractor.ModelNameSeparators, removeSeparators: true);
}
else if (isFormatTypeOfBinary)
{
Expand All @@ -106,7 +118,7 @@ private static void AppendParametersFromBody(
}
else if (requestSchema.Items is not null)
{
requestBodyType = requestSchema.Items.Reference.Id.EnsureFirstCharacterToUpper();
requestBodyType = requestSchema.Items.Reference.Id.PascalCase(ApiOperationExtractor.ModelNameSeparators, removeSeparators: true);
}

parameters.Add(new ContentGeneratorClientParameterParametersProperty(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,15 +262,15 @@ public static bool IsUsingRequiredForSystemTextJsonSerializationAndSystemRuntime

foreach (var apiParameter in apiPathPair.Value.Parameters)
{
if (apiParameter.IsSchemaEnumAndUseJsonString())
if (apiParameter.IsSchemaEnumAndUsesJsonString())
{
return true;
}
}

foreach (var apiParameter in apiOperationPair.Value.Parameters)
{
if (apiParameter.IsSchemaEnumAndUseJsonString())
if (apiParameter.IsSchemaEnumAndUsesJsonString())
{
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ namespace Atc.Rest.ApiGenerator.OpenApi.Extensions;

public static class OpenApiParameterExtensions
{
public static bool IsSchemaEnumAndUseJsonString(this OpenApiParameter apiParameter)
public static bool IsSchemaEnumAndUsesJsonString(
this OpenApiParameter apiParameter)
{
if (apiParameter.Schema.IsSchemaEnum())
{
Expand All @@ -24,6 +25,13 @@ public static bool IsSchemaEnumAndUseJsonString(this OpenApiParameter apiParamet
return false;
}

public static bool ContainsEnumInSchemaOrProperties(
this OpenApiParameter apiParameter)
=> apiParameter.Schema.IsSchemaEnumOrPropertyEnum() ||
apiParameter.Schema.AllOf.Any(allOfSchema => allOfSchema.IsSchemaEnumOrPropertyEnum()) ||
apiParameter.Schema.OneOf.Any(oneOfSchema => oneOfSchema.IsSchemaEnumOrPropertyEnum()) ||
apiParameter.Schema.AnyOf.Any(anyOfSchema => anyOfSchema.IsSchemaEnumOrPropertyEnum());

public static CodeDocumentationTags ExtractDocumentationTags(
this OpenApiParameter apiParameter)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace Atc.Rest.ApiGenerator.OpenApi.Extractors;

public sealed class ApiOperationExtractor : IApiOperationExtractor
{
public static readonly char[] ModelNameSeparators = { ' ', '-', '_', '.' };
public static readonly char[] ModelNameSeparators = [' ', '-', '_', '.'];

public IList<ApiOperation> Extract(
OpenApiDocument apiDocument)
Expand Down
Loading

0 comments on commit 76dbf20

Please sign in to comment.