Skip to content

Feat: Serialize const prop from 3.1 schemas as single enum #2371

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

Merged
merged 4 commits into from
Jun 4, 2025
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
10 changes: 8 additions & 2 deletions src/Microsoft.OpenApi/Models/OpenApiSchema.cs
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@
/// Initializes a copy of <see cref="IOpenApiSchema"/> object
/// </summary>
/// <param name="schema">The schema object to copy from.</param>
internal OpenApiSchema(IOpenApiSchema schema)

Check warning on line 267 in src/Microsoft.OpenApi/Models/OpenApiSchema.cs

View workflow job for this annotation

GitHub Actions / Build

Refactor this constructor to reduce its Cognitive Complexity from 20 to the 15 allowed. (https://rules.sonarsource.com/csharp/RSPEC-3776)
{
Utils.CheckArgumentNull(schema);
Title = schema.Title ?? Title;
Expand Down Expand Up @@ -336,23 +336,23 @@
SerializeInternal(writer, OpenApiSpecVersion.OpenApi3_0, (writer, element) => element.SerializeAsV3(writer));
}

private static void SerializeBounds(IOpenApiWriter writer, OpenApiSpecVersion version, string propertyName, string exclusivePropertyName, string isExclusivePropertyName, string? value, string? exclusiveValue, bool? isExclusiveValue)

Check warning on line 339 in src/Microsoft.OpenApi/Models/OpenApiSchema.cs

View workflow job for this annotation

GitHub Actions / Build

Method has 8 parameters, which is greater than the 7 authorized. (https://rules.sonarsource.com/csharp/RSPEC-107)

Check warning on line 339 in src/Microsoft.OpenApi/Models/OpenApiSchema.cs

View workflow job for this annotation

GitHub Actions / Build

Refactor this method to reduce its Cognitive Complexity from 17 to the 15 allowed. (https://rules.sonarsource.com/csharp/RSPEC-3776)
{
if (version >= OpenApiSpecVersion.OpenApi3_1)
{
if (!string.IsNullOrEmpty(exclusiveValue) && exclusiveValue is not null)

Check warning on line 343 in src/Microsoft.OpenApi/Models/OpenApiSchema.cs

View workflow job for this annotation

GitHub Actions / Build

Change this condition so that it does not always evaluate to 'True'. (https://rules.sonarsource.com/csharp/RSPEC-2589)
{
// was explicitly set in the document or object model
writer.WritePropertyName(exclusivePropertyName);
writer.WriteRaw(exclusiveValue);
}
else if (isExclusiveValue == true && !string.IsNullOrEmpty(value) && value is not null)

Check warning on line 349 in src/Microsoft.OpenApi/Models/OpenApiSchema.cs

View workflow job for this annotation

GitHub Actions / Build

Change this condition so that it does not always evaluate to 'True'. (https://rules.sonarsource.com/csharp/RSPEC-2589)
{
// came from parsing an old document
writer.WritePropertyName(exclusivePropertyName);
writer.WriteRaw(value);
}
else if (!string.IsNullOrEmpty(value) && value is not null)

Check warning on line 355 in src/Microsoft.OpenApi/Models/OpenApiSchema.cs

View workflow job for this annotation

GitHub Actions / Build

Change this condition so that it does not always evaluate to 'True'. (https://rules.sonarsource.com/csharp/RSPEC-2589)
{
// was explicitly set in the document or object model
writer.WritePropertyName(propertyName);
Expand All @@ -361,14 +361,14 @@
}
else
{
if (!string.IsNullOrEmpty(exclusiveValue) && exclusiveValue is not null)

Check warning on line 364 in src/Microsoft.OpenApi/Models/OpenApiSchema.cs

View workflow job for this annotation

GitHub Actions / Build

Change this condition so that it does not always evaluate to 'True'. (https://rules.sonarsource.com/csharp/RSPEC-2589)
{
// was explicitly set in a new document being downcast or object model
writer.WritePropertyName(propertyName);
writer.WriteRaw(exclusiveValue);
writer.WriteProperty(isExclusivePropertyName, true);
}
else if (!string.IsNullOrEmpty(value) && value is not null)

Check warning on line 371 in src/Microsoft.OpenApi/Models/OpenApiSchema.cs

View workflow job for this annotation

GitHub Actions / Build

Change this condition so that it does not always evaluate to 'True'. (https://rules.sonarsource.com/csharp/RSPEC-2589)
{
// came from parsing an old document, we're just mirroring the information
writer.WritePropertyName(propertyName);
Expand Down Expand Up @@ -430,14 +430,17 @@
// required
writer.WriteOptionalCollection(OpenApiConstants.Required, Required, (w, s) =>
{
if (!string.IsNullOrEmpty(s) && s is not null)

Check warning on line 433 in src/Microsoft.OpenApi/Models/OpenApiSchema.cs

View workflow job for this annotation

GitHub Actions / Build

Change this condition so that it does not always evaluate to 'True'. (https://rules.sonarsource.com/csharp/RSPEC-2589)
{
w.WriteValue(s);
}
});

// enum
writer.WriteOptionalCollection(OpenApiConstants.Enum, Enum, (nodeWriter, s) => nodeWriter.WriteAny(s));
var enumValue = !string.IsNullOrEmpty(Const) && version != OpenApiSpecVersion.OpenApi3_1
? new List<JsonNode> { JsonValue.Create(Const)! }
: Enum;
writer.WriteOptionalCollection(OpenApiConstants.Enum, enumValue, (nodeWriter, s) => nodeWriter.WriteAny(s));

// type
SerializeTypeProperty(writer, version);
Expand Down Expand Up @@ -687,7 +690,10 @@
});

// enum
writer.WriteOptionalCollection(OpenApiConstants.Enum, Enum, (w, s) => w.WriteAny(s));
var enumValue = !string.IsNullOrEmpty(Const)
? new List<JsonNode> { JsonValue.Create(Const)! }
: Enum;
writer.WriteOptionalCollection(OpenApiConstants.Enum, enumValue, (nodeWriter, s) => nodeWriter.WriteAny(s));

// items
writer.WriteOptionalObject(OpenApiConstants.Items, Items, (w, s) => s.SerializeAsV2(w));
Expand Down Expand Up @@ -804,7 +810,7 @@
{
writer.WriteOptionalCollection(OpenApiConstants.Type, array, (w, s) =>
{
if (!string.IsNullOrEmpty(s) && s is not null)

Check warning on line 813 in src/Microsoft.OpenApi/Models/OpenApiSchema.cs

View workflow job for this annotation

GitHub Actions / Build

Change this condition so that it does not always evaluate to 'True'. (https://rules.sonarsource.com/csharp/RSPEC-2589)
{
w.WriteValue(s);
}
Expand Down
38 changes: 38 additions & 0 deletions test/Microsoft.OpenApi.Tests/Models/OpenApiSchemaTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,44 @@ public async Task WriteAsItemsPropertiesDoesNotWriteNull()
""";
Assert.True(JsonNode.DeepEquals(JsonNode.Parse(expected), JsonNode.Parse(actual)));
}
[Fact]
public async Task SerializeConstAsEnumV30()
{
// Arrange
var schema = new OpenApiSchema
{
Type = JsonSchemaType.String,
Const = "foo"
};


// Act
var actual = await schema.SerializeAsJsonAsync(OpenApiSpecVersion.OpenApi3_0);

// Assert
var v3Node = JsonNode.Parse(actual);
Assert.True(v3Node!["enum"] is JsonArray singleEnum && singleEnum.Count == 1 && singleEnum[0]?.ToString() == "foo");
Assert.False(v3Node.AsObject().ContainsKey("const"));
}

[Fact]
public async Task SerializeConstAsEnumV20()
{
// Arrange
var schema = new OpenApiSchema
{
Type = JsonSchemaType.String,
Const = "foo"
};

// Act
var actual = await schema.SerializeAsJsonAsync(OpenApiSpecVersion.OpenApi2_0);

// Assert
var v2Node = JsonNode.Parse(actual);
Assert.True(v2Node!["enum"] is JsonArray singleEnum && singleEnum.Count == 1 && singleEnum[0]?.ToString() == "foo");
Assert.False(v2Node.AsObject().ContainsKey("const"));
}


internal class SchemaVisitor : OpenApiVisitorBase
Expand Down