Skip to content

Commit 8c0300f

Browse files
committed
fix: Improved enum/boolean detection for some cases.
1 parent 4826f63 commit 8c0300f

File tree

3 files changed

+13
-3
lines changed

3 files changed

+13
-3
lines changed

src/libs/OpenApiGenerator.Core/Extensions/OpenApiSchemaExtensions.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using Microsoft.OpenApi.Any;
12
using Microsoft.OpenApi.Models;
23

34
namespace OpenApiGenerator.Core.Extensions;
@@ -47,7 +48,16 @@ public static bool IsEnum(
4748
{
4849
schema = schema ?? throw new ArgumentNullException(nameof(schema));
4950

50-
return schema.Type == "string" && schema.Enum.Any();
51+
return schema.Enum.Any() && schema.Type is "string" or null;
52+
}
53+
54+
public static bool IsBoolean(
55+
this OpenApiSchema schema)
56+
{
57+
schema = schema ?? throw new ArgumentNullException(nameof(schema));
58+
59+
return schema.Type == "boolean" ||
60+
schema.Default is OpenApiBoolean;
5161
}
5262

5363
public static bool IsBase64(

src/libs/OpenApiGenerator.Core/Models/SchemaContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ private static string ComputeType(OpenApiSchema schema)
120120
{
121121
return "class";
122122
}
123-
if (schema.Type == "boolean")
123+
if (schema.IsBoolean())
124124
{
125125
return "bool";
126126
}

src/libs/OpenApiGenerator.Core/Models/TypeData.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ public static string GetCSharpType(SchemaContext context, SchemaContext? additio
213213
("array", _) =>
214214
($"{context.Children.FirstOrDefault(x => x.Hint == Hint.ArrayItem)?.TypeData?.CSharpTypeWithoutNullability}".AsArray(), true),
215215

216-
(null, null) when context.IsClass =>
216+
(null, null) when context.IsClass || context.IsEnum =>
217217
($"global::{context.Settings.Namespace}.{context.Id}", true),
218218
(null, null) => ("object", true),
219219
("null", _) => ("object", true),

0 commit comments

Comments
 (0)