Skip to content

Commit fadc853

Browse files
Copilotdesjoerd
andcommitted
Rewrite type checking logic to avoid cryptic binary operators
Co-authored-by: desjoerd <2460430+desjoerd@users.noreply.github.com>
1 parent cf00a1e commit fadc853

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

src/Microsoft.OpenApi/Models/OpenApiSchema.cs

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -877,22 +877,35 @@ private static (IList<IOpenApiSchema>? effective, JsonSchemaType? inferredType,
877877

878878
if (nonNullSchemas.Count > 0)
879879
{
880-
JsonSchemaType commonType = 0;
880+
// Check if all schemas have the same type
881+
JsonSchemaType? firstType = null;
882+
bool allSameType = true;
881883

882884
foreach (var schema in nonNullSchemas)
883885
{
884-
commonType |= schema.Type.GetValueOrDefault() & ~JsonSchemaType.Null;
886+
var schemaType = schema.Type;
887+
if (schemaType.HasValue && schemaType.Value != JsonSchemaType.Null)
888+
{
889+
if (firstType == null)
890+
{
891+
firstType = schemaType.Value;
892+
}
893+
else if (firstType != schemaType.Value)
894+
{
895+
allSameType = false;
896+
break;
897+
}
898+
}
885899
}
886900

887-
// Check if commonType is a single flag (power of 2) indicating all schemas share the same type
888-
var isSingleType = commonType != 0 && (commonType & (commonType - 1)) == 0;
889-
if (isSingleType)
901+
if (allSameType && firstType.HasValue)
890902
{
891-
// Single common type
892-
return (nonNullSchemas, commonType, true);
903+
// All schemas share the same type
904+
return (nonNullSchemas, firstType.Value, true);
893905
}
894906
else
895907
{
908+
// Multiple different types
896909
return (nonNullSchemas, null, true);
897910
}
898911

0 commit comments

Comments
 (0)