From a66998dafa4d2839f2c8ec275235e1051a78f98b Mon Sep 17 00:00:00 2001 From: HavenDV Date: Sun, 18 Aug 2024 17:37:41 +0400 Subject: [PATCH] feat: Removed Required from enum properties with single value. --- src/libs/Directory.Build.props | 2 +- .../Generation/Sources.Models.cs | 6 ------ .../OpenApiGenerator.Core/Models/PropertyData.cs | 13 ++++++++++--- 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/src/libs/Directory.Build.props b/src/libs/Directory.Build.props index 34307be897..fac61fe156 100644 --- a/src/libs/Directory.Build.props +++ b/src/libs/Directory.Build.props @@ -39,7 +39,7 @@ - 0.15.5 + 0.15.6 0.1 v dev diff --git a/src/libs/OpenApiGenerator.Core/Generation/Sources.Models.cs b/src/libs/OpenApiGenerator.Core/Generation/Sources.Models.cs index 509af2b9da..1e159ca2a7 100644 --- a/src/libs/OpenApiGenerator.Core/Generation/Sources.Models.cs +++ b/src/libs/OpenApiGenerator.Core/Generation/Sources.Models.cs @@ -63,12 +63,6 @@ private static string GetDefaultValue(PropertyData property, bool isRequiredKeyw { return " = default!;"; } - - // Special case for enums with a single value. - if (property is { IsRequired: true, Type: { IsEnum: true, EnumValues.Length: 1 } }) - { - return $" = {property.Type.CSharpTypeWithoutNullability}.{property.Type.EnumValues[0].ToEnumValue(property.Settings).Name};"; - } return property.IsRequired || string.IsNullOrWhiteSpace(property.DefaultValue) ? string.Empty : $" = {property.DefaultValue};"; } diff --git a/src/libs/OpenApiGenerator.Core/Models/PropertyData.cs b/src/libs/OpenApiGenerator.Core/Models/PropertyData.cs index 5b22c53a67..803d4dfe70 100644 --- a/src/libs/OpenApiGenerator.Core/Models/PropertyData.cs +++ b/src/libs/OpenApiGenerator.Core/Models/PropertyData.cs @@ -55,13 +55,20 @@ public static PropertyData FromSchemaContext(SchemaContext context) ? new HashSet(context.Parent.Schema.Required) : []; + var isRequired = context.Hint is Hint.Parameter + ? context.Parameter?.Required == true || context.Parameter?.In == Microsoft.OpenApi.Models.ParameterLocation.Path + : requiredProperties.Contains(propertyName); + // Special case for enums with a single value. + if (isRequired && type is { IsEnum: true, EnumValues.Length: 1 }) + { + isRequired = false; + } + return new PropertyData( Id: propertyName, Name: name, Type: type, - IsRequired: context.Hint is Hint.Parameter - ? context.Parameter?.Required == true || context.Parameter?.In == Microsoft.OpenApi.Models.ParameterLocation.Path - : requiredProperties.Contains(propertyName), + IsRequired: isRequired, IsMultiPartFormDataFilename: false, ParameterLocation: context.Parameter?.In, ParameterStyle: context.Parameter?.Style,