Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Define JSON schema's Type property as a flaggable enum to allow storing multiple values #1897

Merged
merged 19 commits into from
Oct 31, 2024
Merged
Changes from 1 commit
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
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
Add null check for type; simplify condition to verify flag has been set
  • Loading branch information
MaggieKimani1 committed Oct 30, 2024
commit 38954dce7ddeac66b37132a832e698da798509bd
37 changes: 20 additions & 17 deletions src/Microsoft.OpenApi/Models/OpenApiSchema.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.

using System;
Expand Down Expand Up @@ -810,18 +810,21 @@ private void SerializeTypeProperty(JsonSchemaType? type, IOpenApiWriter writer,
}
else
{
var list = new List<JsonSchemaType>();
foreach (JsonSchemaType flag in System.Enum.GetValues(typeof(JsonSchemaType)))
if (type is not null)
{
if ((type & flag) == flag)
var list = new List<JsonSchemaType?>();
foreach (JsonSchemaType flag in System.Enum.GetValues(typeof(JsonSchemaType)))
{
list.Add(flag);
if (type.Value.HasFlag(flag))
{
list.Add(flag);
}
}
}

writer.WriteOptionalCollection(OpenApiConstants.Type, list, (w, s) => w.WriteValue(OpenApiTypeMapper.ToIdentifier(s)));

writer.WriteOptionalCollection(OpenApiConstants.Type, list, (w, s) => w.WriteValue(s.ToIdentifier()));
}
}
}
}
}

private static int CountEnumSetFlags(JsonSchemaType? schemaType)
Expand All @@ -834,7 +837,7 @@ private static int CountEnumSetFlags(JsonSchemaType? schemaType)
foreach (JsonSchemaType value in System.Enum.GetValues(typeof(JsonSchemaType)))
{
// Ignore the None flag and check if the flag is set
if ((schemaType.Value.HasFlag(value))
if (schemaType.Value.HasFlag(value))
{
count++;
}
Expand All @@ -849,12 +852,12 @@ private void UpCastSchemaTypeToV31(JsonSchemaType? type, IOpenApiWriter writer)
// create a new array and insert the type and "null" as values
Type = type | JsonSchemaType.Null;
var list = new List<string>();
foreach (JsonSchemaType flag in System.Enum.GetValues(typeof(JsonSchemaType)))
foreach (JsonSchemaType? flag in System.Enum.GetValues(typeof(JsonSchemaType)))
{
// Check if the flag is set in 'type' using a bitwise AND operation
if ((Type & flag) == flag)
if (Type.Value.HasFlag(flag))
{
list.Add(OpenApiTypeMapper.ToIdentifier(flag));
list.Add(flag.ToIdentifier());
}
}

Expand All @@ -881,18 +884,18 @@ private void DowncastTypeArrayToV2OrV3(JsonSchemaType? schemaType, IOpenApiWrite
}
else
{
writer.WriteProperty(OpenApiConstants.Type, OpenApiTypeMapper.ToIdentifier(schemaType));
writer.WriteProperty(OpenApiConstants.Type, schemaType.ToIdentifier());
}
}
else if (flagsCount is 2 && (schemaType & JsonSchemaType.Null) == JsonSchemaType.Null) // checks for two values and one is null
{
foreach (JsonSchemaType flag in System.Enum.GetValues(typeof(JsonSchemaType)))
foreach (JsonSchemaType? flag in System.Enum.GetValues(typeof(JsonSchemaType)))
{
// Skip if the flag is not set or if it's the Null flag
if ((schemaType & flag) == flag && flag != JsonSchemaType.Null)
if (schemaType.Value.HasFlag(flag) && flag != JsonSchemaType.Null)

Check warning

Code scanning / CodeQL

Dereferenced variable may be null Warning

Variable
schemaType
may be null at this access because it has a nullable type.
{
// Write the non-null flag value to the writer
writer.WriteProperty(OpenApiConstants.Type, OpenApiTypeMapper.ToIdentifier(flag));
writer.WriteProperty(OpenApiConstants.Type, flag.ToIdentifier());
}
}
if (!Nullable)
Expand Down