Skip to content

Commit

Permalink
feat: Removed Required from enum properties with single value.
Browse files Browse the repository at this point in the history
  • Loading branch information
HavenDV committed Aug 18, 2024
1 parent 1e5f719 commit a66998d
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/libs/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
</ItemGroup>

<PropertyGroup Label="Versioning">
<Version>0.15.5</Version>
<Version>0.15.6</Version>
<MinVerMinimumMajorMinor>0.1</MinVerMinimumMajorMinor>
<MinVerTagPrefix>v</MinVerTagPrefix>
<MinVerDefaultPreReleaseIdentifiers>dev</MinVerDefaultPreReleaseIdentifiers>
Expand Down
6 changes: 0 additions & 6 deletions src/libs/OpenApiGenerator.Core/Generation/Sources.Models.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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};";
}
Expand Down
13 changes: 10 additions & 3 deletions src/libs/OpenApiGenerator.Core/Models/PropertyData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,20 @@ public static PropertyData FromSchemaContext(SchemaContext context)
? new HashSet<string>(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,
Expand Down

0 comments on commit a66998d

Please sign in to comment.