Skip to content

Commit

Permalink
feat: Added ModelStyle setting.
Browse files Browse the repository at this point in the history
  • Loading branch information
HavenDV committed Dec 6, 2023
1 parent 7f4f7a1 commit eef7188
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/libs/OpenApiGenerator/Models/Model.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
namespace OpenApiGenerator.Models;

internal readonly record struct Model(
ModelStyle Style,
EquatableArray<Property> Properties);
8 changes: 8 additions & 0 deletions src/libs/OpenApiGenerator/Models/ModelStyle.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace OpenApiGenerator.Models;

internal enum ModelStyle
{
Class,
Record,
ReadonlyRecordStruct,
}
5 changes: 4 additions & 1 deletion src/libs/OpenApiGenerator/Models/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@ namespace OpenApiGenerator.Models;
internal readonly record struct Settings(
string TargetFramework,
string Namespace,
bool UseNSwag);
bool UseNSwag,

bool GenerateModels,
ModelStyle ModelStyle);
14 changes: 12 additions & 2 deletions src/libs/OpenApiGenerator/OpenApiGenerator.props
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,26 @@
<!-- Used as default TargetFramework if OpenApiGenerator_TargetFramework is not specified -->
<CompilerVisibleProperty Include="TargetFramework"/>

<!-- Namespace. Default: $(TargetFramework) or "netstandard2.0" -->
<CompilerVisibleProperty Include="OpenApiGenerator_TargetFramework"/>
<!-- Namespace. Default: $(PackageId) or $(AssemblyName) or "OpenApiGenerator" -->
<CompilerVisibleProperty Include="OpenApiGenerator_Namespace"/>
<!-- true/false. Default: false -->
<CompilerVisibleProperty Include="OpenApiGenerator_UseCache"/>
<!-- true/false. Default: false -->
<CompilerVisibleProperty Include="OpenApiGenerator_UseNSwag"/>
<CompilerVisibleProperty Include="OpenApiGenerator_Models_Generate"/>

<!-- true/false. Default: false -->
<CompilerVisibleProperty Include="OpenApiGenerator_GenerateModels"/>
<!-- Class/Record/ReadonlyRecordStruct. Default: Class -->
<CompilerVisibleProperty Include="OpenApiGenerator_ModelStyle"/>

<!-- true/false. Default: false -->
<CompilerVisibleProperty Include="OpenApiGenerator_GenerateSdk"/>
</ItemGroup>

<ItemGroup>
<AdditionalFiles Include="**\*.yaml" GenerateModels="true" />
<AdditionalFiles Include="**\*.yaml" />
</ItemGroup>

</Project>
14 changes: 13 additions & 1 deletion src/libs/OpenApiGenerator/OptionsExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,19 @@ public static IncrementalValueProvider<Settings> DetectSettings(
options.GetGlobalOption("PackageId") ??
options.GetGlobalOption("AssemblyName") ??
prefix,
UseNSwag: bool.Parse(options.GetGlobalOption("UseNSwag", prefix) ?? bool.FalseString)
UseNSwag: bool.TryParse(
options.GetGlobalOption("UseNSwag", prefix),
out var useNSwag) && useNSwag,

GenerateModels: bool.TryParse(
options.GetGlobalOption(nameof(Settings.GenerateModels), prefix),
out var generateModels) && generateModels,
ModelStyle: Enum.TryParse<ModelStyle>(
options.GetGlobalOption(nameof(Settings.ModelStyle), prefix) ??
$"{default(ModelStyle):G}",
ignoreCase: true,
out var modelStyle) ? modelStyle : default

));
}
}

0 comments on commit eef7188

Please sign in to comment.