Skip to content

Commit 38954dc

Browse files
committed
Add null check for type; simplify condition to verify flag has been set
1 parent 3abe36c commit 38954dc

File tree

1 file changed

+20
-17
lines changed

1 file changed

+20
-17
lines changed

src/Microsoft.OpenApi/Models/OpenApiSchema.cs

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) Microsoft Corporation. All rights reserved.
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT license.
33

44
using System;
@@ -810,18 +810,21 @@ private void SerializeTypeProperty(JsonSchemaType? type, IOpenApiWriter writer,
810810
}
811811
else
812812
{
813-
var list = new List<JsonSchemaType>();
814-
foreach (JsonSchemaType flag in System.Enum.GetValues(typeof(JsonSchemaType)))
813+
if (type is not null)
815814
{
816-
if ((type & flag) == flag)
815+
var list = new List<JsonSchemaType?>();
816+
foreach (JsonSchemaType flag in System.Enum.GetValues(typeof(JsonSchemaType)))
817817
{
818-
list.Add(flag);
818+
if (type.Value.HasFlag(flag))
819+
{
820+
list.Add(flag);
821+
}
819822
}
820-
}
821-
822-
writer.WriteOptionalCollection(OpenApiConstants.Type, list, (w, s) => w.WriteValue(OpenApiTypeMapper.ToIdentifier(s)));
823+
824+
writer.WriteOptionalCollection(OpenApiConstants.Type, list, (w, s) => w.WriteValue(s.ToIdentifier()));
825+
}
823826
}
824-
}
827+
}
825828
}
826829

827830
private static int CountEnumSetFlags(JsonSchemaType? schemaType)
@@ -834,7 +837,7 @@ private static int CountEnumSetFlags(JsonSchemaType? schemaType)
834837
foreach (JsonSchemaType value in System.Enum.GetValues(typeof(JsonSchemaType)))
835838
{
836839
// Ignore the None flag and check if the flag is set
837-
if ((schemaType.Value.HasFlag(value))
840+
if (schemaType.Value.HasFlag(value))
838841
{
839842
count++;
840843
}
@@ -849,12 +852,12 @@ private void UpCastSchemaTypeToV31(JsonSchemaType? type, IOpenApiWriter writer)
849852
// create a new array and insert the type and "null" as values
850853
Type = type | JsonSchemaType.Null;
851854
var list = new List<string>();
852-
foreach (JsonSchemaType flag in System.Enum.GetValues(typeof(JsonSchemaType)))
855+
foreach (JsonSchemaType? flag in System.Enum.GetValues(typeof(JsonSchemaType)))
853856
{
854857
// Check if the flag is set in 'type' using a bitwise AND operation
855-
if ((Type & flag) == flag)
858+
if (Type.Value.HasFlag(flag))
856859
{
857-
list.Add(OpenApiTypeMapper.ToIdentifier(flag));
860+
list.Add(flag.ToIdentifier());
858861
}
859862
}
860863

@@ -881,18 +884,18 @@ private void DowncastTypeArrayToV2OrV3(JsonSchemaType? schemaType, IOpenApiWrite
881884
}
882885
else
883886
{
884-
writer.WriteProperty(OpenApiConstants.Type, OpenApiTypeMapper.ToIdentifier(schemaType));
887+
writer.WriteProperty(OpenApiConstants.Type, schemaType.ToIdentifier());
885888
}
886889
}
887890
else if (flagsCount is 2 && (schemaType & JsonSchemaType.Null) == JsonSchemaType.Null) // checks for two values and one is null
888891
{
889-
foreach (JsonSchemaType flag in System.Enum.GetValues(typeof(JsonSchemaType)))
892+
foreach (JsonSchemaType? flag in System.Enum.GetValues(typeof(JsonSchemaType)))
890893
{
891894
// Skip if the flag is not set or if it's the Null flag
892-
if ((schemaType & flag) == flag && flag != JsonSchemaType.Null)
895+
if (schemaType.Value.HasFlag(flag) && flag != JsonSchemaType.Null)
893896
{
894897
// Write the non-null flag value to the writer
895-
writer.WriteProperty(OpenApiConstants.Type, OpenApiTypeMapper.ToIdentifier(flag));
898+
writer.WriteProperty(OpenApiConstants.Type, flag.ToIdentifier());
896899
}
897900
}
898901
if (!Nullable)

0 commit comments

Comments
 (0)