Skip to content

Commit

Permalink
fix: Fixed OpenAPI 3.1 json support for null types.
Browse files Browse the repository at this point in the history
  • Loading branch information
HavenDV committed Oct 12, 2024
1 parent 325a222 commit 8a4cf83
Show file tree
Hide file tree
Showing 889 changed files with 11,481 additions and 24,047 deletions.
62 changes: 45 additions & 17 deletions src/libs/AutoSDK/Helpers/OpenApi31Support.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,33 +78,51 @@ private static void ConvertJsonNode(JsonObject node)
{
node["openapi"] = "3.0.3";
}
if (keyString == "anyOf" &&
entry.Value is JsonArray anyOfList && anyOfList.Count == 2 &&

if (keyString is "anyOf" or "oneOf" &&
entry.Value is JsonArray { Count: 2 } anyOfList &&
anyOfList.Any(v =>
v is JsonObject objects &&
objects.ContainsKey("type") &&
objects["type"] == null))
objects["type"]?.ToJsonString() == "\"null\""))
{
var first = anyOfList[0] as JsonObject;
var second = anyOfList[1] as JsonObject;
var firstType = first?["type"];
var secondType = second?["type"];

if (firstType == null || secondType == null)
// Replace "anyOf" with "type: string, nullable: true"
node["type"] = firstType?.ToJsonString() == "\"null\""
? secondType?.DeepClone()
: firstType?.DeepClone();
node["nullable"] = true;

var nonNullObject = firstType?.ToJsonString() == "\"null\""
? second
: first;
foreach (var kvp in nonNullObject!)
{
// Replace "anyOf" with "type: string, nullable: true"
node["type"] = firstType?.ToString() ?? secondType?.ToString();
node["nullable"] = true;

var nonNullObject = firstType != null ? first : second;
foreach (var kvp in nonNullObject!)
{
node[kvp.Key] = kvp.Value;
}

node.Remove(keyString);
node[kvp.Key] = kvp.Value?.DeepClone();
}

node.Remove(keyString);
}

if (keyString is "anyOf" or "oneOf" &&
entry.Value is JsonArray { Count: > 2 } anyOfList3 &&
anyOfList3.Any(v =>
v is JsonObject objects &&
objects.ContainsKey("type") &&
objects["type"]?.ToJsonString() == "\"null\""))
{
node[keyString] = new JsonArray(anyOfList3
.Where(v =>
v is JsonObject objects &&
(!objects.ContainsKey("type") ||
objects["type"]?.ToJsonString() != "\"null\""))
.Select(v => v?.DeepClone())
.ToArray());
node["nullable"] = true;
}

if (keyString == "exclusiveMinimum" && entry.Value is not JsonValue)
Expand Down Expand Up @@ -233,7 +251,7 @@ private static void ConvertYamlNode(Dictionary<object, object?> node)
}

var keyString = entry.Key as string;
if (keyString == "anyOf" &&
if (keyString is "anyOf" or "oneOf" &&
entry.Value is List<object?> anyOfList && anyOfList.Count == 2 &&
anyOfList.Any(v =>
v is Dictionary<object, object?> objects &&
Expand Down Expand Up @@ -284,6 +302,16 @@ private static void ConvertYamlNode(Dictionary<object, object?> node)
node[new string("exclusiveMaximum".ToCharArray())] = true;
}

// Remove "prefixItems"
if (keyString == "prefixItems" && entry.Value is List<object?> { Count: > 0 } prefixItemsList)
{
node.Remove(keyString);
if (!node.ContainsKey("items"))
{
node[new string("items".ToCharArray())] = prefixItemsList[0];
}
}

// Identify "type" that is a list containing "string" and "null"
if (keyString == "type" &&
entry.Value is List<object?> typeList &&
Expand Down
Loading

0 comments on commit 8a4cf83

Please sign in to comment.