Skip to content

Commit

Permalink
fix: Improved enum/boolean detection for some cases.
Browse files Browse the repository at this point in the history
  • Loading branch information
HavenDV committed Aug 28, 2024
1 parent 4826f63 commit 8c0300f
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Models;

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

return schema.Type == "string" && schema.Enum.Any();
return schema.Enum.Any() && schema.Type is "string" or null;
}

public static bool IsBoolean(
this OpenApiSchema schema)
{
schema = schema ?? throw new ArgumentNullException(nameof(schema));

return schema.Type == "boolean" ||
schema.Default is OpenApiBoolean;
}

public static bool IsBase64(
Expand Down
2 changes: 1 addition & 1 deletion src/libs/OpenApiGenerator.Core/Models/SchemaContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ private static string ComputeType(OpenApiSchema schema)
{
return "class";
}
if (schema.Type == "boolean")
if (schema.IsBoolean())
{
return "bool";
}
Expand Down
2 changes: 1 addition & 1 deletion src/libs/OpenApiGenerator.Core/Models/TypeData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ public static string GetCSharpType(SchemaContext context, SchemaContext? additio
("array", _) =>
($"{context.Children.FirstOrDefault(x => x.Hint == Hint.ArrayItem)?.TypeData?.CSharpTypeWithoutNullability}".AsArray(), true),

(null, null) when context.IsClass =>
(null, null) when context.IsClass || context.IsEnum =>
($"global::{context.Settings.Namespace}.{context.Id}", true),
(null, null) => ("object", true),
("null", _) => ("object", true),
Expand Down

0 comments on commit 8c0300f

Please sign in to comment.