Visitar URL original
fix/boolean schemas to v2 by baywet · Pull Request #2840 · microsoft/OpenAPI.NET · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
perf(schema): optimize boolean schema deserialization
Add generic GetValue<T>() method to ValueNode to allow direct type access without
string conversion. Update V31 and V32 schema deserializers to use GetValue<bool>()
directly instead of converting to string and parsing with bool.TryParse().

This eliminates unnecessary string allocations and conversions for boolean schema
values, improving performance in the deserialization hot path.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
(cherry picked from commit 7316e3f)
  • Loading branch information
baywet and Copilot committed Apr 27, 2026
commit fe6f5138b8fbb2368c9a011df77a940089e91647
11 changes: 11 additions & 0 deletions src/Microsoft.OpenApi/Reader/ParseNodes/ValueNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,17 @@ public override string GetScalarValue()
?? throw new OpenApiReaderException($"Expected a value at {Context.GetLocation()}.");
}

/// <summary>
/// Attempts to get the underlying value directly as the specified type without string conversion.
/// </summary>
/// <typeparam name="T">The type to retrieve the value as.</typeparam>
/// <param name="value">The retrieved value if successful.</param>
/// <returns>True if the value was successfully converted to the specified type; otherwise, false.</returns>
public bool TryGetValue<T>(out T? value)
{
return _node.TryGetValue(out value);
}

/// <summary>
/// Create a <see cref="JsonNode"/>
/// </summary>
Expand Down
18 changes: 7 additions & 11 deletions src/Microsoft.OpenApi/Reader/V31/OpenApiSchemaDeserializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -367,20 +367,16 @@ internal static partial class OpenApiV31Deserializer
public static IOpenApiSchema LoadSchema(ParseNode node, OpenApiDocument hostDocument)
{
// Handle boolean schemas (true/false) for JSON Schema 2020-12 compatibility
if (node is ValueNode valueNode)
if (node is ValueNode valueNode && valueNode.TryGetValue<bool>(out var boolValue))
{
var value = valueNode.GetScalarValue();
if (value != null && bool.TryParse(value, out var boolValue))
var boolSchema = new OpenApiSchema();
if (!boolValue)
{
var boolSchema = new OpenApiSchema();
if (!boolValue)
{
// false schema: represents "not valid" -> convert to "not: {}"
boolSchema.Not = new OpenApiSchema();
}
// true schema: represents "always valid" -> return empty schema (default)
return boolSchema;
// false schema: represents "not valid" -> convert to "not: {}"
boolSchema.Not = new OpenApiSchema();
}
// true schema: represents "always valid" -> return empty schema (default)
return boolSchema;
}

var mapNode = node.CheckMapNode(OpenApiConstants.Schema);
Expand Down